package admin import ( "errors" "net/http" "strconv" "github.com/gin-gonic/gin" "github.com/metarare/metarare_api/common" "github.com/metarare/metarare_api/helpers/gauth" "github.com/metarare/metarare_api/helpers/gerror" "github.com/metarare/metarare_api/models" "gorm.io/gorm" ) type AdminSettingV1Router struct { group *gin.RouterGroup mDB *gorm.DB rDB *gorm.DB } func NewAdminSettingV1Router(r common.Router, basePath string) AdminSettingV1Router { s := AdminSettingV1Router{ group: r.Version.Group(basePath), mDB: r.Db.MasterDB, rDB: r.Db.ReadDB, } s.group.GET("", s.getSettingList) s.group.PATCH(":type", s.updateSetting) return s } // getSettingList godoc // @Summary get setting list // @Description 현재 셋팅 값 가져오기 // @Schemes // @security ApiKeyAuth // @Tags admin // @Accept json // @Produce json // @Success 200 {object} models.Setting // @Router /admin/setting [get] func (s AdminSettingV1Router) getSettingList(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, s.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.System == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err) return } response := models.Setting{} if err := s.rDB.Find(&response).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err) return } gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil) return } // updateSetting godoc // @Summary update setting value // @Description 설정 값 변경 // @Schemes // @security ApiKeyAuth // @Tags admin // @Accept json // @Produce json // @Param SettingData body SettingData true "selected target data" // @Param type path string true "type: commission, special_collection, gas_deposit" // @Success 200 {string} OK // @Router /admin/setting/{type} [patch] func (s AdminSettingV1Router) updateSetting(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, s.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.System == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err) return } _type := c.Param("type") if _type == "" { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, errors.New("invalid parameter value")) return } request := SettingData{} if err := c.ShouldBindJSON(&request); err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } setting := models.Setting{} if err := s.rDB.Find(&setting).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err) return } tx := s.mDB.Begin() defer common.DBTransaction(tx) //SECTION admin log log := Log{ DB: tx, ActionType: "system", SubActionType: "modified", Admin: admin, From: "", To: "", Target: _type, } if _type == "special_collection" { log.From = setting.SpecialCollection log.To = request.SpecialCollection setting.SpecialCollection = request.SpecialCollection } else if _type == "commission" { log.From = strconv.FormatFloat(setting.Commission, 'f', -1, 64) log.To = strconv.FormatFloat(request.Commission, 'f', -1, 64) setting.Commission = request.Commission } else if _type == "gas_deposit" { log.From = strconv.FormatFloat(setting.GasDeposit, 'f', -1, 64) log.To = strconv.FormatFloat(request.GasDeposit, 'f', -1, 64) setting.GasDeposit = request.GasDeposit } if err := s.mDB.Save(&setting).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.MysqlSaveError, nil, err) return } if err = StackLog(log); err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err) tx.Rollback() return } if err := tx.Commit().Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err) tx.Rollback() return } gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, err) return }