PracowniaProgramowania/backend/connector_couchdb.go

76 lines
1.3 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`
Timestamp int64 `json:"timestamp`
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-06 16:00:15 +01:00
func addNewCard(_isQuestion bool, _blank int, _text string) {
if !_isQuestion {
fmt.Printf("NIE")
_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-06 16:00:15 +01:00
fmt.Println(reflect.TypeOf(result))
2018-11-20 00:32:55 +01:00
if err := db.Get(doc, result.ID); err != nil { // get id and current revision.
2019-01-07 00:44:47 +01:00
panic(err)
}
fmt.Printf(result.ID)
2019-01-06 16:00:15 +01:00
}
2018-11-20 00:32:55 +01:00
2019-01-06 16:00:15 +01:00
func main() {
addNewCard(true, 1, "moj_testowy_text")
2019-01-06 16:00:15 +01:00
}