fixes
This commit is contained in:
parent
a0e21db5a0
commit
2728051fde
4 changed files with 58 additions and 17 deletions
|
|
@ -731,7 +731,7 @@ func (co *controller) deleteZeroPrices(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var payload DeleteZeroPrices
|
var payload []DeleteZeroPrices
|
||||||
if err = c.ShouldBindJSON(&payload); err != nil {
|
if err = c.ShouldBindJSON(&payload); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||||||
log.WithError(err).Error(logMsg)
|
log.WithError(err).Error(logMsg)
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,7 @@ type LabelLink struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ZeroPrice struct {
|
type ZeroPrice struct {
|
||||||
|
Id int `json:"id"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
MerchUuid string `json:"merch_uuid"`
|
MerchUuid string `json:"merch_uuid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
@ -89,5 +90,6 @@ type ZeroPrice struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteZeroPrices struct {
|
type DeleteZeroPrices struct {
|
||||||
MerchUuids []string `json:"merch_uuids"`
|
Id uint `json:"id"`
|
||||||
|
MerchUuid string `json:"merch_uuid"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package merch
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -22,6 +23,7 @@ type repository interface {
|
||||||
addMerch(bundle merchBundle) error
|
addMerch(bundle merchBundle) error
|
||||||
|
|
||||||
merchRecordExists(userUuid, merchUuid string) (bool, error)
|
merchRecordExists(userUuid, merchUuid string) (bool, error)
|
||||||
|
userOwnsMerchUuids(userUuid string, merchUuids []string) (bool, error)
|
||||||
getSingleMerch(userUuid, merchUuid string) (merchBundle, error)
|
getSingleMerch(userUuid, merchUuid string) (merchBundle, error)
|
||||||
getAllMerch(userUuid string) ([]ListResponse, error)
|
getAllMerch(userUuid string) ([]ListResponse, error)
|
||||||
|
|
||||||
|
|
@ -40,7 +42,7 @@ type prices interface {
|
||||||
getDistinctPrices(userUuid, merchUuid string, period time.Time) (prices []Price, err error)
|
getDistinctPrices(userUuid, merchUuid string, period time.Time) (prices []Price, err error)
|
||||||
|
|
||||||
getZeroPrices(userUuid string) ([]ZeroPrice, error)
|
getZeroPrices(userUuid string) ([]ZeroPrice, error)
|
||||||
deleteZeroPrices(userUuid string, list []string) error
|
deleteZeroPrices(list []DeleteZeroPrices) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type labels interface {
|
type labels interface {
|
||||||
|
|
@ -84,6 +86,22 @@ func (r *Repo) merchRecordExists(userUuid, merchUuid string) (bool, error) {
|
||||||
return exists, err
|
return exists, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repo) userOwnsMerchUuids(userUuid string, merchUuids []string) (bool, error) {
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
err := r.db.Model(&Merch{}).
|
||||||
|
Where("user_uuid = ?", userUuid).
|
||||||
|
Where("merch_uuid IN ?", merchUuids).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Count(&count).Error
|
||||||
|
fmt.Println("!!!!!!", count)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
fmt.Println("!!!!!!", len(merchUuids))
|
||||||
|
return count == int64(len(merchUuids)), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Repo) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) {
|
func (r *Repo) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) {
|
||||||
var merch Merch
|
var merch Merch
|
||||||
if err := r.db.
|
if err := r.db.
|
||||||
|
|
@ -320,16 +338,17 @@ func (r *Repo) getZeroPrices(userUuid string) ([]ZeroPrice, error) {
|
||||||
if err := r.db.Raw(`
|
if err := r.db.Raw(`
|
||||||
WITH price_with_neighbors AS (
|
WITH price_with_neighbors AS (
|
||||||
SELECT
|
SELECT
|
||||||
p.created_at, p.merch_uuid, p.price, p.origin, m.name,
|
p.id, p.created_at, p.merch_uuid, p.price, p.origin, m.name,
|
||||||
LAG(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS prev_price,
|
LAG(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS prev_price,
|
||||||
LEAD(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS next_price
|
LEAD(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS next_price
|
||||||
FROM prices AS p
|
FROM prices AS p
|
||||||
JOIN merch as m ON m.merch_uuid = p.merch_uuid
|
JOIN merch as m ON m.merch_uuid = p.merch_uuid
|
||||||
WHERE p.deleted_at IS NULL
|
WHERE p.deleted_at IS NULL
|
||||||
|
AND m.deleted_at IS NULL
|
||||||
AND m.user_uuid = ?)
|
AND m.user_uuid = ?)
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
created_at, merch_uuid, origin, name
|
id, created_at, merch_uuid, origin, name
|
||||||
FROM price_with_neighbors
|
FROM price_with_neighbors
|
||||||
WHERE
|
WHERE
|
||||||
price = 0
|
price = 0
|
||||||
|
|
@ -343,14 +362,13 @@ func (r *Repo) getZeroPrices(userUuid string) ([]ZeroPrice, error) {
|
||||||
return priceList, nil
|
return priceList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repo) deleteZeroPrices(userUuid string, list []string) error {
|
func (r *Repo) deleteZeroPrices(list []DeleteZeroPrices) error {
|
||||||
subQuery := r.db.Table("merch").
|
for _, item := range list {
|
||||||
Select("merch_uuid").
|
if err := r.db.Model(&Price{}).
|
||||||
Where("user_uuid = ?", userUuid)
|
Where("id = ? AND merch_uuid = ?", item.Id, item.MerchUuid).
|
||||||
|
Update("deleted_at", time.Now().UTC()).Error; err != nil {
|
||||||
return r.db.Model(&Price{}).
|
return err
|
||||||
Where("merch_uuid IN ?", list).
|
}
|
||||||
Where("merch_uuid IN (?)", subQuery).
|
}
|
||||||
Update("deleted_at", time.Now().UTC()).
|
return nil
|
||||||
Error
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import (
|
||||||
is "merch-parser-api/proto/imageStorage"
|
is "merch-parser-api/proto/imageStorage"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -617,6 +618,26 @@ func (s *service) getZeroPrices(userUuid string) ([]ZeroPrice, error) {
|
||||||
return s.repo.getZeroPrices(userUuid)
|
return s.repo.getZeroPrices(userUuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) deleteZeroPrices(userUuid string, list DeleteZeroPrices) error {
|
func (s *service) deleteZeroPrices(userUuid string, list []DeleteZeroPrices) error {
|
||||||
return s.repo.deleteZeroPrices(userUuid, list.MerchUuids)
|
if len(list) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ids := make([]string, 0, len(list))
|
||||||
|
for _, item := range list {
|
||||||
|
ids = append(ids, item.MerchUuid)
|
||||||
|
fmt.Println(item.MerchUuid, ids)
|
||||||
|
}
|
||||||
|
slices.Compact(ids)
|
||||||
|
|
||||||
|
owns, err := s.repo.userOwnsMerchUuids(userUuid, ids)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !owns {
|
||||||
|
return errors.New("wrong ids")
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.repo.deleteZeroPrices(list)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue