PracowniaProgramowania/backend/connector_couchdb.go

76 lines
1.3 KiB
Go

package main
import (
"fmt"
"net/url"
"reflect"
"time"
// "github.com/mikebell-org/go-couchdb"
"github.com/zemirco/couchdb"
)
// create your own document
type cardDocument struct {
couchdb.Document
IsQuestion bool `json:"isquestion"`
Blank int `json:"blank"`
Text string `json:"text`
Timestamp int64 `json:"timestamp`
}
func connectCouchdb() couchdb.DatabaseService {
url_couchdb := "http://root:password@127.0.0.1:5984/"
u, err := url.Parse(url_couchdb)
if err != nil {
panic(err)
}
client, err := couchdb.NewClient(u)
if err != nil {
panic(err)
}
db := client.Use("golang_cards")
return db
}
func addNewCard(_isQuestion bool, _blank int, _text string) {
if !_isQuestion {
fmt.Printf("NIE")
_blank = 0
}
db := connectCouchdb()
now := time.Now()
unixNano := now.UnixNano()
umillisec := unixNano / 1000000
doc := &cardDocument{ //struktura karty
IsQuestion: _isQuestion,
Blank: _blank,
Text: _text,
Timestamp: umillisec,
}
result, err := db.Post(doc)
if err != nil {
panic(err)
}
fmt.Println(reflect.TypeOf(result))
if err := db.Get(doc, result.ID); err != nil { // get id and current revision.
panic(err)
}
fmt.Printf(result.ID)
}
func main() {
addNewCard(true, 1, "moj_testowy_text")
}