setting.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package admin
  2. import (
  3. "errors"
  4. "net/http"
  5. "strconv"
  6. "github.com/gin-gonic/gin"
  7. "github.com/metarare/metarare_api/common"
  8. "github.com/metarare/metarare_api/helpers/gauth"
  9. "github.com/metarare/metarare_api/helpers/gerror"
  10. "github.com/metarare/metarare_api/models"
  11. "gorm.io/gorm"
  12. )
  13. type AdminSettingV1Router struct {
  14. group *gin.RouterGroup
  15. mDB *gorm.DB
  16. rDB *gorm.DB
  17. }
  18. func NewAdminSettingV1Router(r common.Router, basePath string) AdminSettingV1Router {
  19. s := AdminSettingV1Router{
  20. group: r.Version.Group(basePath),
  21. mDB: r.Db.MasterDB,
  22. rDB: r.Db.ReadDB,
  23. }
  24. s.group.GET("", s.getSettingList)
  25. s.group.PATCH(":type", s.updateSetting)
  26. return s
  27. }
  28. // getSettingList godoc
  29. // @Summary get setting list
  30. // @Description 현재 셋팅 값 가져오기
  31. // @Schemes
  32. // @security ApiKeyAuth
  33. // @Tags admin
  34. // @Accept json
  35. // @Produce json
  36. // @Success 200 {object} models.Setting
  37. // @Router /admin/setting [get]
  38. func (s AdminSettingV1Router) getSettingList(c *gin.Context) {
  39. admin, err := gauth.ConfirmAdminInfo(c, s.rDB)
  40. if err != nil || admin.ID == 0 {
  41. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  42. return
  43. }
  44. if admin.AdminPermission.System == 0 {
  45. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  46. return
  47. }
  48. response := models.Setting{}
  49. if err := s.rDB.Find(&response).Error; err != nil {
  50. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err)
  51. return
  52. }
  53. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil)
  54. return
  55. }
  56. // updateSetting godoc
  57. // @Summary update setting value
  58. // @Description 설정 값 변경
  59. // @Schemes
  60. // @security ApiKeyAuth
  61. // @Tags admin
  62. // @Accept json
  63. // @Produce json
  64. // @Param SettingData body SettingData true "selected target data"
  65. // @Param type path string true "type: commission, special_collection, gas_deposit"
  66. // @Success 200 {string} OK
  67. // @Router /admin/setting/{type} [patch]
  68. func (s AdminSettingV1Router) updateSetting(c *gin.Context) {
  69. admin, err := gauth.ConfirmAdminInfo(c, s.rDB)
  70. if err != nil || admin.ID == 0 {
  71. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  72. return
  73. }
  74. if admin.AdminPermission.System == 0 {
  75. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  76. return
  77. }
  78. _type := c.Param("type")
  79. if _type == "" {
  80. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, errors.New("invalid parameter value"))
  81. return
  82. }
  83. request := SettingData{}
  84. if err := c.ShouldBindJSON(&request); err != nil {
  85. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  86. return
  87. }
  88. setting := models.Setting{}
  89. if err := s.rDB.Find(&setting).Error; err != nil {
  90. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err)
  91. return
  92. }
  93. tx := s.mDB.Begin()
  94. defer common.DBTransaction(tx)
  95. //SECTION admin log
  96. log := Log{
  97. DB: tx,
  98. ActionType: "system",
  99. SubActionType: "modified",
  100. Admin: admin,
  101. From: "",
  102. To: "",
  103. Target: _type,
  104. }
  105. if _type == "special_collection" {
  106. log.From = setting.SpecialCollection
  107. log.To = request.SpecialCollection
  108. setting.SpecialCollection = request.SpecialCollection
  109. } else if _type == "commission" {
  110. log.From = strconv.FormatFloat(setting.Commission, 'f', -1, 64)
  111. log.To = strconv.FormatFloat(request.Commission, 'f', -1, 64)
  112. setting.Commission = request.Commission
  113. } else if _type == "gas_deposit" {
  114. log.From = strconv.FormatFloat(setting.GasDeposit, 'f', -1, 64)
  115. log.To = strconv.FormatFloat(request.GasDeposit, 'f', -1, 64)
  116. setting.GasDeposit = request.GasDeposit
  117. }
  118. if err := s.mDB.Save(&setting).Error; err != nil {
  119. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.MysqlSaveError, nil, err)
  120. return
  121. }
  122. if err = StackLog(log); err != nil {
  123. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  124. tx.Rollback()
  125. return
  126. }
  127. if err := tx.Commit().Error; err != nil {
  128. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  129. tx.Rollback()
  130. return
  131. }
  132. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, err)
  133. return
  134. }