zero prices methods
This commit is contained in:
parent
33327eaa2c
commit
756b5c126f
3 changed files with 235 additions and 112 deletions
|
|
@ -42,6 +42,12 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup) {
|
||||||
chartsGroup.GET("", h.controller.getChartsPrices)
|
chartsGroup.GET("", h.controller.getChartsPrices)
|
||||||
chartsGroup.GET("/:uuid", h.controller.getDistinctPrices)
|
chartsGroup.GET("/:uuid", h.controller.getDistinctPrices)
|
||||||
|
|
||||||
|
zeroPricesGroup := merchGroup.Group("/zeroprices")
|
||||||
|
zeroPricesGroup.GET("", h.controller.getZeroPrices)
|
||||||
|
zeroPricesGroup.DELETE("", h.controller.deleteZeroPrices)
|
||||||
|
|
||||||
|
zeroPricesGroup.DELETE("/period", h.controller.deleteZeroPricesPeriod)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create godoc
|
// create godoc
|
||||||
|
|
@ -313,7 +319,6 @@ func (co *controller) deleteOrigin(c *gin.Context) {
|
||||||
// @Summary Получить цены мерча за период
|
// @Summary Получить цены мерча за период
|
||||||
// @Description Получить цены мерча за период
|
// @Description Получить цены мерча за период
|
||||||
// @Tags Merch
|
// @Tags Merch
|
||||||
// @Security BearerAuth
|
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param days query string false "period in days"
|
// @Param days query string false "period in days"
|
||||||
// @Success 200 {array} PricesResponse
|
// @Success 200 {array} PricesResponse
|
||||||
|
|
@ -337,7 +342,6 @@ func (co *controller) getChartsPrices(c *gin.Context) {
|
||||||
// @Summary Получить перепады цен мерча за период по его merch_uuid
|
// @Summary Получить перепады цен мерча за период по его merch_uuid
|
||||||
// @Description Получить перепады цен мерча за период по его merch_uuid
|
// @Description Получить перепады цен мерча за период по его merch_uuid
|
||||||
// @Tags Merch
|
// @Tags Merch
|
||||||
// @Security BearerAuth
|
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param uuid path string true "merch_uuid"
|
// @Param uuid path string true "merch_uuid"
|
||||||
// @Param days query string false "period in days"
|
// @Param days query string false "period in days"
|
||||||
|
|
@ -370,3 +374,98 @@ func (co *controller) getDistinctPrices(c *gin.Context) {
|
||||||
|
|
||||||
c.JSON(http.StatusOK, response)
|
c.JSON(http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getZeroPrices godoc
|
||||||
|
//
|
||||||
|
// @Summary Получить нулевые цены
|
||||||
|
// @Description Получить нулевые цены
|
||||||
|
// @Tags Merch zero prices
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {array} ZeroPrice
|
||||||
|
// @Success 204
|
||||||
|
// @Failure 401 {object} responses.Unauthorized
|
||||||
|
// @Failure 500 {object} responses.InternalServerError
|
||||||
|
// @Router /merch/zeroprices [get]
|
||||||
|
func (co *controller) getZeroPrices(c *gin.Context) {
|
||||||
|
response, err := co.service.getZeroPrices(c, getUserId(c))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, responses.InternalServerError{Error: err.Error()})
|
||||||
|
logErr(controllerLogHeader, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if response == nil {
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteZeroPrices godoc
|
||||||
|
//
|
||||||
|
// @Summary Пометить нулевые цены как удаленные
|
||||||
|
// @Description Пометить нулевые цены как удаленные
|
||||||
|
// @Tags Merch zero prices
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Accept json
|
||||||
|
// @Param payload body DeleteZeroPrices true "payload"
|
||||||
|
// @Success 204
|
||||||
|
// @Failure 400 {object} responses.BadRequest
|
||||||
|
// @Failure 401 {object} responses.Unauthorized
|
||||||
|
// @Failure 500 {object} responses.InternalServerError
|
||||||
|
// @Router /merch/zeroprices [delete]
|
||||||
|
func (co *controller) deleteZeroPrices(c *gin.Context) {
|
||||||
|
var payload []DeleteZeroPrices
|
||||||
|
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, responses.BadRequest{Error: err.Error()})
|
||||||
|
logErr(controllerLogHeader, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(payload) < 1 || payload == nil {
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := co.service.deleteZeroPrices(c, getUserId(c), payload); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, responses.InternalServerError{Error: err.Error()})
|
||||||
|
logErr(controllerLogHeader, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Пометить нулевые цены как удаленные за указанный период
|
||||||
|
// @Description Пометить нулевые цены как удаленные за указанный период
|
||||||
|
// @Tags Merch zero prices
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Param start query string true "start"
|
||||||
|
// @Param end query string true "end"
|
||||||
|
// @Success 204
|
||||||
|
// @Failure 400 {object} responses.BadRequest
|
||||||
|
// @Failure 401 {object} responses.Unauthorized
|
||||||
|
// @Failure 500 {object} responses.InternalServerError
|
||||||
|
// @Router /merch/zeroprices/period [delete]
|
||||||
|
func (co *controller) deleteZeroPricesPeriod(c *gin.Context) {
|
||||||
|
start, err := co.utils.ParseTime(c.Query("start"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, responses.BadRequest{Error: err.Error()})
|
||||||
|
logErr(controllerLogHeader, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
end, err := co.utils.ParseTime(c.Query("end"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, responses.BadRequest{Error: err.Error()})
|
||||||
|
logErr(controllerLogHeader, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = co.service.deleteZeroPricesPeriod(c, getUserId(c), start, end); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, responses.InternalServerError{Error: err.Error()})
|
||||||
|
logErr(controllerLogHeader, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Status(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ type Prices interface {
|
||||||
|
|
||||||
getZeroPrices(ctx context.Context, userId int64) ([]ZeroPrice, error)
|
getZeroPrices(ctx context.Context, userId int64) ([]ZeroPrice, error)
|
||||||
deleteZeroPricesPeriod(ctx context.Context, userId int64, start, end time.Time, now sql.NullTime) error
|
deleteZeroPricesPeriod(ctx context.Context, userId int64, start, end time.Time, now sql.NullTime) error
|
||||||
deleteZeroPrices(ctx context.Context, now sql.NullTime, list []int64) error
|
deleteZeroPrices(ctx context.Context, userId int64, now sql.NullTime, list []int64) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repo) insertPrices(ctx context.Context, prices []Price) error {
|
func (r *repo) insertPrices(ctx context.Context, prices []Price) error {
|
||||||
|
|
@ -192,10 +192,17 @@ func (r *repo) deleteZeroPricesPeriod(ctx context.Context, userId int64, start,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repo) deleteZeroPrices(ctx context.Context, now sql.NullTime, list []int64) error {
|
func (r *repo) deleteZeroPrices(ctx context.Context, userId int64, now sql.NullTime, list []int64) error {
|
||||||
q := `UPDATE merch_prices SET deleted_at = $1 WHERE id IN $2`
|
q := `
|
||||||
|
UPDATE merch_prices mp
|
||||||
|
SET deleted_at = $1
|
||||||
|
FROM merch m
|
||||||
|
WHERE mp.id = ANY($2)
|
||||||
|
AND mp.merch_id = m.id
|
||||||
|
AND m.user_id = $3
|
||||||
|
`
|
||||||
|
|
||||||
_, err := r.db.Exec(ctx, q, now, list)
|
_, err := r.db.Exec(ctx, q, now, list, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *service) getPrices(ctx context.Context, userId int64, days int) ([]PricesResponse, error) {
|
func (s *service) getPrices(ctx context.Context, userId int64, days int) ([]PricesResponse, error) {
|
||||||
|
|
@ -123,3 +124,19 @@ func (s *service) getDistinctPrices(ctx context.Context, userId int64, merchUuid
|
||||||
|
|
||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *service) getZeroPrices(ctx context.Context, userId int64) ([]ZeroPrice, error) {
|
||||||
|
return s.repo.getZeroPrices(ctx, userId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) deleteZeroPricesPeriod(ctx context.Context, userId int64, start, end time.Time) error {
|
||||||
|
return s.repo.deleteZeroPricesPeriod(ctx, userId, start, end, s.utils.NullTimeNowUTC())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) deleteZeroPrices(ctx context.Context, userId int64, list []DeleteZeroPrices) error {
|
||||||
|
var l []int64
|
||||||
|
for _, item := range list {
|
||||||
|
l = append(l, item.Id)
|
||||||
|
}
|
||||||
|
return s.repo.deleteZeroPrices(ctx, userId, s.utils.NullTimeNowUTC(), l)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue