group_controller.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package controllers
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/datamodels"
  5. "xps/service"
  6. )
  7. type GroupController struct {
  8. Base
  9. Service service.GroupService
  10. }
  11. func (c *GroupController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET" ,"/" ,"GetList" )
  13. b.Handle("GET" ,"/groupId:int64}" ,"GetById" )
  14. b.Handle("POST" ,"/add" ,"Create" )
  15. b.Handle("PUT" ,"/update" ,"Update" )
  16. b.Handle("DELETE" ,"/{groupId:int64}" ,"DeleteById" )
  17. }
  18. func (c *GroupController) GetList() *JsonResult {
  19. ocId := c.GetCurOcId()
  20. params := c.buildParams()
  21. params["ocId"] = ocId
  22. data, err := c.Service.GetList(params)
  23. if err != nil {
  24. return ResultErr(err.Error(), nil)
  25. } else {
  26. return ResultOk("获取成功", data)
  27. }
  28. }
  29. func (c *GroupController) GetById(groupId int64) *JsonResult {
  30. ocId := c.GetCurOcId()
  31. data, err := c.Service.GetById(ocId, groupId)
  32. if err != nil {
  33. return ResultErr(err.Error(), nil)
  34. } else {
  35. return ResultOk("获取成功", data)
  36. }
  37. }
  38. func (c *GroupController) Create() *JsonResult {
  39. ocId := c.GetCurOcId()
  40. userId := c.GetCurUserId()
  41. group := datamodels.Group{}
  42. if err := c.Ctx.ReadJSON(&group); err != nil {
  43. return ResultErr("读取数据失败", nil)
  44. }
  45. group.OcId = ocId
  46. group.CreatedBy = userId
  47. if err := c.Service.Create(&group); err != nil {
  48. return ResultErr(err.Error(), nil)
  49. } else {
  50. return ResultOk("新增成功", nil)
  51. }
  52. }
  53. func (c *GroupController) Update() *JsonResult {
  54. ocId := c.GetCurOcId()
  55. userId := c.GetCurUserId()
  56. group := datamodels.Group{}
  57. if err := c.Ctx.ReadJSON(&group); err != nil {
  58. return ResultErr("读取数据失败", nil)
  59. }
  60. group.OcId = ocId
  61. group.UpdatedBy = userId
  62. if err := c.Service.Update(&group); err != nil {
  63. return ResultErr(err.Error(), nil)
  64. } else {
  65. return ResultOk("更新成功", nil)
  66. }
  67. }
  68. func (c *GroupController) DeleteById(groupId int64) *JsonResult {
  69. ocId := c.GetCurOcId()
  70. userId := c.GetCurUserId()
  71. m := make(map[string]interface{})
  72. m["ocId"] = ocId
  73. m["groupId"] = groupId
  74. m["deletedBy"] = userId
  75. if err := c.Service.Delete(m); err != nil {
  76. return ResultErr(err.Error(), nil)
  77. } else {
  78. return ResultOk("删除成功", nil)
  79. }
  80. }