collection.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package admin
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "github.com/guregu/null"
  8. "github.com/metarare/metarare_api/common"
  9. "github.com/metarare/metarare_api/helpers/gauth"
  10. "github.com/metarare/metarare_api/helpers/gerror"
  11. "github.com/metarare/metarare_api/models"
  12. "github.com/metarare/metarare_api/view"
  13. "gorm.io/gorm"
  14. )
  15. type AdminCollectionV1Router struct {
  16. group *gin.RouterGroup
  17. mDB *gorm.DB
  18. rDB *gorm.DB
  19. }
  20. func NewAdminCollectionV1Router(r common.Router, basePath string) AdminCollectionV1Router {
  21. co := AdminCollectionV1Router{
  22. group: r.Version.Group(basePath),
  23. mDB: r.Db.MasterDB,
  24. rDB: r.Db.ReadDB,
  25. }
  26. co.group.GET("", co.getCollectionList)
  27. co.group.PATCH("curation", co.updateCuration)
  28. co.group.PATCH("status", co.updateCollectionStatus)
  29. return co
  30. }
  31. // getCollectionList godoc
  32. // @Summary get collection list
  33. // @Description 데이터가 쌓인 이후에 진행 예정, 컬렉션 리스트 가져오기
  34. // @Schemes
  35. // @security ApiKeyAuth
  36. // @Tags admin
  37. // @Accept json
  38. // @Produce json
  39. // @Success 200 {object} []view.CollectionInfo
  40. // @Router /admin/collection [get]
  41. func (co AdminCollectionV1Router) getCollectionList(c *gin.Context) {
  42. admin, err := gauth.ConfirmAdminInfo(c, co.rDB)
  43. if err != nil || admin.ID == 0 {
  44. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  45. return
  46. }
  47. if admin.AdminPermission.Collection == 0 {
  48. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  49. return
  50. }
  51. response := []view.CollectionInfo{}
  52. _err := view.GetCollectionInfo(co.rDB, 0).Order("collection.curating_number desc, collection.id desc").Find(&response).Error
  53. if errors.Is(_err, gorm.ErrRecordNotFound) {
  54. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, _err)
  55. return
  56. }
  57. err, _currency := common.UpdateCurrency(co.rDB)
  58. if err != nil {
  59. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.InvalidParameterValue, nil, err)
  60. return
  61. }
  62. for idx, item := range response {
  63. response[idx].Items = view.GetCollectionItemCount(co.rDB, item.ID)
  64. response[idx].HighestSalePrice = view.GetHighestPrice(co.rDB, _currency, item.ID)
  65. response[idx].MarketCap = view.GetMarketCap(co.rDB, _currency, item.ID)
  66. response[idx].TotalVolume = view.GetTotalVolume(co.rDB, _currency, item.ID)
  67. response[idx].RecentTradingDay = view.GetRecentTradingDate(co.rDB, item.ID)
  68. response[idx].TotalLikeCount = view.GetTotalLikeCount(co.rDB, item.ID)
  69. }
  70. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil)
  71. }
  72. // updateCuration godoc
  73. // @Summary update collection curation
  74. // @Description 컬렉션 큐레이션 변경
  75. // @Schemes
  76. // @security ApiKeyAuth
  77. // @Tags admin
  78. // @Accept json
  79. // @Produce json
  80. // @Param UpdateCuration body UpdateCuration true "selected target data"
  81. // @Success 200 {string} OK
  82. // @Router /admin/collection/curation [patch]
  83. func (co AdminCollectionV1Router) updateCuration(c *gin.Context) {
  84. admin, err := gauth.ConfirmAdminInfo(c, co.rDB)
  85. if err != nil || admin.ID == 0 {
  86. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  87. return
  88. }
  89. if admin.AdminPermission.Collection == 0 {
  90. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  91. return
  92. }
  93. request := UpdateCuration{}
  94. if err := c.ShouldBindJSON(&request); err != nil {
  95. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  96. return
  97. }
  98. if request.Index > 25 {
  99. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, errors.New("maximum index value is 25"))
  100. return
  101. }
  102. collection := models.Collection{}
  103. if err := co.rDB.Where("id = ?", request.ID).Find(&collection).Error; err != nil {
  104. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  105. return
  106. } else if collection.ID == 0 {
  107. gerror.IntegratedResponseToRequest(c, http.StatusNotFound, gerror.NotFoundRecord, nil, err)
  108. return
  109. }
  110. tx := co.mDB.Begin()
  111. defer common.DBTransaction(tx)
  112. //SECTION admin log
  113. log := Log{
  114. DB: tx,
  115. ActionType: "collection",
  116. SubActionType: "modified",
  117. Admin: admin,
  118. TargetID: request.ID,
  119. Target: "curation",
  120. To: fmt.Sprint(request.Index),
  121. }
  122. prevCollection := models.Collection{}
  123. if err := co.rDB.Where("curating_number = ?", request.Index).Find(&prevCollection).Error; err != nil {
  124. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  125. return
  126. } else if prevCollection.ID != 0 {
  127. test := models.Collection{}
  128. prevCollection.CuratingNumber = test.CuratingNumber
  129. if err := tx.Save(&prevCollection).Error; err != nil {
  130. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  131. tx.Rollback()
  132. return
  133. }
  134. }
  135. collection.CuratingNumber = null.IntFrom(int64(request.Index))
  136. if err := tx.Save(&collection).Error; err != nil {
  137. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  138. tx.Rollback()
  139. return
  140. }
  141. if err = StackLog(log); err != nil {
  142. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  143. tx.Rollback()
  144. return
  145. }
  146. if err := tx.Commit().Error; err != nil {
  147. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  148. tx.Rollback()
  149. return
  150. }
  151. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, nil)
  152. return
  153. }
  154. // updateCollectionStatus godoc
  155. // @Summary update collection status
  156. // @Description 컬렉션 상태값 변경 (보이기, 숨김)
  157. // @Schemes
  158. // @security ApiKeyAuth
  159. // @Tags admin
  160. // @Accept json
  161. // @Produce json
  162. // @Param UpdateStatus body UpdateStatus true "selected target data"
  163. // @Success 200 {string} OK
  164. // @Router /admin/collection/status [patch]
  165. func (co AdminCollectionV1Router) updateCollectionStatus(c *gin.Context) {
  166. admin, err := gauth.ConfirmAdminInfo(c, co.rDB)
  167. if err != nil || admin.ID == 0 {
  168. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  169. return
  170. }
  171. if admin.AdminPermission.Collection == 0 {
  172. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  173. return
  174. }
  175. request := UpdateStatus{}
  176. if err := c.ShouldBindJSON(&request); err != nil {
  177. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  178. return
  179. }
  180. if request.Status != "visible" && request.Status != "invisible" {
  181. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  182. return
  183. }
  184. collection := models.Collection{}
  185. if err := co.rDB.Where("id = ?", request.ID).Find(&collection).Error; err != nil {
  186. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  187. return
  188. } else if collection.ID == 0 {
  189. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err)
  190. return
  191. }
  192. tx := co.mDB.Begin()
  193. defer common.DBTransaction(tx)
  194. //SECTION admin log
  195. log := Log{
  196. DB: tx,
  197. ActionType: "collection",
  198. SubActionType: "modified",
  199. Admin: admin,
  200. TargetID: request.ID,
  201. To: request.Status,
  202. From: collection.Status,
  203. Target: "collection",
  204. }
  205. collection.Status = request.Status
  206. if err := tx.Save(&collection).Error; err != nil {
  207. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  208. tx.Rollback()
  209. return
  210. }
  211. if err = StackLog(log); err != nil {
  212. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  213. tx.Rollback()
  214. return
  215. }
  216. if err := tx.Commit().Error; err != nil {
  217. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  218. tx.Rollback()
  219. return
  220. }
  221. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, nil)
  222. return
  223. }