2018-11-20 00:32:55 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2019-01-06 16:00:15 +01:00
|
|
|
"reflect"
|
2018-11-20 00:32:55 +01:00
|
|
|
|
|
|
|
"github.com/zemirco/couchdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// create your own document
|
2019-01-06 00:24:09 +01:00
|
|
|
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`
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 16:00:15 +01:00
|
|
|
type dummyDocument struct {
|
|
|
|
couchdb.Document
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
Beep string `json:"beep"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectCouchdb() couchdb.DatabaseService {
|
|
|
|
/*
|
|
|
|
http://127.0.0.1:5984/_utils/#database/golang_cards/_all_docs
|
|
|
|
*/
|
|
|
|
// if url_couchdb == "" {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-06 01:53:33 +01:00
|
|
|
client, err := couchdb.NewClient(u)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-06 16:00:15 +01:00
|
|
|
// return client, err
|
|
|
|
fmt.Println(reflect.TypeOf(client))
|
|
|
|
db := client.Use("golang_cards")
|
|
|
|
// return db
|
|
|
|
fmt.Println(reflect.TypeOf(db))
|
|
|
|
return db
|
|
|
|
|
2019-01-06 01:53:33 +01:00
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
doc := &cardDocument{ //struktura karty
|
|
|
|
IsQuestion: _isQuestion,
|
|
|
|
Blank: _blank,
|
|
|
|
Text: _text,
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 16:00:15 +01:00
|
|
|
// doc := &dummyDocument{
|
|
|
|
// Foo: "bar",
|
|
|
|
// Beep: "bopp",
|
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.Printf(result)
|
|
|
|
fmt.Println(reflect.TypeOf(result))
|
2018-11-20 00:32:55 +01:00
|
|
|
|
2019-01-07 00:44:47 +01:00
|
|
|
// get id and current revision.
|
|
|
|
if err := db.Get(doc, result.ID); err != nil {
|
|
|
|
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() {
|
|
|
|
// db := connectCouchdb()
|
|
|
|
addNewCard(true, 1, "_text")
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
2019-01-06 16:00:15 +01:00
|
|
|
|
|
|
|
// func addNewCard(){
|
|
|
|
// u, err = connectCouchdb("")
|
|
|
|
// client, err := couchdb.NewClient(u)
|
|
|
|
|
|
|
|
// db := client.Use("golang_cards")
|
|
|
|
// doc := &cardDocument{
|
|
|
|
// Foo: "bar",
|
|
|
|
// Beep: "bopp",
|
|
|
|
// ID int
|
|
|
|
// isQuestion bool //True karta pytanie, False karta odpowiedź
|
|
|
|
// Blank int //ile kart odpowiedzi na pytanie
|
|
|
|
// Text string // podłoga to luka
|
|
|
|
// }
|
|
|
|
// result, err := db.Post(doc)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
// // start
|
|
|
|
// func main() {
|
|
|
|
|
|
|
|
// u, err = connectCouchdb("")
|
|
|
|
// // create a new client
|
|
|
|
// client, err := couchdb.NewClient(u)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // get some information about your CouchDB
|
|
|
|
// info, err := client.Info()
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// fmt.Println(info)
|
|
|
|
|
|
|
|
// // // create a database
|
|
|
|
// // if _, err = client.Create("dummy"); err != nil {
|
|
|
|
// // panic(err)
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// // use your new "dummy" database and create a document
|
|
|
|
// db := client.Use("dummy")
|
|
|
|
// doc := &cardDocument{
|
|
|
|
// Foo: "bar",
|
|
|
|
// Beep: "bopp",
|
|
|
|
// ID int
|
|
|
|
// isQuestion bool //True karta pytanie, False karta odpowiedź
|
|
|
|
// Blank int //ile kart odpowiedzi na pytanie
|
|
|
|
// Text string // podłoga to luka
|
|
|
|
// }
|
|
|
|
// result, err := db.Post(doc)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // get id and current revision.
|
|
|
|
// if err := db.Get(doc, result.ID); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // delete document
|
|
|
|
// if _, err = db.Delete(doc); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // and finally delete the database
|
|
|
|
// if _, err = client.Delete("dummy"); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|