144 lines
3.1 KiB
Go
144 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"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
|
|
}
|
|
|
|
var myClient = &http.Client{Timeout: 10 * time.Second}
|
|
|
|
func getJson(url string, target interface{}) error {
|
|
r, err := myClient.Get(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Body.Close()
|
|
|
|
return json.NewDecoder(r.Body).Decode(target)
|
|
}
|
|
|
|
func getAllDoc() {
|
|
// db := connectCouchdb()
|
|
// zmiena := db.AllDocs()
|
|
// allCards := []Card{}
|
|
|
|
resp, err := http.Get("http://localhost:5984/golang_cards/_all_docs?include_docs=true")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
var generic map[string]interface{}
|
|
//fmt.Println(resp.Body) //&{0xc000040740 {0 0} false <nil> 0x693ad0 0x693a50}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&generic)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
keys := reflect.ValueOf(generic).MapKeys()
|
|
fmt.Println("KEYS: ", keys)
|
|
|
|
for k, v := range generic {
|
|
if k == "rows" {
|
|
switch val := v.(type) {
|
|
// case string:
|
|
// fmt.Println(k, "is string", val)
|
|
// case int:
|
|
// fmt.Println(k, "is int", val)
|
|
case []interface{}:
|
|
fmt.Println(k, "is an array")
|
|
for _, v := range val {
|
|
// fmt.Println(v) //, i)
|
|
// fmt.Println(reflect.TypeOf(v))
|
|
// fmt.Println(i)
|
|
mResult := v.(map[string]interface{})
|
|
// fmt.Println(mResult["doc"])
|
|
// fmt.Println(reflect.TypeOf(mResult["doc"]))
|
|
|
|
mResult2 := mResult["doc"].(map[string]interface{})
|
|
// fmt.Println(reflect.TypeOf(mResult2))
|
|
fmt.Println(mResult2["Text"])
|
|
fmt.Println(mResult2["Timestamp"])
|
|
fmt.Println(mResult2["_id"])
|
|
fmt.Println(mResult2["blank"])
|
|
|
|
// mResult2 := mResult.(map[string]interface{})
|
|
// fmt.Println(mResult2["Timestamp"])
|
|
|
|
}
|
|
// default:
|
|
// fmt.Println(k, "is unknown type")
|
|
}
|
|
}
|
|
}
|
|
// fmt.Println("Zmienna: ", zmiena)
|
|
|
|
}
|