Skip to content

Commit

Permalink
Groundwork for normalization, fixes #6 and closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Torjas committed Jun 27, 2016
1 parent 863143b commit 3d7d066
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 174 deletions.
95 changes: 24 additions & 71 deletions backend/ducklib/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ducklib

import (
"encoding/json"
"errors"
"fmt"

"github.com/Microsoft/DUCK/backend/ducklib/structs"
Expand Down Expand Up @@ -34,19 +33,21 @@ func FillTestdata(data []byte) error {
for _, l := range listOfData {

mp := l.(map[string]interface{})
id := mp["_id"].(string)

entryType := mp["type"].(string)
entry, err := json.Marshal(l)
if err != nil {
return err
}

switch entryType {
case "document":
if err := db.NewDocument(id, string(entry)); err != nil {
var d structs.Document
d.FromValueMap(mp)

if err := db.NewDocument(d); err != nil {
return err
}
case "user":
if err := db.NewUser(id, string(entry)); err != nil {
var u structs.User
u.FromValueMap(mp)
if err := db.NewUser(u); err != nil {
return err
}

Expand Down Expand Up @@ -78,47 +79,28 @@ func (database *Database) GetLogin(username string) (id string, pw string, err e
}

func (database *Database) GetUser(userid string) (structs.User, error) {
var u structs.User
mp, err := db.GetUser(userid)
if err != nil {
return u, err
}

u.FromValueMap(mp)
return db.GetUser(userid)

return u, err
}

func (database *Database) DeleteUser(id string) error {

doc, err := db.GetDocument(id)
if err != nil {

return err
}
if rev, prs := doc["_rev"]; prs {
err := db.DeleteDocument(id, rev.(string))
if err != nil {
return err
}
return nil
}

return errors.New("Could not delete Entry")
return db.DeleteUser(id)

}

func (database *Database) PutUser(id string, content []byte) error {
func (database *Database) PutUser(user structs.User) error {

return db.UpdateDocument(id, string(content))
return db.UpdateUser(user)

}

func (database *Database) PostUser(content []byte) (string, error) {
func (database *Database) PostUser(user structs.User) (ID string, err error) {
u := uuid.NewV4()
uuid := uuid.Formatter(u, uuid.Clean)

return uuid, db.NewDocument(uuid, string(content))
user.ID = uuid
return uuid, db.NewUser(user)

}

Expand All @@ -127,62 +109,33 @@ Document DB operations
*/
func (database *Database) GetDocument(documentid string) (structs.Document, error) {
var doc structs.Document
mp, err := db.GetDocument(documentid)
if err != nil {
return doc, err
}

doc.FromValueMap(mp)
return db.GetDocument(documentid)

return doc, err
}
func (database *Database) GetDocumentSummariesForUser(userid string) ([]structs.Document, error) {
var docs []structs.Document
list, err := db.GetDocumentSummariesForUser(userid)
if err != nil {
fmt.Println(err.Error())
return docs, err
}

for _, item := range list {
docs = append(docs, structs.Document{Name: item["name"], ID: item["id"]})
}

return docs, nil
return db.GetDocumentSummariesForUser(userid)

}

func (database *Database) DeleteDocument(id string) error {

doc, err := db.GetDocument(id)
if err != nil {

return err
}
if rev, prs := doc["_rev"]; prs {
err := db.DeleteDocument(id, rev.(string))
if err != nil {
return err
}
return nil
}

return errors.New("Could not delete Entry")
return db.DeleteDocument(id)

}

func (database *Database) PutDocument(id string, content []byte) error {
func (database *Database) PutDocument(doc structs.Document) error {

return db.UpdateDocument(id, string(content))
return db.UpdateDocument(doc)

}

func (database *Database) PostDocument(content []byte) (string, error) {
func (database *Database) PostDocument(doc structs.Document) (ID string, err error) {
u := uuid.NewV4()
uuid := uuid.Formatter(u, uuid.Clean)

return uuid, db.NewDocument(uuid, string(content))
doc.ID = uuid
return uuid, db.NewDocument(doc)

}

Expand Down
59 changes: 35 additions & 24 deletions backend/ducklib/handlers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ducklib

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -60,6 +60,7 @@ func getDocHandler(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
fmt.Printf("%+v\n", doc)
return c.JSON(http.StatusOK, doc)
}
func deleteDocHandler(c echo.Context) error {
Expand All @@ -74,49 +75,58 @@ func deleteDocHandler(c echo.Context) error {

func putDocHandler(c echo.Context) error {

resp, err := ioutil.ReadAll(c.Request().Body())
/*resp, err := ioutil.ReadAll(c.Request().Body())
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

data := structs.Document{}
json.Unmarshal(resp, &data)

err = datab.PutDocument(data.ID, resp)
fmt.Println(string(resp))
*/
doc := new(structs.Document)
if err := c.Bind(doc); err != nil {
e := err.Error()
fmt.Println("1")
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}
err := datab.PutDocument(*doc)
if err != nil {
e := err.Error()
fmt.Printf("%+v", *doc)
fmt.Println(e)
fmt.Println("2")
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

doc, err := datab.GetDocument(data.ID)
docu, err := datab.GetDocument(doc.ID)
if err != nil {
e := err.Error()

fmt.Println("3")
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

return c.JSON(http.StatusOK, doc)
return c.JSON(http.StatusOK, docu)
}
func postDocHandler(c echo.Context) error {

req, err := ioutil.ReadAll(c.Request().Body())
if err != nil {
doc := new(structs.Document)
if err := c.Bind(doc); err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

id, err := datab.PostDocument(req)
id, err := datab.PostDocument(*doc)
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}
doc, err := datab.GetDocument(id)
docu, err := datab.GetDocument(id)
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

return c.JSON(http.StatusOK, doc)
return c.JSON(http.StatusOK, docu)
}

/*
Expand All @@ -135,46 +145,47 @@ func deleteUserHandler(c echo.Context) error {

func putUserHandler(c echo.Context) error {

resp, err := ioutil.ReadAll(c.Request().Body())
if err != nil {
u := new(structs.User)
if err := c.Bind(u); err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}
id := c.Param("id")
err = datab.PutUser(id, resp)
u.ID = id
err := datab.PutUser(*u)
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

doc, err := datab.GetUser(id)
us, err := datab.GetUser(id)
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

return c.JSON(http.StatusOK, doc)
return c.JSON(http.StatusOK, us)
}
func postUserHandler(c echo.Context) error {

req, err := ioutil.ReadAll(c.Request().Body())
if err != nil {
var u structs.User
if err := c.Bind(u); err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

id, err := datab.PostUser(req)
id, err := datab.PostUser(u)
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}
doc, err := datab.GetUser(id)
u, err = datab.GetUser(id)
if err != nil {
e := err.Error()
return c.JSON(http.StatusNotFound, structs.Response{Ok: false, Reason: &e})
}

return c.JSON(http.StatusOK, doc)
return c.JSON(http.StatusOK, u)
}

/*
Expand Down
14 changes: 9 additions & 5 deletions backend/ducklib/structs/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package structs
type Document struct {
ID string `json:"id"`
Name string `json:"name"`
Revision string `json:"_rev"`
Revision string `json:"revision"`
Owner string `json:"owner"`
Locale string `json:"locale"`
Statements []Statement `json:"statements"`
Expand Down Expand Up @@ -38,12 +38,16 @@ func (d *Document) FromValueMap(mp map[string]interface{}) {
d.Locale = locale.(string)
}

d.Statements = make([]Statement, 0)
if stmts, prs := mp["statements"].([]interface{}); prs {
d.Statements = make([]Statement, len(stmts))
for i, stmt := range stmts {

for _, stmt := range stmts {
s := new(Statement)
s.FromInterfaceMap(stmt.(map[string]interface{}))
d.Statements[i] = *s
if stmt != nil {
s.FromInterfaceMap(stmt.(map[string]interface{}))
d.Statements = append(d.Statements, *s)
}

}
}

Expand Down
8 changes: 4 additions & 4 deletions backend/ducklib/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Locale string `json:"locale"`
Revision string `json:"_rev"`
Documents []string `json:"documents"`
Revision string `json:"revision"`
//Documents []string `json:"documents"`
}

//Response represents a JSON response from the ducklib server
Expand Down Expand Up @@ -54,12 +54,12 @@ func (u *User) FromValueMap(mp map[string]interface{}) {
u.Locale = locale.(string)
}

if docs, prs := mp["documents"].([]interface{}); prs {
/* if docs, prs := mp["documents"].([]interface{}); prs {
u.Documents = make([]string, len(docs))
for i, v := range docs {
u.Documents[i] = v.(string)
}
}
}*/

}

Expand Down
Loading

0 comments on commit 3d7d066

Please sign in to comment.