123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- package admin
- import (
- "errors"
- "net/http"
- "github.com/gin-gonic/gin"
- "github.com/guregu/null"
- "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 AdminArtistV1Router struct {
- group *gin.RouterGroup
- mDB *gorm.DB
- rDB *gorm.DB
- }
- func NewAdminArtistV1Router(r common.Router, basePath string) AdminArtistV1Router {
- ar := AdminArtistV1Router{
- group: r.Version.Group(basePath),
- mDB: r.Db.MasterDB,
- rDB: r.Db.ReadDB,
- }
- ar.group.GET("", ar.getArtistList)
- ar.group.POST("", ar.registerArtist)
- ar.group.PATCH("status", ar.updateArtistStatus)
- return ar
- }
- // getArtistList godoc
- // @Summary get artist list
- // @Description 작가 리스트 가져오기
- // @Schemes
- // @security ApiKeyAuth
- // @Tags admin
- // @Accept json
- // @Produce json
- // @Success 200 {object} view.MemberList
- // @Router /admin/artist [get]
- func (ar AdminArtistV1Router) getArtistList(c *gin.Context) {
- admin, err := gauth.ConfirmAdminInfo(c, ar.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(ar.rDB).Where("artist_profile.id IS NOT NULL").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
- }
- // updateArtistStatus godoc
- // @Summary update artist 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/artist/status [patch]
- func (ar AdminArtistV1Router) updateArtistStatus(c *gin.Context) {
- admin, err := gauth.ConfirmAdminInfo(c, ar.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 := ar.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 := ar.mDB.Begin()
- defer common.DBTransaction(tx)
- //SECTION admin log
- log := Log{
- DB: tx,
- ActionType: "artist",
- 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
- }
- // registerArtist godoc
- // @Summary create artist
- // @Description 작가 등록
- // @Schemes
- // @security ApiKeyAuth
- // @Tags admin
- // @Accept json
- // @Produce json
- // @Param RegisterArtistData body RegisterArtistData true "body struct"
- // @Success 200 {number} authentication.UserID
- // @Router /admin/artist [post]
- func (ar AdminArtistV1Router) registerArtist(c *gin.Context) {
- admin, err := gauth.ConfirmAdminInfo(c, ar.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 := RegisterArtistData{}
- if err := c.ShouldBindJSON(&request); err != nil {
- gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
- return
- }
- authentication := models.UserAuthentication{}
- if err := ar.rDB.Where("email = ?", request.Email).Find(&authentication).Error; err != nil {
- gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
- return
- } else if authentication.ID == 0 {
- gerror.IntegratedResponseToRequest(c, http.StatusNotFound, gerror.NotFoundRecord, nil, err)
- return
- }
- //NOTE x artist_profile 확인
- _artist := models.ArtistProfile{}
- if err := ar.rDB.Where("user_id = ?", authentication.UserID).Find(&_artist).Error; err != nil {
- gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
- return
- } else if _artist.ID != 0 {
- gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.DuplicateValue, nil, errors.New("duplicate value"))
- return
- }
- profile := models.UserProfile{}
- if err := ar.rDB.Where("user_id = ?", authentication.UserID).Find(&profile).Error; err != nil {
- gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err)
- return
- }
- tx := ar.mDB.Begin()
- defer common.DBTransaction(tx)
- profile.Name = null.StringFrom(request.Name)
- profile.Phone = null.StringFrom(request.Phone)
- if err := tx.Save(&profile).Error; err != nil {
- gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
- tx.Rollback()
- return
- }
- artist := models.ArtistProfile{
- UserID: authentication.UserID,
- Team: null.StringFrom(request.Team),
- Category: null.StringFrom(request.Category),
- }
- if err := tx.Save(&artist).Error; err != nil {
- gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
- tx.Rollback()
- return
- }
- //SECTION admin log
- log := Log{
- DB: tx,
- ActionType: "artist",
- SubActionType: "created",
- Admin: admin,
- TargetID: artist.UserID,
- }
- 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, authentication.UserID, nil)
- return
- }
|