186 lines
4.9 KiB
Go
186 lines
4.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"multitenantStack/constants"
|
|
"multitenantStack/models"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// CompanyDataController operations for CompanyData
|
|
type CompanyDataController struct {
|
|
BaseAPIController
|
|
}
|
|
|
|
// URLMapping ...
|
|
func (c *CompanyDataController) URLMapping() {
|
|
c.Mapping("Post", c.Post)
|
|
c.Mapping("GetOne", c.GetOne)
|
|
c.Mapping("GetAll", c.GetAll)
|
|
c.Mapping("Put", c.Put)
|
|
c.Mapping("Delete", c.Delete)
|
|
}
|
|
|
|
// Post ...
|
|
// @Title Post
|
|
// @Description create CompanyData
|
|
// @Param body body models.CompanyData true "body for CompanyData content"
|
|
// @Success 200 {int} models.CompanyData
|
|
// @Failure 403 body is empty
|
|
// @router / [post]
|
|
func (c *CompanyDataController) Post() {
|
|
var v models.CompanyData
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &v)
|
|
if err == nil {
|
|
if _, err := models.AddCompanyData(o, &v); err == nil {
|
|
c.Ctx.Output.SetStatus(200)
|
|
c.Data["json"] = v
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
c.ServeJSONErrorWithError("Error creating Post", err)
|
|
return
|
|
}
|
|
c.ServeJSONErrorWithError("Error bad format", err)
|
|
return
|
|
}
|
|
|
|
// GetOne ...
|
|
// @Title Get One
|
|
// @Description get CompanyData by id
|
|
// @Param id path string true "The key for staticblock"
|
|
// @Success 200 {object} models.CompanyData
|
|
// @Failure 403 :id is empty
|
|
// @router /:id [get]
|
|
func (c *CompanyDataController) GetOne() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, _ := strconv.Atoi(idStr)
|
|
v, err := models.GetCompanyDataById(o, id)
|
|
if err != nil {
|
|
if err.Error() == "<QuerySeter> no row found" {
|
|
c.ServeJSONError("Company Data does not exist")
|
|
return
|
|
}
|
|
c.ServeJSONErrorWithError("Error getting Company Data", err)
|
|
return
|
|
}
|
|
c.Data["json"] = v
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// GetAll ...
|
|
// @Title Get All
|
|
// @Description get CompanyData
|
|
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
|
|
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
|
|
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
|
|
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
|
|
// @Param limit query string false "Limit the size of result set. Must be an integer"
|
|
// @Param offset query string false "Start position of result set. Must be an integer"
|
|
// @Success 200 {object} models.CompanyData
|
|
// @Failure 403
|
|
// @router / [get]
|
|
func (c *CompanyDataController) GetAll() {
|
|
var fields []string
|
|
var sortby []string
|
|
var order []string
|
|
var query = make(map[string]string)
|
|
var limit int64 = 10
|
|
var offset int64
|
|
|
|
// fields: col1,col2,entity.col3
|
|
if v := c.GetString("fields"); v != "" {
|
|
fields = strings.Split(v, ",")
|
|
}
|
|
// limit: 10 (default is 10)
|
|
if v, err := c.GetInt64("limit"); err == nil {
|
|
limit = v
|
|
}
|
|
// offset: 0 (default is 0)
|
|
if v, err := c.GetInt64("offset"); err == nil {
|
|
offset = v
|
|
}
|
|
// sortby: col1,col2
|
|
if v := c.GetString("sortby"); v != "" {
|
|
sortby = strings.Split(v, ",")
|
|
}
|
|
// order: desc,asc
|
|
if v := c.GetString("order"); v != "" {
|
|
order = strings.Split(v, ",")
|
|
}
|
|
// query: k:v,k:v
|
|
if v := c.GetString("query"); v != "" {
|
|
for _, cond := range strings.Split(v, ",") {
|
|
kv := strings.SplitN(cond, ":", 2)
|
|
if len(kv) != 2 {
|
|
c.ServeJSONError("Error: invalid query key/value pair")
|
|
return
|
|
}
|
|
k, v := kv[0], kv[1]
|
|
query[k] = v
|
|
}
|
|
}
|
|
|
|
l, err := models.GetAllCompanyData(o, query, fields, sortby, order, offset, limit)
|
|
if err != nil {
|
|
c.ServeJSONErrorWithError("Error getting company data", err)
|
|
return
|
|
}
|
|
c.Data["json"] = l
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// Put ...
|
|
// @Title Put
|
|
// @Description update the CompanyData
|
|
// @Param id path string true "The id you want to update"
|
|
// @Param body body models.CompanyData true "body for CompanyData content"
|
|
// @Success 200 {object} models.CompanyData
|
|
// @Failure 403 :id is not int
|
|
// @router /:id [put]
|
|
func (c *CompanyDataController) Put() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, _ := strconv.Atoi(idStr)
|
|
|
|
if currentUser.Role != constants.RoleAdmin {
|
|
c.ServeJSONError("Only Admins can edit company Data")
|
|
return
|
|
}
|
|
|
|
v := models.CompanyData{Id: id}
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &v)
|
|
if err == nil {
|
|
v.ModifiedBy = int64(currentUser.Id)
|
|
if err := models.UpdateCompanyDataById(o, &v); err == nil {
|
|
c.ServeJSONSuccess("Updated CompanyData")
|
|
return
|
|
}
|
|
c.ServeJSONErrorWithError("Error updating company data", err)
|
|
return
|
|
}
|
|
c.ServeJSONErrorWithError("Error bad format", err)
|
|
return
|
|
}
|
|
|
|
// Delete ...
|
|
// @Title Delete
|
|
// @Description delete the CompanyData
|
|
// @Param id path string true "The id you want to delete"
|
|
// @Success 200 {string} delete success!
|
|
// @Failure 403 id is empty
|
|
// @router /:id [delete]
|
|
func (c *CompanyDataController) Delete() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, _ := strconv.Atoi(idStr)
|
|
err := models.DeleteCompanyData(o, id)
|
|
if err == nil {
|
|
c.ServeJSONSuccess("Ok")
|
|
return
|
|
}
|
|
c.ServeJSONErrorWithError("Error on deleting Company Data", err)
|
|
return
|
|
}
|