Protecting Created Date, updating docs, changin errors to JSON

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

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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() == "<QuerySeter> 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() == "<QuerySeter> 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()
}