From aeb5cb819b0d0bfcd192e9c13d3951946017b7dc Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 4 Nov 2025 16:49:04 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81hanged=20ownership=20validation=20for=20u?= =?UTF-8?q?ser's=20merch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/merch/repository.go | 21 ++++++++++---------- internal/api/merch/service.go | 33 ++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 711b731..e84f551 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -3,7 +3,6 @@ package merch import ( "database/sql" "errors" - "fmt" "gorm.io/gorm" "gorm.io/gorm/clause" "time" @@ -23,7 +22,7 @@ type repository interface { addMerch(bundle merchBundle) error merchRecordExists(userUuid, merchUuid string) (bool, error) - userOwnsMerchUuids(userUuid string, merchUuids []string) (bool, error) + userOwnsMerchUuids(userUuid string, merchUuids []string) ([]Merch, error) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) getAllMerch(userUuid string) ([]ListResponse, error) @@ -86,20 +85,20 @@ func (r *Repo) merchRecordExists(userUuid, merchUuid string) (bool, error) { return exists, err } -func (r *Repo) userOwnsMerchUuids(userUuid string, merchUuids []string) (bool, error) { - var count int64 - +func (r *Repo) userOwnsMerchUuids(userUuid string, merchUuids []string) ([]Merch, error) { + var ownsUuids []Merch err := r.db.Model(&Merch{}). + Select("merch_uuid"). Where("user_uuid = ?", userUuid). - Where("merch_uuid IN ?", merchUuids). + Where("merch_uuid IN (?)", merchUuids). Where("deleted_at IS NULL"). - Count(&count).Error - fmt.Println("!!!!!!", count) + Find(&ownsUuids).Error + if err != nil { - return false, err + return nil, err } - fmt.Println("!!!!!!", len(merchUuids)) - return count == int64(len(merchUuids)), nil + + return ownsUuids, nil } func (r *Repo) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) { diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 4d06a37..d409b71 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -16,7 +16,6 @@ import ( is "merch-parser-api/proto/imageStorage" "mime/multipart" "path/filepath" - "slices" "strings" "time" ) @@ -619,6 +618,7 @@ func (s *service) getZeroPrices(userUuid string) ([]ZeroPrice, error) { } func (s *service) deleteZeroPrices(userUuid string, list []DeleteZeroPrices) error { + const delMsg = "Merch - service | Delete zero prices" if len(list) == 0 { return nil } @@ -626,18 +626,39 @@ func (s *service) deleteZeroPrices(userUuid string, list []DeleteZeroPrices) err 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) + uniqueMap := make(map[string]struct{}, len(list)) + uniqueIds := make([]string, 0, len(list)) + for _, id := range ids { + if _, ok := uniqueMap[id]; !ok { + uniqueMap[id] = struct{}{} + uniqueIds = append(uniqueIds, id) + } + } + + log.WithField("uuid count", len(uniqueIds)).Debug(delMsg) + + owns, err := s.repo.userOwnsMerchUuids(userUuid, uniqueIds) if err != nil { return err } - if !owns { + if len(owns) < 1 { return errors.New("wrong ids") } - return s.repo.deleteZeroPrices(list) + ownsMap := make(map[string]struct{}, len(owns)) + for _, own := range owns { + ownsMap[own.MerchUuid] = struct{}{} + } + + toDelete := make([]DeleteZeroPrices, 0, len(owns)) + for _, item := range list { + if _, ok := ownsMap[item.MerchUuid]; ok { + toDelete = append(toDelete, item) + } + } + + return s.repo.deleteZeroPrices(toDelete) }