artist.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package admin
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/guregu/null"
  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. "github.com/metarare/metarare_api/view"
  12. "gorm.io/gorm"
  13. )
  14. type AdminArtistV1Router struct {
  15. group *gin.RouterGroup
  16. mDB *gorm.DB
  17. rDB *gorm.DB
  18. }
  19. func NewAdminArtistV1Router(r common.Router, basePath string) AdminArtistV1Router {
  20. ar := AdminArtistV1Router{
  21. group: r.Version.Group(basePath),
  22. mDB: r.Db.MasterDB,
  23. rDB: r.Db.ReadDB,
  24. }
  25. ar.group.GET("", ar.getArtistList)
  26. ar.group.POST("", ar.registerArtist)
  27. ar.group.PATCH("status", ar.updateArtistStatus)
  28. return ar
  29. }
  30. // getArtistList godoc
  31. // @Summary get artist list
  32. // @Description 작가 리스트 가져오기
  33. // @Schemes
  34. // @security ApiKeyAuth
  35. // @Tags admin
  36. // @Accept json
  37. // @Produce json
  38. // @Success 200 {object} view.MemberList
  39. // @Router /admin/artist [get]
  40. func (ar AdminArtistV1Router) getArtistList(c *gin.Context) {
  41. admin, err := gauth.ConfirmAdminInfo(c, ar.rDB)
  42. if err != nil || admin.ID == 0 {
  43. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  44. return
  45. }
  46. if admin.AdminPermission.User == 0 {
  47. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  48. return
  49. }
  50. response := []view.MemberList{}
  51. if err := view.GetMemberList(ar.rDB).Where("artist_profile.id IS NOT NULL").Find(&response).Error; err != nil {
  52. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  53. return
  54. }
  55. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil)
  56. return
  57. }
  58. // updateArtistStatus godoc
  59. // @Summary update artist status
  60. // @Description 작가 상태값 변경
  61. // @Schemes
  62. // @security ApiKeyAuth
  63. // @Tags admin
  64. // @Accept json
  65. // @Produce json
  66. // @Param UpdateStatus body UpdateStatus true "selected target data"
  67. // @Success 200 {string} OK
  68. // @Router /admin/artist/status [patch]
  69. func (ar AdminArtistV1Router) updateArtistStatus(c *gin.Context) {
  70. admin, err := gauth.ConfirmAdminInfo(c, ar.rDB)
  71. if err != nil || admin.ID == 0 {
  72. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  73. return
  74. }
  75. if admin.AdminPermission.User == 0 {
  76. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  77. return
  78. }
  79. request := UpdateStatus{}
  80. if err := c.ShouldBindJSON(&request); err != nil {
  81. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  82. return
  83. }
  84. if request.Status != "stable" && request.Status != "blocked" && request.Status != "withdrawal" {
  85. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  86. return
  87. }
  88. user := models.User{}
  89. if err := ar.rDB.Where("id = ?", request.ID).Find(&user).Error; err != nil {
  90. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  91. return
  92. } else if user.ID == 0 {
  93. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err)
  94. return
  95. }
  96. tx := ar.mDB.Begin()
  97. defer common.DBTransaction(tx)
  98. //SECTION admin log
  99. log := Log{
  100. DB: tx,
  101. ActionType: "artist",
  102. SubActionType: "modified",
  103. Admin: admin,
  104. TargetID: request.ID,
  105. To: request.Status,
  106. From: user.Status,
  107. }
  108. user.Status = request.Status
  109. if err := tx.Save(&user).Error; err != nil {
  110. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  111. tx.Rollback()
  112. return
  113. }
  114. if err = StackLog(log); err != nil {
  115. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  116. tx.Rollback()
  117. return
  118. }
  119. if err := tx.Commit().Error; err != nil {
  120. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  121. tx.Rollback()
  122. return
  123. }
  124. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, nil)
  125. return
  126. }
  127. // registerArtist godoc
  128. // @Summary create artist
  129. // @Description 작가 등록
  130. // @Schemes
  131. // @security ApiKeyAuth
  132. // @Tags admin
  133. // @Accept json
  134. // @Produce json
  135. // @Param RegisterArtistData body RegisterArtistData true "body struct"
  136. // @Success 200 {number} authentication.UserID
  137. // @Router /admin/artist [post]
  138. func (ar AdminArtistV1Router) registerArtist(c *gin.Context) {
  139. admin, err := gauth.ConfirmAdminInfo(c, ar.rDB)
  140. if err != nil || admin.ID == 0 {
  141. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  142. return
  143. }
  144. if admin.AdminPermission.User == 0 {
  145. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  146. return
  147. }
  148. request := RegisterArtistData{}
  149. if err := c.ShouldBindJSON(&request); err != nil {
  150. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  151. return
  152. }
  153. authentication := models.UserAuthentication{}
  154. if err := ar.rDB.Where("email = ?", request.Email).Find(&authentication).Error; err != nil {
  155. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  156. return
  157. } else if authentication.ID == 0 {
  158. gerror.IntegratedResponseToRequest(c, http.StatusNotFound, gerror.NotFoundRecord, nil, err)
  159. return
  160. }
  161. //NOTE x artist_profile 확인
  162. _artist := models.ArtistProfile{}
  163. if err := ar.rDB.Where("user_id = ?", authentication.UserID).Find(&_artist).Error; err != nil {
  164. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  165. return
  166. } else if _artist.ID != 0 {
  167. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.DuplicateValue, nil, errors.New("duplicate value"))
  168. return
  169. }
  170. profile := models.UserProfile{}
  171. if err := ar.rDB.Where("user_id = ?", authentication.UserID).Find(&profile).Error; err != nil {
  172. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err)
  173. return
  174. }
  175. tx := ar.mDB.Begin()
  176. defer common.DBTransaction(tx)
  177. profile.Name = null.StringFrom(request.Name)
  178. profile.Phone = null.StringFrom(request.Phone)
  179. if err := tx.Save(&profile).Error; err != nil {
  180. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  181. tx.Rollback()
  182. return
  183. }
  184. artist := models.ArtistProfile{
  185. UserID: authentication.UserID,
  186. Team: null.StringFrom(request.Team),
  187. Category: null.StringFrom(request.Category),
  188. }
  189. if err := tx.Save(&artist).Error; err != nil {
  190. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  191. tx.Rollback()
  192. return
  193. }
  194. //SECTION admin log
  195. log := Log{
  196. DB: tx,
  197. ActionType: "artist",
  198. SubActionType: "created",
  199. Admin: admin,
  200. TargetID: artist.UserID,
  201. }
  202. if err = StackLog(log); err != nil {
  203. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  204. tx.Rollback()
  205. return
  206. }
  207. if err := tx.Commit().Error; err != nil {
  208. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  209. tx.Rollback()
  210. return
  211. }
  212. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, authentication.UserID, nil)
  213. return
  214. }