initial
This commit is contained in:
171
controllers/company_data.go
Normal file
171
controllers/company_data.go
Normal file
@ -0,0 +1,171 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"multitenantStack/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// CompanyDataController operations for CompanyData
|
||||
type CompanyDataController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// 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 201 {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(&v); err == nil {
|
||||
c.Ctx.Output.SetStatus(201)
|
||||
c.Data["json"] = v
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// 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(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = v
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// 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.Data["json"] = errors.New("Error: invalid query key/value pair")
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
k, v := kv[0], kv[1]
|
||||
query[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
l, err := models.GetAllCompanyData(query, fields, sortby, order, offset, limit)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = l
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// 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)
|
||||
v := models.CompanyData{Id: id}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if err := models.UpdateCompanyDataById(&v); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err := models.DeleteCompanyData(id); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
171
controllers/company_user.go
Normal file
171
controllers/company_user.go
Normal file
@ -0,0 +1,171 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"multitenantStack/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// CompanyUserController operations for CompanyUser
|
||||
type CompanyUserController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// URLMapping ...
|
||||
func (c *CompanyUserController) 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 CompanyUser
|
||||
// @Param body body models.CompanyUser true "body for CompanyUser content"
|
||||
// @Success 201 {int} models.CompanyUser
|
||||
// @Failure 403 body is empty
|
||||
// @router / [post]
|
||||
func (c *CompanyUserController) Post() {
|
||||
var v models.CompanyUser
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if _, err := models.AddCompanyUser(&v); err == nil {
|
||||
c.Ctx.Output.SetStatus(201)
|
||||
c.Data["json"] = v
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetOne ...
|
||||
// @Title Get One
|
||||
// @Description get CompanyUser by id
|
||||
// @Param id path string true "The key for staticblock"
|
||||
// @Success 200 {object} models.CompanyUser
|
||||
// @Failure 403 :id is empty
|
||||
// @router /:id [get]
|
||||
func (c *CompanyUserController) GetOne() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
v, err := models.GetCompanyUserById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = v
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetAll ...
|
||||
// @Title Get All
|
||||
// @Description get CompanyUser
|
||||
// @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.CompanyUser
|
||||
// @Failure 403
|
||||
// @router / [get]
|
||||
func (c *CompanyUserController) 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.Data["json"] = errors.New("Error: invalid query key/value pair")
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
k, v := kv[0], kv[1]
|
||||
query[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
l, err := models.GetAllCompanyUser(query, fields, sortby, order, offset, limit)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = l
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Put ...
|
||||
// @Title Put
|
||||
// @Description update the CompanyUser
|
||||
// @Param id path string true "The id you want to update"
|
||||
// @Param body body models.CompanyUser true "body for CompanyUser content"
|
||||
// @Success 200 {object} models.CompanyUser
|
||||
// @Failure 403 :id is not int
|
||||
// @router /:id [put]
|
||||
func (c *CompanyUserController) Put() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
v := models.CompanyUser{Id: id}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if err := models.UpdateCompanyUserById(&v); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Delete ...
|
||||
// @Title Delete
|
||||
// @Description delete the CompanyUser
|
||||
// @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 *CompanyUserController) Delete() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
if err := models.DeleteCompanyUser(id); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
171
controllers/contact.go
Normal file
171
controllers/contact.go
Normal file
@ -0,0 +1,171 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"multitenantStack/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// ContactController operations for Contact
|
||||
type ContactController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// URLMapping ...
|
||||
func (c *ContactController) 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 Contact
|
||||
// @Param body body models.Contact true "body for Contact content"
|
||||
// @Success 201 {int} models.Contact
|
||||
// @Failure 403 body is empty
|
||||
// @router / [post]
|
||||
func (c *ContactController) Post() {
|
||||
var v models.Contact
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if _, err := models.AddContact(&v); err == nil {
|
||||
c.Ctx.Output.SetStatus(201)
|
||||
c.Data["json"] = v
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetOne ...
|
||||
// @Title Get One
|
||||
// @Description get Contact by id
|
||||
// @Param id path string true "The key for staticblock"
|
||||
// @Success 200 {object} models.Contact
|
||||
// @Failure 403 :id is empty
|
||||
// @router /:id [get]
|
||||
func (c *ContactController) GetOne() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
v, err := models.GetContactById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = v
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetAll ...
|
||||
// @Title Get All
|
||||
// @Description get Contact
|
||||
// @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.Contact
|
||||
// @Failure 403
|
||||
// @router / [get]
|
||||
func (c *ContactController) 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.Data["json"] = errors.New("Error: invalid query key/value pair")
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
k, v := kv[0], kv[1]
|
||||
query[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
l, err := models.GetAllContact(query, fields, sortby, order, offset, limit)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = l
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Put ...
|
||||
// @Title Put
|
||||
// @Description update the Contact
|
||||
// @Param id path string true "The id you want to update"
|
||||
// @Param body body models.Contact true "body for Contact content"
|
||||
// @Success 200 {object} models.Contact
|
||||
// @Failure 403 :id is not int
|
||||
// @router /:id [put]
|
||||
func (c *ContactController) Put() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
v := models.Contact{Id: id}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if err := models.UpdateContactById(&v); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Delete ...
|
||||
// @Title Delete
|
||||
// @Description delete the Contact
|
||||
// @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 *ContactController) Delete() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
if err := models.DeleteContact(id); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
171
controllers/post.go
Normal file
171
controllers/post.go
Normal file
@ -0,0 +1,171 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"multitenantStack/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// PostController operations for Post
|
||||
type PostController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// URLMapping ...
|
||||
func (c *PostController) 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 Post
|
||||
// @Param body body models.Post true "body for Post content"
|
||||
// @Success 201 {int} models.Post
|
||||
// @Failure 403 body is empty
|
||||
// @router / [post]
|
||||
func (c *PostController) Post() {
|
||||
var v models.Post
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if _, err := models.AddPost(&v); err == nil {
|
||||
c.Ctx.Output.SetStatus(201)
|
||||
c.Data["json"] = v
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetOne ...
|
||||
// @Title Get One
|
||||
// @Description get Post by id
|
||||
// @Param id path string true "The key for staticblock"
|
||||
// @Success 200 {object} models.Post
|
||||
// @Failure 403 :id is empty
|
||||
// @router /:id [get]
|
||||
func (c *PostController) GetOne() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
v, err := models.GetPostById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = v
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetAll ...
|
||||
// @Title Get All
|
||||
// @Description get Post
|
||||
// @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.Post
|
||||
// @Failure 403
|
||||
// @router / [get]
|
||||
func (c *PostController) 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.Data["json"] = errors.New("Error: invalid query key/value pair")
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
k, v := kv[0], kv[1]
|
||||
query[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
l, err := models.GetAllPost(query, fields, sortby, order, offset, limit)
|
||||
if err != nil {
|
||||
c.Data["json"] = err.Error()
|
||||
} else {
|
||||
c.Data["json"] = l
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Put ...
|
||||
// @Title Put
|
||||
// @Description update the Post
|
||||
// @Param id path string true "The id you want to update"
|
||||
// @Param body body models.Post true "body for Post content"
|
||||
// @Success 200 {object} models.Post
|
||||
// @Failure 403 :id is not int
|
||||
// @router /:id [put]
|
||||
func (c *PostController) Put() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
v := models.Post{Id: id}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
|
||||
if err := models.UpdatePostById(&v); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Delete ...
|
||||
// @Title Delete
|
||||
// @Description delete the Post
|
||||
// @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 *PostController) Delete() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
if err := models.DeletePost(id); err == nil {
|
||||
c.Data["json"] = "OK"
|
||||
} else {
|
||||
c.Data["json"] = err.Error()
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
Reference in New Issue
Block a user