package admin import ( "errors" "fmt" "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 AdminCollectionV1Router struct { group *gin.RouterGroup mDB *gorm.DB rDB *gorm.DB } func NewAdminCollectionV1Router(r common.Router, basePath string) AdminCollectionV1Router { co := AdminCollectionV1Router{ group: r.Version.Group(basePath), mDB: r.Db.MasterDB, rDB: r.Db.ReadDB, } co.group.GET("", co.getCollectionList) co.group.PATCH("curation", co.updateCuration) co.group.PATCH("status", co.updateCollectionStatus) return co } // getCollectionList godoc // @Summary get collection list // @Description 데이터가 쌓인 이후에 진행 예정, 컬렉션 리스트 가져오기 // @Schemes // @security ApiKeyAuth // @Tags admin // @Accept json // @Produce json // @Success 200 {object} []view.CollectionInfo // @Router /admin/collection [get] func (co AdminCollectionV1Router) getCollectionList(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, co.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.Collection == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err) return } response := []view.CollectionInfo{} _err := view.GetCollectionInfo(co.rDB, 0).Order("collection.curating_number desc, collection.id desc").Find(&response).Error if errors.Is(_err, gorm.ErrRecordNotFound) { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, _err) return } err, _currency := common.UpdateCurrency(co.rDB) if err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err) return } for idx, item := range response { response[idx].Items = view.GetCollectionItemCount(co.rDB, item.ID) response[idx].HighestSalePrice = view.GetHighestPrice(co.rDB, _currency, item.ID) response[idx].MarketCap = view.GetMarketCap(co.rDB, _currency, item.ID) response[idx].TotalVolume = view.GetTotalVolume(co.rDB, _currency, item.ID) response[idx].RecentTradingDay = view.GetRecentTradingDate(co.rDB, item.ID) response[idx].TotalLikeCount = view.GetTotalLikeCount(co.rDB, item.ID) } gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil) } // updateCuration godoc // @Summary update collection curation // @Description 컬렉션 큐레이션 변경 // @Schemes // @security ApiKeyAuth // @Tags admin // @Accept json // @Produce json // @Param UpdateCuration body UpdateCuration true "selected target data" // @Success 200 {string} OK // @Router /admin/collection/curation [patch] func (co AdminCollectionV1Router) updateCuration(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, co.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.Collection == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err) return } request := UpdateCuration{} if err := c.ShouldBindJSON(&request); err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } if request.Index > 25 { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, errors.New("maximum index value is 25")) return } collection := models.Collection{} if err := co.rDB.Where("id = ?", request.ID).Find(&collection).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } else if collection.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusNotFound, gerror.NotFoundRecord, nil, err) return } tx := co.mDB.Begin() defer common.DBTransaction(tx) //SECTION admin log log := Log{ DB: tx, ActionType: "collection", SubActionType: "modified", Admin: admin, TargetID: request.ID, Target: "curation", To: fmt.Sprint(request.Index), } prevCollection := models.Collection{} if err := co.rDB.Where("curating_number = ?", request.Index).Find(&prevCollection).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } else if prevCollection.ID != 0 { test := models.Collection{} prevCollection.CuratingNumber = test.CuratingNumber if err := tx.Save(&prevCollection).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err) tx.Rollback() return } } collection.CuratingNumber = null.IntFrom(int64(request.Index)) if err := tx.Save(&collection).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 } // updateCollectionStatus godoc // @Summary update collection 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/collection/status [patch] func (co AdminCollectionV1Router) updateCollectionStatus(c *gin.Context) { admin, err := gauth.ConfirmAdminInfo(c, co.rDB) if err != nil || admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err) return } if admin.AdminPermission.Collection == 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 != "visible" && request.Status != "invisible" { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } collection := models.Collection{} if err := co.rDB.Where("id = ?", request.ID).Find(&collection).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err) return } else if collection.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err) return } tx := co.mDB.Begin() defer common.DBTransaction(tx) //SECTION admin log log := Log{ DB: tx, ActionType: "collection", SubActionType: "modified", Admin: admin, TargetID: request.ID, To: request.Status, From: collection.Status, Target: "collection", } collection.Status = request.Status if err := tx.Save(&collection).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 }