PracowniaProgramowania/backend/connector_couchdb.go

129 lines
2.6 KiB
Go
Raw Normal View History

2018-11-20 00:32:55 +01:00
package main
import (
2019-01-13 05:29:57 +01:00
"encoding/json"
2018-11-20 00:32:55 +01:00
"fmt"
2019-01-13 05:29:57 +01:00
"log"
"net/http"
2018-11-20 00:32:55 +01:00
"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
}
2019-01-13 05:29:57 +01:00
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)
// }
// // default:
// // fmt.Println(k, "is unknown type")
// }
// }
// }
// fmt.Println("Zmienna: ", zmiena)
}