package admin import ( "net/http" "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" "github.com/metarare/metarare_api/view" "gorm.io/gorm" ) type AdminUserV1Router struct { group *gin.RouterGroup mDB *gorm.DB rDB *gorm.DB } func NewAdminUserV1Router(r common.Router, basePath string) AdminUserV1Router { u := AdminUserV1Router{ group: r.Version.Group(basePath), mDB: r.Db.MasterDB, rDB: r.Db.ReadDB, } u.group.GET("", u.getUserList) u.group.PATCH("status", u.updateStatus) return u } // getUserList godoc // @Summary get user list // @Description 유저 리스트 가져오기 // @Schemes // @security ApiKeyAuth // @Tags admin // @Accept json // @Produce json // @Success 200 {object} view.MemberList // @Router /admin/user [get] func (u AdminUserV1Router) getUserList(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, u.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.User == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err) return } response := []view.MemberList{} if err := view.GetMemberList(u.rDB).Find(&response).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err) return } gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil) return } // updateStatus godoc // @Summary update user status // @Description 유저 상태값 변경 // @Schemes // @security ApiKeyAuth // @Tags admin // @Accept json // @Produce json // @Param UpdateStatus body UpdateStatus true "selected target data" // @Success 200 {string} OK // @Router /admin/user/status [patch] func (u AdminUserV1Router) updateStatus(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, u.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.User == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err) return } request := UpdateStatus{} if err := c.ShouldBindJSON(&request); err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } if request.Status != "stable" && request.Status != "blocked" && request.Status != "withdrawal" { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } user := models.User{} if err := u.rDB.Where("id = ?", request.ID).Find(&user).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err) return } else if user.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err) return } tx := u.mDB.Begin() defer common.DBTransaction(tx) //SECTION admin log log := Log{ DB: tx, ActionType: "user", SubActionType: "modified", Admin: admin, TargetID: request.ID, To: request.Status, From: user.Status, } user.Status = request.Status if err := tx.Save(&user).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err) tx.Rollback() 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, nil) return }