PracowniaProgramowania/backend/connector_couchdb.go

73 lines
1.4 KiB
Go
Raw Normal View History

2018-11-20 00:32:55 +01:00
package main
import (
"fmt"
"net/url"
2019-01-06 16:00:15 +01:00
"reflect"
"time"
2018-11-20 00:32:55 +01:00
// "github.com/mikebell-org/go-couchdb"
2018-11-20 00:32:55 +01:00
"github.com/zemirco/couchdb"
)
// create your own document
type cardDocument struct {
2018-11-20 00:32:55 +01:00
couchdb.Document
2019-01-06 16:00:15 +01:00
IsQuestion bool `json:"isquestion"`
Blank int `json:"blank"`
Text string `json:"text`
2019-01-09 02:29:12 +01:00
Timestamp int64 `json:"timestamp` //na froncie jest
2019-01-06 16:00:15 +01:00
}
func connectCouchdb() couchdb.DatabaseService {
url_couchdb := "http://root:password@127.0.0.1:5984/"
2018-11-20 00:32:55 +01:00
u, err := url.Parse(url_couchdb)
if err != nil {
panic(err)
}
client, err := couchdb.NewClient(u)
if err != nil {
panic(err)
}
2019-01-06 16:00:15 +01:00
db := client.Use("golang_cards")
return db
}
2018-11-20 00:32:55 +01:00
2019-01-09 07:26:21 +01:00
func addNewCard(_isQuestion bool, _blank int, _text string) error {
if !_isQuestion { //jezeli nie jest karta pytanie to zero pol pustych
2019-01-06 16:00:15 +01:00
_blank = 0
2018-11-20 00:32:55 +01:00
}
2019-01-06 16:00:15 +01:00
db := connectCouchdb()
now := time.Now()
unixNano := now.UnixNano()
umillisec := unixNano / 1000000
2019-01-06 16:00:15 +01:00
doc := &cardDocument{ //struktura karty
IsQuestion: _isQuestion,
Blank: _blank,
Text: _text,
Timestamp: umillisec,
2018-11-20 00:32:55 +01:00
}
result, err := db.Post(doc)
if err != nil {
panic(err)
2019-01-10 03:45:25 +01:00
// return err
2018-11-20 00:32:55 +01:00
}
2019-01-06 16:00:15 +01:00
fmt.Println(reflect.TypeOf(result))
2018-11-20 00:32:55 +01:00
2019-01-10 03:45:25 +01:00
// 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
// }
2019-01-07 00:44:47 +01:00
fmt.Printf(result.ID)
2019-01-09 07:26:21 +01:00
return nil
2019-01-06 16:00:15 +01:00
}