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