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~~
- ~~modifiedby (companyuserid) relation~~
- ~~checking some roles in all controller endpoints~~
- return error in case of not found
- go through all endpoints for errors
- ~~return error in case of not found~~
- ~~go through all endpoints for errors~~
## Notes:

View File

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

View File

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

View File

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

View File

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

View File

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