123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 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
- }
|