Protecting Created Date, updating docs, changin errors to JSON

This commit is contained in:
Lukas Bachschwell 2018-11-16 11:48:45 +01:00
parent cb7a46617d
commit dc1778a5b9
12 changed files with 86 additions and 46 deletions

View File

@ -19,12 +19,13 @@ Todo till we can fork this repo
- hardcoded roles - hardcoded roles
* ~~/register endpoint creates database, company and first admin~~ * ~~/register endpoint creates database, company and first admin~~
* checking some roles in all endpoints
* ~~load db connections from config~~ * ~~load db connections from config~~
* ~~user delete needs to update system~~ * ~~user delete needs to update system~~
* ~~company delete needs to exist and update usercompanymap~~ * ~~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: ## Notes:

View File

@ -36,10 +36,10 @@ func (c *CompanyDataController) Post() {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(201)
c.Data["json"] = v c.Data["json"] = v
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }
@ -56,7 +56,7 @@ func (c *CompanyDataController) GetOne() {
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v, err := models.GetCompanyDataById(o, id) v, err := models.GetCompanyDataById(o, id)
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} else { } else {
c.Data["json"] = v c.Data["json"] = v
} }
@ -119,7 +119,7 @@ func (c *CompanyDataController) GetAll() {
l, err := models.GetAllCompanyData(o, query, fields, sortby, order, offset, limit) l, err := models.GetAllCompanyData(o, query, fields, sortby, order, offset, limit)
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} else { } else {
c.Data["json"] = l c.Data["json"] = l
} }
@ -140,12 +140,12 @@ func (c *CompanyDataController) Put() {
v := models.CompanyData{Id: id} v := models.CompanyData{Id: id}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if err := models.UpdateCompanyDataById(o, &v); err == nil { if err := models.UpdateCompanyDataById(o, &v); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Ok")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }
@ -161,9 +161,9 @@ func (c *CompanyDataController) Delete() {
idStr := c.Ctx.Input.Param(":id") idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
if err := models.DeleteCompanyData(o, id); err == nil { if err := models.DeleteCompanyData(o, id); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Ok")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }

View File

@ -26,7 +26,7 @@ func (c *CompanyUserController) URLMapping() {
c.Mapping("GetAll", c.GetAll) c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put) c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete) c.Mapping("Delete", c.Delete)
c.Mapping("Delete", c.DeleteCompany) c.Mapping("DeleteCompany", c.DeleteCompany)
} }
// Post ... // Post ...
@ -106,7 +106,7 @@ func (c *CompanyUserController) GetOne() {
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v, err := models.GetCompanyUserById(o, id) v, err := models.GetCompanyUserById(o, id)
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} else { } else {
c.Data["json"] = v c.Data["json"] = v
} }
@ -169,7 +169,7 @@ func (c *CompanyUserController) GetAll() {
l, err := models.GetAllCompanyUser(o, query, fields, sortby, order, offset, limit) l, err := models.GetAllCompanyUser(o, query, fields, sortby, order, offset, limit)
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} else { } else {
c.Data["json"] = l c.Data["json"] = l
} }
@ -190,12 +190,12 @@ func (c *CompanyUserController) Put() {
v := models.CompanyUser{Id: id} v := models.CompanyUser{Id: id}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if err := models.UpdateCompanyUserById(o, &v); err == nil { if err := models.UpdateCompanyUserById(o, &v); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Ok")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }

View File

@ -6,8 +6,6 @@ import (
"multitenantStack/models" "multitenantStack/models"
"strconv" "strconv"
"strings" "strings"
"github.com/astaxie/beego/orm"
) )
// ContactController operations for Contact // ContactController operations for Contact
@ -38,10 +36,10 @@ func (c *ContactController) Post() {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(201)
c.Data["json"] = v c.Data["json"] = v
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }
@ -56,9 +54,9 @@ func (c *ContactController) Post() {
func (c *ContactController) GetOne() { func (c *ContactController) GetOne() {
idStr := c.Ctx.Input.Param(":id") idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v, err := models.GetContactById(orm.NewOrm(), id) v, err := models.GetContactById(o, id)
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} else { } else {
c.Data["json"] = v c.Data["json"] = v
} }
@ -121,7 +119,7 @@ func (c *ContactController) GetAll() {
l, err := models.GetAllContact(o, query, fields, sortby, order, offset, limit) l, err := models.GetAllContact(o, query, fields, sortby, order, offset, limit)
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} else { } else {
c.Data["json"] = l c.Data["json"] = l
} }
@ -143,12 +141,12 @@ func (c *ContactController) Put() {
v := models.Contact{Id: id} v := models.Contact{Id: id}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if err := models.UpdateContactById(o, &v); err == nil { if err := models.UpdateContactById(o, &v); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Ok")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }
@ -164,9 +162,9 @@ func (c *ContactController) Delete() {
idStr := c.Ctx.Input.Param(":id") idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
if err := models.DeleteContact(o, id); err == nil { if err := models.DeleteContact(o, id); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Ok")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }

View File

@ -36,10 +36,10 @@ func (c *PostController) Post() {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(201)
c.Data["json"] = v c.Data["json"] = v
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }
@ -56,11 +56,16 @@ func (c *PostController) GetOne() {
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v, err := models.GetPostById(o, id) v, err := models.GetPostById(o, id)
if err != nil { if err != nil {
c.Data["json"] = err.Error() if err.Error() == "<QuerySeter> no row found" {
c.ServeJSONError("Post does not exist")
return
}
} else { } else {
c.Data["json"] = v c.Data["json"] = v
}
c.ServeJSON() c.ServeJSON()
return
}
c.ServeJSONError("Error retrieving Post")
} }
// GetAll ... // GetAll ...
@ -119,11 +124,16 @@ func (c *PostController) GetAll() {
l, err := models.GetAllPost(o, query, fields, sortby, order, offset, limit) l, err := models.GetAllPost(o, query, fields, sortby, order, offset, limit)
if err != nil { if err != nil {
c.Data["json"] = err.Error() if err.Error() == "<QuerySeter> no row found" {
c.ServeJSONError("No Posts found")
return
}
} else { } else {
c.Data["json"] = l c.Data["json"] = l
}
c.ServeJSON() c.ServeJSON()
return
}
c.ServeJSONError("Error retrieving Post")
} }
// Put ... // Put ...
@ -140,14 +150,13 @@ func (c *PostController) Put() {
v := models.Post{Id: id} v := models.Post{Id: id}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if err := models.UpdatePostById(o, &v); err == nil { if err := models.UpdatePostById(o, &v); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Updated Post")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON()
} }
// Delete ... // Delete ...
@ -161,9 +170,9 @@ func (c *PostController) Delete() {
idStr := c.Ctx.Input.Param(":id") idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
if err := models.DeletePost(o, id); err == nil { if err := models.DeletePost(o, id); err == nil {
c.Data["json"] = "OK" c.ServeJSONSuccess("Ok")
} else { } else {
c.Data["json"] = err.Error() c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSON()
} }

View File

@ -128,6 +128,7 @@ func UpdateCompanyDataById(o orm.Ormer, m *CompanyData) (err error) {
v := CompanyData{Id: m.Id} v := CompanyData{Id: m.Id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {
m.Created = v.Created
var num int64 var num int64
if num, err = o.Update(m); err == nil { if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num) fmt.Println("Number of records updated in database:", num)

View File

@ -128,6 +128,7 @@ func UpdateCompanyUserById(o orm.Ormer, m *CompanyUser) (err error) {
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {
var num int64 var num int64
m.Created = v.Created
if num, err = o.Update(m); err == nil { if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num) fmt.Println("Number of records updated in database:", num)
} }

View File

@ -132,6 +132,7 @@ func UpdateContactById(o orm.Ormer, m *Contact) (err error) {
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {
var num int64 var num int64
m.Created = v.Created
if num, err = o.Update(m); err == nil { if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num) fmt.Println("Number of records updated in database:", num)
} }

View File

@ -121,14 +121,14 @@ func GetAllPost(o orm.Ormer, query map[string]string, fields []string, sortby []
return nil, err return nil, err
} }
// UpdatePost updates Post by Id and returns error if // UpdatePost updates Post by Id and returns error if the record to be updated doesn't exist
// the record to be updated doesn't exist
func UpdatePostById(o orm.Ormer, m *Post) (err error) { func UpdatePostById(o orm.Ormer, m *Post) (err error) {
m.Modified = time.Now() m.Modified = time.Now()
v := Post{Id: m.Id} v := Post{Id: m.Id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {
var num int64 var num int64
m.Created = v.Created
if num, err = o.Update(m); err == nil { if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num) fmt.Println("Number of records updated in database:", num)
} }

View File

@ -159,6 +159,7 @@ func UpdateUserCompanyMapById(o orm.Ormer, m *UserCompanyMap) (err error) {
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {
var num int64 var num int64
m.Created = v.Created
if num, err = o.Update(m); err == nil { if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num) fmt.Println("Number of records updated in database:", num)
} }

View File

@ -657,7 +657,7 @@
"tags": [ "tags": [
"user" "user"
], ],
"description": "create CompanyUser", "description": "Create a new CompanyUser and his user company mapping",
"operationId": "CompanyUserController.Post", "operationId": "CompanyUserController.Post",
"parameters": [ "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}": { "/user/{id}": {
"get": { "get": {
"tags": [ "tags": [

View File

@ -438,7 +438,7 @@ paths:
post: post:
tags: tags:
- user - user
description: create CompanyUser description: Create a new CompanyUser and his user company mapping
operationId: CompanyUserController.Post operationId: CompanyUserController.Post
parameters: parameters:
- in: body - in: body
@ -511,6 +511,17 @@ paths:
description: '{string} delete success!' description: '{string} delete success!'
"403": "403":
description: id is empty 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: definitions:
models.Auth: models.Auth:
title: Auth title: Auth