222 lines
7.3 KiB
Go
222 lines
7.3 KiB
Go
package merch
|
||
|
||
import (
|
||
"github.com/gin-gonic/gin"
|
||
log "github.com/sirupsen/logrus"
|
||
"merch-parser-api/internal/interfaces"
|
||
"merch-parser-api/pkg/responses"
|
||
"net/http"
|
||
"strings"
|
||
)
|
||
|
||
type controller struct {
|
||
service *service
|
||
utils interfaces.Utils
|
||
}
|
||
|
||
func newController(service *service, utils interfaces.Utils) *controller {
|
||
return &controller{
|
||
service: service,
|
||
utils: utils,
|
||
}
|
||
}
|
||
|
||
func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, refreshMW gin.HandlerFunc) {
|
||
merchGroup := r.Group("/merch", authMW)
|
||
|
||
merchGroup.POST("/", h.controller.addMerch)
|
||
merchGroup.GET("/:uuid", h.controller.getSingleMerch)
|
||
merchGroup.GET("/", h.controller.getAllMerch)
|
||
merchGroup.PUT("/", h.controller.updateMerch)
|
||
merchGroup.DELETE("/:uuid", h.controller.deleteMerch)
|
||
|
||
chartsGroup := r.Group("/prices", authMW)
|
||
chartsGroup.GET("", h.controller.getChartsPrices)
|
||
|
||
}
|
||
|
||
// @Summary Добавить новый мерч
|
||
// @Description Добавить новый мерч
|
||
// @Tags Merch
|
||
// @Security BearerAuth
|
||
// @Accept json
|
||
// @Param body body MerchDTO true "новый мерч"
|
||
// @Success 200
|
||
// @Failure 400 {object} responses.ErrorResponse400
|
||
// @Failure 500 {object} responses.ErrorResponse500
|
||
// @Router /merch [post]
|
||
func (co *controller) addMerch(c *gin.Context) {
|
||
var payload MerchDTO
|
||
if err := c.ShouldBind(&payload); err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to bind JSON on add merch")
|
||
return
|
||
}
|
||
|
||
userUuid, err := co.utils.GetUserUuidFromContext(c)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get user uuid from context")
|
||
return
|
||
}
|
||
|
||
err = co.service.addMerch(payload, userUuid)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to add merch")
|
||
return
|
||
}
|
||
|
||
c.Status(http.StatusOK)
|
||
}
|
||
|
||
// @Summary Получить всю информацию про мерч
|
||
// @Description Получить всю информацию про мерч по его uuid
|
||
// @Tags Merch
|
||
// @Security BearerAuth
|
||
// @Param uuid path string true "merch_uuid"
|
||
// @Success 200 {object} MerchDTO
|
||
// @Failure 400 {object} responses.ErrorResponse400
|
||
// @Failure 500 {object} responses.ErrorResponse500
|
||
// @Router /merch/{uuid} [get]
|
||
func (co *controller) getSingleMerch(c *gin.Context) {
|
||
merchUuid := c.Param("uuid")
|
||
if merchUuid == "" {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"})
|
||
return
|
||
}
|
||
|
||
userUuid, err := co.utils.GetUserUuidFromContext(c)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get user uuid from context")
|
||
return
|
||
}
|
||
|
||
response, err := co.service.getSingleMerch(userUuid, merchUuid)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get single merch")
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, response)
|
||
}
|
||
|
||
// @Summary Получить все записи мерча
|
||
// @Description Получить все записи мерча
|
||
// @Tags Merch
|
||
// @Security BearerAuth
|
||
// @Success 200 {array} ListResponse
|
||
// @Failure 400 {object} responses.ErrorResponse400
|
||
// @Failure 500 {object} responses.ErrorResponse500
|
||
// @Router /merch/ [get]
|
||
func (co *controller) getAllMerch(c *gin.Context) {
|
||
userUuid, err := co.utils.GetUserUuidFromContext(c)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get user uuid from context")
|
||
return
|
||
}
|
||
|
||
response, err := co.service.getAllMerch(userUuid)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get all merch")
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, response)
|
||
}
|
||
|
||
// @Summary Обновить информацию про мерч
|
||
// @Description Обновить информацию про мерч по его uuid в json-е
|
||
// @Tags Merch
|
||
// @Security BearerAuth
|
||
// @Param body body MerchDTO true "merch_uuid"
|
||
// @Success 200 {object} MerchDTO
|
||
// @Failure 400 {object} responses.ErrorResponse400
|
||
// @Failure 500 {object} responses.ErrorResponse500
|
||
// @Router /merch/{uuid} [put]
|
||
func (co *controller) updateMerch(c *gin.Context) {
|
||
var payload MerchDTO
|
||
if err := c.ShouldBind(&payload); err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to bind JSON on add merch")
|
||
return
|
||
}
|
||
|
||
userUuid, err := co.utils.GetUserUuidFromContext(c)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get user uuid from context")
|
||
return
|
||
}
|
||
|
||
if err = co.service.updateMerch(payload, userUuid); err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get single merch")
|
||
return
|
||
}
|
||
}
|
||
|
||
// @Summary Пометить мерч как удаленный
|
||
// @Description Пометить мерч как удаленный по его uuid
|
||
// @Tags Merch
|
||
// @Security BearerAuth
|
||
// @Param uuid path string true "merch_uuid"
|
||
// @Success 200 {object} MerchDTO
|
||
// @Failure 400 {object} responses.ErrorResponse400
|
||
// @Failure 500 {object} responses.ErrorResponse500
|
||
// @Router /merch/{uuid} [delete]
|
||
func (co *controller) deleteMerch(c *gin.Context) {
|
||
merchUuid := c.Param("uuid")
|
||
if merchUuid == "" {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"})
|
||
return
|
||
}
|
||
|
||
userUuid, err := co.utils.GetUserUuidFromContext(c)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get user uuid from context")
|
||
return
|
||
}
|
||
|
||
if err = co.service.deleteMerch(userUuid, merchUuid); err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get single merch")
|
||
return
|
||
}
|
||
|
||
c.Status(http.StatusOK)
|
||
}
|
||
|
||
// @Summary Получить цены мерча за период
|
||
// @Description Получить цены мерча за период
|
||
// @Tags Merch
|
||
// @Security BearerAuth
|
||
// @Param days query string false "merch_uuid"
|
||
// @Success 200 {array} PricesResponse
|
||
// @Failure 400 {object} responses.ErrorResponse400
|
||
// @Failure 500 {object} responses.ErrorResponse500
|
||
// @Router /prices [get]
|
||
func (co *controller) getChartsPrices(c *gin.Context) {
|
||
daysQuery := strings.ToLower(c.DefaultQuery("days", ""))
|
||
|
||
userUuid, err := co.utils.GetUserUuidFromContext(c)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get user uuid from context")
|
||
return
|
||
}
|
||
|
||
response, err := co.service.getPrices(userUuid, daysQuery)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()})
|
||
log.WithError(err).Error("Merch | Failed to get single merch")
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, response)
|
||
}
|