region.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package admin
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "server/dao"
  5. "server/model/common/response"
  6. "server/utils/logger"
  7. "strconv"
  8. )
  9. type RegionApi struct{}
  10. func (ra *RegionApi) QueryAllRegions(c *gin.Context) {
  11. regions, err := regionService.QueryAllRegions()
  12. if err != nil {
  13. logger.Get().Error("QueryAllRegions ---- " + err.Error())
  14. response.FailWithMessage("获取地区失败", c)
  15. return
  16. }
  17. response.OkWithData(regions, c)
  18. }
  19. func (ra *RegionApi) CreateRegion(c *gin.Context) {
  20. var region dao.Region
  21. if err := c.ShouldBindJSON(&region); err != nil {
  22. logger.Get().Error("CreateRegions ---- " + err.Error())
  23. response.FailWithMessage("参数解析失败", c)
  24. return
  25. }
  26. err := regionService.CreateRegion(region)
  27. if err != nil {
  28. logger.Get().Error("CreateRegions ---- " + err.Error())
  29. response.FailWithMessage("创建地区失败", c)
  30. return
  31. }
  32. response.OkWithMessage("创建地区成功", c)
  33. }
  34. func (ra *RegionApi) UpdateRegion(c *gin.Context) {
  35. var region dao.Region
  36. if err := c.ShouldBindJSON(&region); err != nil {
  37. logger.Get().Error("UpdateRegions ---- " + err.Error())
  38. response.FailWithMessage("参数解析失败", c)
  39. return
  40. }
  41. err := regionService.UpdateRegion(region)
  42. if err != nil {
  43. logger.Get().Error("UpdateRegions ---- " + err.Error())
  44. response.FailWithMessage("更新地区失败", c)
  45. return
  46. }
  47. response.OkWithMessage("更新地区成功", c)
  48. }
  49. func (ra *RegionApi) DeleteRegion(c *gin.Context) {
  50. id, err := strconv.Atoi(c.Query("id"))
  51. if err != nil {
  52. logger.Get().Error("DeleteRegions ---- " + err.Error())
  53. response.FailWithMessage("参数错误", c)
  54. return
  55. }
  56. err = regionService.DeleteRegion(id)
  57. if err != nil {
  58. logger.Get().Error("DeleteRegions ---- " + err.Error())
  59. response.FailWithMessage("删除地区失败", c)
  60. return
  61. }
  62. response.OkWithMessage("删除地区成功", c)
  63. }