user.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/metarare/metarare_api/common"
  6. "github.com/metarare/metarare_api/helpers/gauth"
  7. "github.com/metarare/metarare_api/helpers/gerror"
  8. "github.com/metarare/metarare_api/models"
  9. "github.com/metarare/metarare_api/view"
  10. "gorm.io/gorm"
  11. )
  12. type AdminUserV1Router struct {
  13. group *gin.RouterGroup
  14. mDB *gorm.DB
  15. rDB *gorm.DB
  16. }
  17. func NewAdminUserV1Router(r common.Router, basePath string) AdminUserV1Router {
  18. u := AdminUserV1Router{
  19. group: r.Version.Group(basePath),
  20. mDB: r.Db.MasterDB,
  21. rDB: r.Db.ReadDB,
  22. }
  23. u.group.GET("", u.getUserList)
  24. u.group.PATCH("status", u.updateStatus)
  25. return u
  26. }
  27. // getUserList godoc
  28. // @Summary get user list
  29. // @Description 유저 리스트 가져오기
  30. // @Schemes
  31. // @security ApiKeyAuth
  32. // @Tags admin
  33. // @Accept json
  34. // @Produce json
  35. // @Success 200 {object} view.MemberList
  36. // @Router /admin/user [get]
  37. func (u AdminUserV1Router) getUserList(c *gin.Context) {
  38. admin, err := gauth.ConfirmAdminInfo(c, u.rDB)
  39. if err != nil || admin.ID == 0 {
  40. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  41. return
  42. }
  43. if admin.AdminPermission.User == 0 {
  44. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  45. return
  46. }
  47. response := []view.MemberList{}
  48. if err := view.GetMemberList(u.rDB).Find(&response).Error; err != nil {
  49. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  50. return
  51. }
  52. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil)
  53. return
  54. }
  55. // updateStatus godoc
  56. // @Summary update user status
  57. // @Description 유저 상태값 변경
  58. // @Schemes
  59. // @security ApiKeyAuth
  60. // @Tags admin
  61. // @Accept json
  62. // @Produce json
  63. // @Param UpdateStatus body UpdateStatus true "selected target data"
  64. // @Success 200 {string} OK
  65. // @Router /admin/user/status [patch]
  66. func (u AdminUserV1Router) updateStatus(c *gin.Context) {
  67. admin, err := gauth.ConfirmAdminInfo(c, u.rDB)
  68. if err != nil || admin.ID == 0 {
  69. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  70. return
  71. }
  72. if admin.AdminPermission.User == 0 {
  73. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  74. return
  75. }
  76. request := UpdateStatus{}
  77. if err := c.ShouldBindJSON(&request); err != nil {
  78. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  79. return
  80. }
  81. if request.Status != "stable" && request.Status != "blocked" && request.Status != "withdrawal" {
  82. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  83. return
  84. }
  85. user := models.User{}
  86. if err := u.rDB.Where("id = ?", request.ID).Find(&user).Error; err != nil {
  87. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  88. return
  89. } else if user.ID == 0 {
  90. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err)
  91. return
  92. }
  93. tx := u.mDB.Begin()
  94. defer common.DBTransaction(tx)
  95. //SECTION admin log
  96. log := Log{
  97. DB: tx,
  98. ActionType: "user",
  99. SubActionType: "modified",
  100. Admin: admin,
  101. TargetID: request.ID,
  102. To: request.Status,
  103. From: user.Status,
  104. }
  105. user.Status = request.Status
  106. if err := tx.Save(&user).Error; err != nil {
  107. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  108. tx.Rollback()
  109. return
  110. }
  111. if err = StackLog(log); err != nil {
  112. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  113. tx.Rollback()
  114. return
  115. }
  116. if err := tx.Commit().Error; err != nil {
  117. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  118. tx.Rollback()
  119. return
  120. }
  121. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, nil)
  122. return
  123. }