2018-11-20 00:32:55 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"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 00:24:09 +01:00
|
|
|
ID int `json:"id" binding:"required"`
|
|
|
|
isQuestion bool `json:"typ"` //True karta pytanie, False karta odpowiedź
|
|
|
|
Blank int `json:"puste" binding:"required"` //ile kart odpowiedzi na pytanie
|
|
|
|
Text string `json:"tekst" binding:"required"` // podłoga to luka
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func connectCouchdb(url_couchdb string) {
|
|
|
|
if url_couchdb == "" {
|
|
|
|
url_couchdb = "http://127.0.0.1:5984/"
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(url_couchdb)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
2019-01-06 01:53:33 +01:00
|
|
|
func addNewCard(){
|
|
|
|
u, err = connectCouchdb("")
|
|
|
|
client, err := couchdb.NewClient(u)
|
|
|
|
|
|
|
|
db := client.Use("golangCards")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-20 00:32:55 +01:00
|
|
|
// 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")
|
2019-01-06 00:24:09 +01:00
|
|
|
doc := &cardDocument{
|
2018-11-20 00:32:55 +01:00
|
|
|
Foo: "bar",
|
|
|
|
Beep: "bopp",
|
2019-01-06 01:53:33 +01:00
|
|
|
ID int
|
|
|
|
isQuestion bool //True karta pytanie, False karta odpowiedź
|
|
|
|
Blank int //ile kart odpowiedzi na pytanie
|
|
|
|
Text string // podłoga to luka
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|