73 lines
1.3 KiB
Go
73 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` //na froncie jest
|
|
}
|
|
|
|
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) error {
|
|
if !_isQuestion { //jezeli nie jest karta pytanie to zero pol pustych
|
|
_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)
|
|
return err
|
|
}
|
|
fmt.Println(reflect.TypeOf(result))
|
|
|
|
if err := db.Get(doc, result.ID); err != nil { // get id and current revision.
|
|
panic(err)
|
|
return err //nie matakiego dodane pliku - cos sie nie powiodlo
|
|
}
|
|
|
|
fmt.Printf(result.ID)
|
|
return nil
|
|
}
|