From dc1778a5b9aa90dba795dbf6d2edae9cdb956052 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Fri, 16 Nov 2018 11:48:45 +0100 Subject: [PATCH] Protecting Created Date, updating docs, changin errors to JSON --- Readme.md | 7 ++++--- controllers/companyData.go | 18 +++++++++--------- controllers/companyUser.go | 12 ++++++------ controllers/contact.go | 22 ++++++++++------------ controllers/post.go | 33 +++++++++++++++++++++------------ models/companyData.go | 1 + models/companyUser.go | 1 + models/contact.go | 1 + models/post.go | 4 ++-- models/userCompanyMap.go | 1 + swagger/swagger.json | 19 ++++++++++++++++++- swagger/swagger.yml | 13 ++++++++++++- 12 files changed, 86 insertions(+), 46 deletions(-) diff --git a/Readme.md b/Readme.md index e4a5df7..0ac32e1 100644 --- a/Readme.md +++ b/Readme.md @@ -19,12 +19,13 @@ Todo till we can fork this repo - hardcoded roles * ~~/register endpoint creates database, company and first admin~~ -* checking some roles in all endpoints * ~~load db connections from config~~ * ~~user delete needs to update system~~ * ~~company delete needs to exist and update usercompanymap~~ -* modified by (companyuserid) -* last modified in update for all tables +* ~~last modified in update for all tables~~ +* checking some roles in all endpoints +* modified by relation (companyuserid) +* return error in case of not found ## Notes: diff --git a/controllers/companyData.go b/controllers/companyData.go index 780c334..14c0559 100644 --- a/controllers/companyData.go +++ b/controllers/companyData.go @@ -36,10 +36,10 @@ func (c *CompanyDataController) Post() { c.Ctx.Output.SetStatus(201) c.Data["json"] = v } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } @@ -56,7 +56,7 @@ func (c *CompanyDataController) GetOne() { id, _ := strconv.Atoi(idStr) v, err := models.GetCompanyDataById(o, id) if err != nil { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } else { c.Data["json"] = v } @@ -119,7 +119,7 @@ func (c *CompanyDataController) GetAll() { l, err := models.GetAllCompanyData(o, query, fields, sortby, order, offset, limit) if err != nil { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } else { c.Data["json"] = l } @@ -140,12 +140,12 @@ func (c *CompanyDataController) Put() { v := models.CompanyData{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := models.UpdateCompanyDataById(o, &v); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Ok") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } @@ -161,9 +161,9 @@ func (c *CompanyDataController) Delete() { idStr := c.Ctx.Input.Param(":id") id, _ := strconv.Atoi(idStr) if err := models.DeleteCompanyData(o, id); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Ok") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } diff --git a/controllers/companyUser.go b/controllers/companyUser.go index e46608f..8d1d3d6 100644 --- a/controllers/companyUser.go +++ b/controllers/companyUser.go @@ -26,7 +26,7 @@ func (c *CompanyUserController) URLMapping() { c.Mapping("GetAll", c.GetAll) c.Mapping("Put", c.Put) c.Mapping("Delete", c.Delete) - c.Mapping("Delete", c.DeleteCompany) + c.Mapping("DeleteCompany", c.DeleteCompany) } // Post ... @@ -106,7 +106,7 @@ func (c *CompanyUserController) GetOne() { id, _ := strconv.Atoi(idStr) v, err := models.GetCompanyUserById(o, id) if err != nil { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } else { c.Data["json"] = v } @@ -169,7 +169,7 @@ func (c *CompanyUserController) GetAll() { l, err := models.GetAllCompanyUser(o, query, fields, sortby, order, offset, limit) if err != nil { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } else { c.Data["json"] = l } @@ -190,12 +190,12 @@ func (c *CompanyUserController) Put() { v := models.CompanyUser{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := models.UpdateCompanyUserById(o, &v); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Ok") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } diff --git a/controllers/contact.go b/controllers/contact.go index c626c00..bd3246c 100644 --- a/controllers/contact.go +++ b/controllers/contact.go @@ -6,8 +6,6 @@ import ( "multitenantStack/models" "strconv" "strings" - - "github.com/astaxie/beego/orm" ) // ContactController operations for Contact @@ -38,10 +36,10 @@ func (c *ContactController) Post() { c.Ctx.Output.SetStatus(201) c.Data["json"] = v } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } @@ -56,9 +54,9 @@ func (c *ContactController) Post() { func (c *ContactController) GetOne() { idStr := c.Ctx.Input.Param(":id") id, _ := strconv.Atoi(idStr) - v, err := models.GetContactById(orm.NewOrm(), id) + v, err := models.GetContactById(o, id) if err != nil { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } else { c.Data["json"] = v } @@ -121,7 +119,7 @@ func (c *ContactController) GetAll() { l, err := models.GetAllContact(o, query, fields, sortby, order, offset, limit) if err != nil { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } else { c.Data["json"] = l } @@ -143,12 +141,12 @@ func (c *ContactController) Put() { v := models.Contact{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := models.UpdateContactById(o, &v); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Ok") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } @@ -164,9 +162,9 @@ func (c *ContactController) Delete() { idStr := c.Ctx.Input.Param(":id") id, _ := strconv.Atoi(idStr) if err := models.DeleteContact(o, id); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Ok") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } diff --git a/controllers/post.go b/controllers/post.go index e454212..bfb9b16 100644 --- a/controllers/post.go +++ b/controllers/post.go @@ -36,10 +36,10 @@ func (c *PostController) Post() { c.Ctx.Output.SetStatus(201) c.Data["json"] = v } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } @@ -56,11 +56,16 @@ func (c *PostController) GetOne() { id, _ := strconv.Atoi(idStr) v, err := models.GetPostById(o, id) if err != nil { - c.Data["json"] = err.Error() + if err.Error() == " no row found" { + c.ServeJSONError("Post does not exist") + return + } } else { c.Data["json"] = v + c.ServeJSON() + return } - c.ServeJSON() + c.ServeJSONError("Error retrieving Post") } // GetAll ... @@ -119,11 +124,16 @@ func (c *PostController) GetAll() { l, err := models.GetAllPost(o, query, fields, sortby, order, offset, limit) if err != nil { - c.Data["json"] = err.Error() + if err.Error() == " no row found" { + c.ServeJSONError("No Posts found") + return + } } else { c.Data["json"] = l + c.ServeJSON() + return } - c.ServeJSON() + c.ServeJSONError("Error retrieving Post") } // Put ... @@ -140,14 +150,13 @@ func (c *PostController) Put() { v := models.Post{Id: id} if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := models.UpdatePostById(o, &v); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Updated Post") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } - c.ServeJSON() } // Delete ... @@ -161,9 +170,9 @@ func (c *PostController) Delete() { idStr := c.Ctx.Input.Param(":id") id, _ := strconv.Atoi(idStr) if err := models.DeletePost(o, id); err == nil { - c.Data["json"] = "OK" + c.ServeJSONSuccess("Ok") } else { - c.Data["json"] = err.Error() + c.ServeJSONErrorWithError("Error", err) } c.ServeJSON() } diff --git a/models/companyData.go b/models/companyData.go index 0afb47d..601b697 100644 --- a/models/companyData.go +++ b/models/companyData.go @@ -128,6 +128,7 @@ func UpdateCompanyDataById(o orm.Ormer, m *CompanyData) (err error) { v := CompanyData{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { + m.Created = v.Created var num int64 if num, err = o.Update(m); err == nil { fmt.Println("Number of records updated in database:", num) diff --git a/models/companyUser.go b/models/companyUser.go index 8e569ae..13e4be2 100644 --- a/models/companyUser.go +++ b/models/companyUser.go @@ -128,6 +128,7 @@ func UpdateCompanyUserById(o orm.Ormer, m *CompanyUser) (err error) { // ascertain id exists in the database if err = o.Read(&v); err == nil { var num int64 + m.Created = v.Created if num, err = o.Update(m); err == nil { fmt.Println("Number of records updated in database:", num) } diff --git a/models/contact.go b/models/contact.go index 43790e7..5f60314 100644 --- a/models/contact.go +++ b/models/contact.go @@ -132,6 +132,7 @@ func UpdateContactById(o orm.Ormer, m *Contact) (err error) { // ascertain id exists in the database if err = o.Read(&v); err == nil { var num int64 + m.Created = v.Created if num, err = o.Update(m); err == nil { fmt.Println("Number of records updated in database:", num) } diff --git a/models/post.go b/models/post.go index 78970c5..14b9f66 100644 --- a/models/post.go +++ b/models/post.go @@ -121,14 +121,14 @@ func GetAllPost(o orm.Ormer, query map[string]string, fields []string, sortby [] return nil, err } -// UpdatePost updates Post by Id and returns error if -// the record to be updated doesn't exist +// UpdatePost updates Post by Id and returns error if the record to be updated doesn't exist func UpdatePostById(o orm.Ormer, m *Post) (err error) { m.Modified = time.Now() v := Post{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { var num int64 + m.Created = v.Created if num, err = o.Update(m); err == nil { fmt.Println("Number of records updated in database:", num) } diff --git a/models/userCompanyMap.go b/models/userCompanyMap.go index 255b311..a6fa86c 100644 --- a/models/userCompanyMap.go +++ b/models/userCompanyMap.go @@ -159,6 +159,7 @@ func UpdateUserCompanyMapById(o orm.Ormer, m *UserCompanyMap) (err error) { // ascertain id exists in the database if err = o.Read(&v); err == nil { var num int64 + m.Created = v.Created if num, err = o.Update(m); err == nil { fmt.Println("Number of records updated in database:", num) } diff --git a/swagger/swagger.json b/swagger/swagger.json index 197b919..ed45fd9 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -657,7 +657,7 @@ "tags": [ "user" ], - "description": "create CompanyUser", + "description": "Create a new CompanyUser and his user company mapping", "operationId": "CompanyUserController.Post", "parameters": [ { @@ -680,6 +680,23 @@ } } }, + "/user/deletecompany": { + "delete": { + "tags": [ + "user" + ], + "description": "Delete the entire Company", + "operationId": "CompanyUserController.Delete Company", + "responses": { + "200": { + "description": "{string} delete success!" + }, + "403": { + "description": "failed" + } + } + } + }, "/user/{id}": { "get": { "tags": [ diff --git a/swagger/swagger.yml b/swagger/swagger.yml index 72eb997..d05d2d5 100644 --- a/swagger/swagger.yml +++ b/swagger/swagger.yml @@ -438,7 +438,7 @@ paths: post: tags: - user - description: create CompanyUser + description: Create a new CompanyUser and his user company mapping operationId: CompanyUserController.Post parameters: - in: body @@ -511,6 +511,17 @@ paths: description: '{string} delete success!' "403": description: id is empty + /user/deletecompany: + delete: + tags: + - user + description: Delete the entire Company + operationId: CompanyUserController.Delete Company + responses: + "200": + description: '{string} delete success!' + "403": + description: failed definitions: models.Auth: title: Auth