Adding proper endpoint handeling in all endpoints

This commit is contained in:
Lukas Bachschwell 2018-11-16 19:58:52 +01:00
parent ae1db49148
commit 3e1ae821cd
8 changed files with 111 additions and 104 deletions

View File

@ -21,8 +21,8 @@ Todo till we can fork this repo
- ~~last modified in update for all tables~~ - ~~last modified in update for all tables~~
- ~~modifiedby (companyuserid) relation~~ - ~~modifiedby (companyuserid) relation~~
- ~~checking some roles in all controller endpoints~~ - ~~checking some roles in all controller endpoints~~
- return error in case of not found - ~~return error in case of not found~~
- go through all endpoints for errors - ~~go through all endpoints for errors~~
## Notes: ## Notes:

View File

@ -119,7 +119,6 @@ func (c *AuthController) Login() {
json := AuthResponse{200, tokenString, *companyUser} json := AuthResponse{200, tokenString, *companyUser}
c.Data["json"] = &json c.Data["json"] = &json
c.ServeJSON() c.ServeJSON()
} }
@ -253,7 +252,5 @@ func (c *AuthController) Register() {
json := AuthResponse{200, tokenString, companyUser} json := AuthResponse{200, tokenString, companyUser}
c.Data["json"] = &json c.Data["json"] = &json
c.ServeJSON() c.ServeJSON()
} }

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors"
"multitenantStack/constants" "multitenantStack/constants"
"multitenantStack/models" "multitenantStack/models"
"strconv" "strconv"
@ -27,24 +26,23 @@ func (c *CompanyDataController) URLMapping() {
// @Title Post // @Title Post
// @Description create CompanyData // @Description create CompanyData
// @Param body body models.CompanyData true "body for CompanyData content" // @Param body body models.CompanyData true "body for CompanyData content"
// @Success 201 {int} models.CompanyData // @Success 200 {int} models.CompanyData
// @Failure 403 body is empty // @Failure 403 body is empty
// @router / [post] // @router / [post]
func (c *CompanyDataController) Post() { func (c *CompanyDataController) Post() {
var v models.CompanyData var v models.CompanyData
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.AddCompanyData(o, &v); err == nil { if _, err := models.AddCompanyData(o, &v); err == nil {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(200)
c.Data["json"] = v c.Data["json"] = v
} else { c.ServeJSON()
c.ServeJSONErrorWithError("Error", err)
return return
} }
} else { c.ServeJSONErrorWithError("Error creating Post", err)
c.ServeJSONErrorWithError("Error", err)
return return
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error bad format", err)
return
} }
// GetOne ... // GetOne ...
@ -59,12 +57,16 @@ 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.ServeJSONErrorWithError("Error", err) if err.Error() == "<QuerySeter> no row found" {
c.ServeJSONError("Company Data does not exist")
return
}
c.ServeJSONErrorWithError("Error getting Company Data", err)
return return
} else {
c.Data["json"] = v
} }
c.Data["json"] = v
c.ServeJSON() c.ServeJSON()
return
} }
// GetAll ... // GetAll ...
@ -112,8 +114,7 @@ func (c *CompanyDataController) GetAll() {
for _, cond := range strings.Split(v, ",") { for _, cond := range strings.Split(v, ",") {
kv := strings.SplitN(cond, ":", 2) kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 { if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair") c.ServeJSONError("Error: invalid query key/value pair")
c.ServeJSON()
return return
} }
k, v := kv[0], kv[1] k, v := kv[0], kv[1]
@ -123,12 +124,12 @@ 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.ServeJSONErrorWithError("Error", err) c.ServeJSONErrorWithError("Error getting company data", err)
return return
} else {
c.Data["json"] = l
} }
c.Data["json"] = l
c.ServeJSON() c.ServeJSON()
return
} }
// Put ... // Put ...
@ -145,22 +146,21 @@ func (c *CompanyDataController) Put() {
if currentUser.Role != constants.RoleAdmin { if currentUser.Role != constants.RoleAdmin {
c.ServeJSONError("Only Admins can edit company Data") c.ServeJSONError("Only Admins can edit company Data")
return
} }
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 {
v.ModifiedBy = int64(currentUser.Id) v.ModifiedBy = int64(currentUser.Id)
if err := models.UpdateCompanyDataById(o, &v); err == nil { if err := models.UpdateCompanyDataById(o, &v); err == nil {
c.ServeJSONSuccess("Ok") c.ServeJSONSuccess("Updated CompanyData")
return
} else {
c.ServeJSONErrorWithError("Error", err)
return return
} }
} else { c.ServeJSONErrorWithError("Error updating company data", err)
c.ServeJSONErrorWithError("Error", err)
return return
} }
c.ServeJSONErrorWithError("Error bad format", err)
return
c.ServeJSON() c.ServeJSON()
} }
@ -177,9 +177,7 @@ func (c *CompanyDataController) Delete() {
if err := models.DeleteCompanyData(o, id); err == nil { if err := models.DeleteCompanyData(o, id); err == nil {
c.ServeJSONSuccess("Ok") c.ServeJSONSuccess("Ok")
return return
} else {
c.ServeJSONErrorWithError("Error", err)
return
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error on deleting Company Data", err)
return
} }

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"multitenantStack/constants" "multitenantStack/constants"
"multitenantStack/models" "multitenantStack/models"
@ -88,10 +87,10 @@ func (c *CompanyUserController) Post() {
if err == nil { if err == nil {
c.ServeJSONSuccess("Success") c.ServeJSONSuccess("Success")
return return
} else {
c.ServeJSONErrorWithError("Error on saving user", err)
return
} }
c.ServeJSONErrorWithError("Error on saving user", err)
return
} }
// GetOne ... // GetOne ...
@ -106,10 +105,15 @@ 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.ServeJSONErrorWithError("Error", err) if err.Error() == "<QuerySeter> no row found" {
} else { c.ServeJSONError("company user does not exist")
c.Data["json"] = v return
}
c.ServeJSONErrorWithError("Error getting company user", err)
return
} }
c.Data["json"] = v
c.ServeJSON() c.ServeJSON()
} }
@ -158,8 +162,7 @@ func (c *CompanyUserController) GetAll() {
for _, cond := range strings.Split(v, ",") { for _, cond := range strings.Split(v, ",") {
kv := strings.SplitN(cond, ":", 2) kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 { if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair") c.ServeJSONError("Error: invalid query key/value pair")
c.ServeJSON()
return return
} }
k, v := kv[0], kv[1] k, v := kv[0], kv[1]
@ -169,10 +172,10 @@ 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.ServeJSONErrorWithError("Error", err) c.ServeJSONErrorWithError("Error getting company users", err)
} else { return
c.Data["json"] = l
} }
c.Data["json"] = l
c.ServeJSON() c.ServeJSON()
} }
@ -190,20 +193,22 @@ func (c *CompanyUserController) Put() {
if currentUser.Role != constants.RoleAdmin && id != currentUser.Id { if currentUser.Role != constants.RoleAdmin && id != currentUser.Id {
c.ServeJSONError("You can only edit your own userdata!") c.ServeJSONError("You can only edit your own userdata!")
return
} }
v := models.CompanyUser{Id: id} v := models.CompanyUser{Id: id}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { err := json.Unmarshal(c.Ctx.Input.RequestBody, &v)
if err == nil {
v.ModifiedBy = int64(currentUser.Id) v.ModifiedBy = int64(currentUser.Id)
if err := models.UpdateCompanyUserById(o, &v); err == nil { if err := models.UpdateCompanyUserById(o, &v); err == nil {
c.ServeJSONSuccess("Ok") c.ServeJSONSuccess("Ok")
} else { return
c.ServeJSONErrorWithError("Error", err)
} }
} else { c.ServeJSONErrorWithError("Error updating company users", err)
c.ServeJSONErrorWithError("Error", err) return
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error bad format", err)
return
} }
// Delete ... // Delete ...
@ -219,12 +224,12 @@ func (c *CompanyUserController) Delete() {
if currentUser.Role != constants.RoleAdmin && currentUser.Id != id { if currentUser.Role != constants.RoleAdmin && currentUser.Id != id {
c.ServeJSONError("You can not delete users other than yourself!") c.ServeJSONError("You can not delete users other than yourself!")
c.ServeJSON() return
} }
if currentUser.Role == constants.RoleOwner { if currentUser.Role == constants.RoleOwner {
c.ServeJSONError("You can not delete users other than yourself!") c.ServeJSONError("You can not delete users other than yourself!")
c.ServeJSON() return
} }
uExists, err := models.GetCompanyUserById(o, id) uExists, err := models.GetCompanyUserById(o, id)
@ -236,6 +241,7 @@ func (c *CompanyUserController) Delete() {
err = models.DeleteCompanyUser(o, id) err = models.DeleteCompanyUser(o, id)
if err != nil { if err != nil {
c.ServeJSONError("Failed to delete company User") c.ServeJSONError("Failed to delete company User")
return
} }
systemDB := companydb.GetSystemDatabase() systemDB := companydb.GetSystemDatabase()
@ -252,9 +258,9 @@ func (c *CompanyUserController) Delete() {
if err != nil { if err != nil {
c.ServeJSONError("Error deleting User Company Relation") c.ServeJSONError("Error deleting User Company Relation")
return return
} else {
c.ServeJSONSuccess("Successfully deleted!")
} }
c.ServeJSONSuccess("Successfully deleted!")
return
} }
// DeleteCompany ... // DeleteCompany ...
@ -296,9 +302,9 @@ func (c *CompanyUserController) DeleteCompany() {
systemO.Rollback() systemO.Rollback()
c.ServeJSONError("Error deleting Company Database") c.ServeJSONError("Error deleting Company Database")
return return
} else {
systemO.Commit()
c.ServeJSONSuccess("Successfully deleted!")
} }
systemO.Commit()
c.ServeJSONSuccess("Successfully deleted!")
return
} }

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors"
"multitenantStack/constants" "multitenantStack/constants"
"multitenantStack/models" "multitenantStack/models"
"strconv" "strconv"
@ -32,17 +31,19 @@ func (c *ContactController) URLMapping() {
// @router / [post] // @router / [post]
func (c *ContactController) Post() { func (c *ContactController) Post() {
var v models.Contact var v models.Contact
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil { err := json.Unmarshal(c.Ctx.Input.RequestBody, &v)
if err == nil {
if _, err := models.AddContact(o, &v); err == nil { if _, err := models.AddContact(o, &v); err == nil {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(201)
c.Data["json"] = v c.Data["json"] = v
} else { c.ServeJSON()
c.ServeJSONErrorWithError("Error", err) return
} }
} else { c.ServeJSONErrorWithError("Error creating contact", err)
c.ServeJSONErrorWithError("Error", err) return
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error bad format", err)
return
} }
// GetOne ... // GetOne ...
@ -57,10 +58,14 @@ func (c *ContactController) GetOne() {
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v, err := models.GetContactById(o, id) v, err := models.GetContactById(o, id)
if err != nil { if err != nil {
c.ServeJSONErrorWithError("Error", err) if err.Error() == "<QuerySeter> no row found" {
} else { c.ServeJSONError("Contact does not exist")
c.Data["json"] = v return
}
c.ServeJSONErrorWithError("Error getting contact", err)
return
} }
c.Data["json"] = v
c.ServeJSON() c.ServeJSON()
} }
@ -109,8 +114,7 @@ func (c *ContactController) GetAll() {
for _, cond := range strings.Split(v, ",") { for _, cond := range strings.Split(v, ",") {
kv := strings.SplitN(cond, ":", 2) kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 { if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair") c.ServeJSONError("Error: invalid query key/value pair")
c.ServeJSON()
return return
} }
k, v := kv[0], kv[1] k, v := kv[0], kv[1]
@ -120,12 +124,12 @@ 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.ServeJSONErrorWithError("Error", err) c.ServeJSONErrorWithError("Error getting contacts", err)
} else { return
c.Data["json"] = l
} }
c.Data["json"] = l
c.ServeJSON() c.ServeJSON()
return
} }
// Put ... // Put ...
@ -161,15 +165,12 @@ func (c *ContactController) Put() {
if err := models.UpdateContactById(o, &v); err == nil { if err := models.UpdateContactById(o, &v); err == nil {
c.ServeJSONSuccess("Ok") c.ServeJSONSuccess("Ok")
return return
} else {
c.ServeJSONErrorWithError("Error", err)
return
} }
} else {
c.ServeJSONErrorWithError("Error", err) c.ServeJSONErrorWithError("Error", err)
return return
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error", err)
return
} }
// Delete ... // Delete ...
@ -182,12 +183,12 @@ func (c *ContactController) Put() {
func (c *ContactController) Delete() { 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 { err := models.DeleteContact(o, id)
if err == nil {
c.ServeJSONSuccess("Ok") c.ServeJSONSuccess("Ok")
return return
} else {
c.ServeJSONErrorWithError("Error", err)
return
} }
c.ServeJSON()
c.ServeJSONErrorWithError("Error deleting contact", err)
return
} }

View File

@ -8,9 +8,11 @@ type ErrorController struct {
// Error404 handle a 404 // Error404 handle a 404
func (c *ErrorController) Error404() { func (c *ErrorController) Error404() {
c.ServeJSONErrorWithCode(404, "Not Found") c.ServeJSONErrorWithCode(404, "Not Found")
return
} }
// Error500 handle a 500 // Error500 handle a 500
func (c *ErrorController) Error500() { func (c *ErrorController) Error500() {
c.ServeJSONErrorWithCode(500, "Internal Server Error") c.ServeJSONErrorWithCode(500, "Internal Server Error")
return
} }

View File

@ -8,9 +8,11 @@ type IndexController struct {
// Get Index response for get // Get Index response for get
func (c *IndexController) Get() { func (c *IndexController) Get() {
c.ServeJSONSuccess("multitenant API") c.ServeJSONSuccess("multitenant API")
return
} }
// Post Index response for post // Post Index response for post
func (c *IndexController) Post() { func (c *IndexController) Post() {
c.ServeJSONSuccess("multitenant API") c.ServeJSONSuccess("multitenant API")
return
} }

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors"
"multitenantStack/constants" "multitenantStack/constants"
"multitenantStack/models" "multitenantStack/models"
"strconv" "strconv"
@ -36,13 +35,14 @@ func (c *PostController) Post() {
if _, err := models.AddPost(o, &v); err == nil { if _, err := models.AddPost(o, &v); err == nil {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(201)
c.Data["json"] = v c.Data["json"] = v
} else { c.ServeJSON()
c.ServeJSONErrorWithError("Error", err) return
} }
} else { c.ServeJSONErrorWithError("Error creating post", err)
c.ServeJSONErrorWithError("Error", err) return
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error bad format", err)
return
} }
// GetOne ... // GetOne ...
@ -67,6 +67,7 @@ func (c *PostController) GetOne() {
return return
} }
c.ServeJSONError("Error retrieving Post") c.ServeJSONError("Error retrieving Post")
return
} }
// GetAll ... // GetAll ...
@ -114,8 +115,7 @@ func (c *PostController) GetAll() {
for _, cond := range strings.Split(v, ",") { for _, cond := range strings.Split(v, ",") {
kv := strings.SplitN(cond, ":", 2) kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 { if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair") c.ServeJSONError("Error: invalid query key/value pair")
c.ServeJSON()
return return
} }
k, v := kv[0], kv[1] k, v := kv[0], kv[1]
@ -125,16 +125,14 @@ 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 {
if err.Error() == "<QuerySeter> no row found" { c.ServeJSONError("Error getting posts")
c.ServeJSONError("No Posts found")
return
}
} else {
c.Data["json"] = l
c.ServeJSON()
return return
} }
c.Data["json"] = l
c.ServeJSON()
return
c.ServeJSONError("Error retrieving Post") c.ServeJSONError("Error retrieving Post")
return
} }
// Put ... // Put ...
@ -156,10 +154,12 @@ func (c *PostController) Put() {
return return
} }
c.ServeJSONError("Error updating Post") c.ServeJSONError("Error updating Post")
return
} }
if currentUser.Role != constants.RoleAdmin && p.ModifiedBy != int64(currentUser.Id) { if currentUser.Role != constants.RoleAdmin && p.ModifiedBy != int64(currentUser.Id) {
c.ServeJSONError("You can only edit your own posts!") c.ServeJSONError("You can only edit your own posts!")
return
} }
v := models.Post{Id: id} v := models.Post{Id: id}
@ -167,12 +167,13 @@ func (c *PostController) Put() {
v.ModifiedBy = int64(currentUser.Id) v.ModifiedBy = int64(currentUser.Id)
if err := models.UpdatePostById(o, &v); err == nil { if err := models.UpdatePostById(o, &v); err == nil {
c.ServeJSONSuccess("Updated Post") c.ServeJSONSuccess("Updated Post")
} else { return
c.ServeJSONErrorWithError("Error", err)
} }
} else { c.ServeJSONErrorWithError("Error updating post", err)
c.ServeJSONErrorWithError("Error", err) return
} }
c.ServeJSONErrorWithError("Error bad format", err)
return
} }
// Delete ... // Delete ...
@ -187,8 +188,8 @@ func (c *PostController) Delete() {
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.ServeJSONSuccess("Ok") c.ServeJSONSuccess("Ok")
} else { return
c.ServeJSONErrorWithError("Error", err)
} }
c.ServeJSON() c.ServeJSONErrorWithError("Error deleting post", err)
return
} }