app_controller.go 2.3 KB

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