From 7dd4a6830e55a40eec3b55b8509599891a2be2b2 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 30 Sep 2025 18:47:23 +0300 Subject: [PATCH 01/87] release option fix --- internal/router/handler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/router/handler.go b/internal/router/handler.go index b197ec3..70e882a 100644 --- a/internal/router/handler.go +++ b/internal/router/handler.go @@ -28,8 +28,13 @@ type Deps struct { } func NewRouter(deps Deps) interfaces.Router { - engine := gin.Default() + if deps.GinMode == "release" { + gin.SetMode(gin.ReleaseMode) + } else { + gin.SetMode(gin.DebugMode) + } + engine := gin.Default() if deps.GinMode == "release" { gin.SetMode(gin.ReleaseMode) err := engine.SetTrustedProxies([]string{"172.20.0.0/16"}) From a4097706de7e5eb8198b51189841c631dfad626b Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 1 Oct 2025 11:00:39 +0300 Subject: [PATCH 02/87] merch update method refactor --- internal/api/merch/controller.go | 10 ++++----- internal/api/merch/dto.go | 7 ++++++ internal/api/merch/model.go | 9 ++++---- internal/api/merch/repository.go | 37 +++++++++++++++----------------- internal/api/merch/service.go | 13 +++++++++-- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 2e31a66..2b23c34 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -134,16 +134,16 @@ func (co *controller) getAllMerch(c *gin.Context) { // @Description Обновить информацию про мерч по его uuid в json-е // @Tags Merch // @Security BearerAuth -// @Param body body MerchDTO true "merch_uuid" -// @Success 200 {object} MerchDTO +// @Param body body UpdateMerchDTO true "merch_uuid" +// @Success 200 // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 -// @Router /merch/{uuid} [put] +// @Router /merch/ [put] func (co *controller) updateMerch(c *gin.Context) { - var payload MerchDTO + var payload UpdateMerchDTO 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") + log.WithError(err).Error("Merch | Failed to bind JSON on update merch") return } diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index cce0e03..6e872a2 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -46,3 +46,10 @@ type PricesResponse struct { MerchUuid string `json:"merch_uuid"` Origins []OriginWithPrices `json:"origins"` } + +type UpdateMerchDTO struct { + MerchUuid string `json:"merch_uuid"` + Name string `json:"name"` + Origin string `json:"origin"` + Link string `json:"link"` +} diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index ed7e550..893fb25 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -20,11 +20,10 @@ func (Merch) TableName() string { } type Surugaya struct { - Id uint `gorm:"primary_key" json:"-"` - DeletedAt sql.NullTime `json:"-"` - MerchUuid string `json:"-"` - Link string `json:"link"` - CookieValues string `json:"cookie_values"` + Id uint `gorm:"primary_key" json:"-"` + DeletedAt sql.NullTime `json:"-"` + MerchUuid string `json:"-"` + Link string `json:"link"` } func (Surugaya) TableName() string { diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index e7a17e9..9639599 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -23,7 +23,7 @@ type repository interface { getSingleMerch(userUuid, merchUuid string) (merchBundle, error) getAllMerch(userUuid string) ([]ListResponse, error) - updateMerch(payload MerchDTO, userUuid string) error + updateMerch(payload UpdateMerchDTO, userUuid string) error deleteMerch(userUuid, merchUuid string) error @@ -99,7 +99,7 @@ func (r *Repo) getAllMerch(userUuid string) ([]ListResponse, error) { return list, nil } -func (r *Repo) updateMerch(payload MerchDTO, userUuid string) error { +func (r *Repo) updateMerch(payload UpdateMerchDTO, userUuid string) error { m := make(map[string]any) m["name"] = payload.Name m["updated_at"] = sql.NullTime{ @@ -115,27 +115,24 @@ func (r *Repo) updateMerch(payload MerchDTO, userUuid string) error { return err } - // surugaya - fields := make(map[string]any, 2) - if payload.OriginSurugaya.Link != "" { - fields["link"] = payload.OriginSurugaya.Link - } - - if len(fields) > 0 { - if err := r.db. - Model(&Surugaya{}). - Where("merch_uuid = ?", payload.MerchUuid). - Updates(fields).Error; err != nil { + switch payload.Origin { + case "surugaya": + var recordSurugaya Surugaya + err := r.db.Where("merch_uuid = ?", payload.MerchUuid).FirstOrCreate(&recordSurugaya, Surugaya{ + MerchUuid: payload.MerchUuid, + Link: payload.Link, + }).Error + if err != nil { return err } - } - // mandarake - if payload.OriginMandarake.Link != "" { - if err := r.db. - Model(&Mandarake{}). - Where("merch_uuid = ?", payload.MerchUuid). - Update("link", payload.OriginMandarake.Link).Error; err != nil { + case "mandarake": + var recordMandarake Mandarake + err := r.db.Where("merch_uuid = ?", payload.MerchUuid).FirstOrCreate(&recordMandarake, Mandarake{ + MerchUuid: payload.MerchUuid, + Link: payload.Link, + }).Error + if err != nil { return err } } diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 0e17c8d..bbc5175 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -67,10 +67,19 @@ func (s *service) getAllMerch(userUuid string) ([]ListResponse, error) { return s.repo.getAllMerch(userUuid) } -func (s *service) updateMerch(payload MerchDTO, userUuid string) error { +func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error { if payload.MerchUuid == "" { - return errors.New("no MerchUuid or empty payload") + return errors.New("no merch uuid provided") } + + if payload.Origin == "" { + return errors.New("no origin provided") + } + + if payload.Link == "" { + return errors.New("no link provided") + } + return s.repo.updateMerch(payload, userUuid) } From 4759e7638ce5e1ab12064969ad2cd35a2d134c8a Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 1 Oct 2025 12:13:43 +0300 Subject: [PATCH 03/87] switch update to upsert --- internal/api/merch/repository.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 9639599..61a3bc5 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "gorm.io/gorm" + "gorm.io/gorm/clause" "time" ) @@ -117,22 +118,18 @@ func (r *Repo) updateMerch(payload UpdateMerchDTO, userUuid string) error { switch payload.Origin { case "surugaya": - var recordSurugaya Surugaya - err := r.db.Where("merch_uuid = ?", payload.MerchUuid).FirstOrCreate(&recordSurugaya, Surugaya{ + if err := r.upsertOrigin(&Surugaya{ MerchUuid: payload.MerchUuid, Link: payload.Link, - }).Error - if err != nil { + }); err != nil { return err } case "mandarake": - var recordMandarake Mandarake - err := r.db.Where("merch_uuid = ?", payload.MerchUuid).FirstOrCreate(&recordMandarake, Mandarake{ + if err := r.upsertOrigin(&Mandarake{ MerchUuid: payload.MerchUuid, Link: payload.Link, - }).Error - if err != nil { + }); err != nil { return err } } @@ -220,3 +217,10 @@ func (r *Repo) getDistinctPrices(userUuid, merchUuid string, period time.Time) ( } return prices, nil } + +func (r *Repo) upsertOrigin(model any) error { + return r.db.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "merch_uuid"}}, + DoUpdates: clause.AssignmentColumns([]string{"link"}), + }).Create(model).Error +} From 2ada5e5a9eae515ac6f0ed99c3b93899ce12187b Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 1 Oct 2025 19:31:58 +0300 Subject: [PATCH 04/87] update --- go.mod | 4 +++- go.sum | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9091402..5a94d40 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,8 @@ require ( github.com/swaggo/gin-swagger v1.6.1 github.com/swaggo/swag v1.16.6 golang.org/x/crypto v0.42.0 + google.golang.org/grpc v1.75.1 + google.golang.org/protobuf v1.36.9 gorm.io/driver/postgres v1.6.0 gorm.io/gorm v1.31.0 ) @@ -66,5 +68,5 @@ require ( golang.org/x/sys v0.36.0 // indirect golang.org/x/text v0.29.0 // indirect golang.org/x/tools v0.37.0 // indirect - google.golang.org/protobuf v1.36.9 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20250929231259-57b25ae835d4 // indirect ) diff --git a/go.sum b/go.sum index b4a9634..b2b1730 100644 --- a/go.sum +++ b/go.sum @@ -173,6 +173,12 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE= golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 h1:pFyd6EwwL2TqFf8emdthzeX+gZE1ElRq3iM8pui4KBY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250929231259-57b25ae835d4 h1:i8QOKZfYg6AbGVZzUAY3LrNWCKF8O6zFisU9Wl9RER4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250929231259-57b25ae835d4/go.mod h1:HSkG/KdJWusxU1F6CNrwNDjBMgisKxGnc5dAZfT0mjQ= +google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI= +google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ= google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 850fb38e160bb4d9c73cf4f667d4f070b8bf63c5 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 1 Oct 2025 19:32:06 +0300 Subject: [PATCH 05/87] removed --- internal/shared/routes.go | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 internal/shared/routes.go diff --git a/internal/shared/routes.go b/internal/shared/routes.go deleted file mode 100644 index 28aea9d..0000000 --- a/internal/shared/routes.go +++ /dev/null @@ -1,6 +0,0 @@ -package shared - -type ExcludeRoute struct { - Route string - Method string -} From 5e1017df69ac8cf742387e0a4646eb04169bc08c Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 1 Oct 2025 19:32:27 +0300 Subject: [PATCH 06/87] created --- proto/task.proto | 41 +++ proto/taskProcessor/task.pb.go | 409 ++++++++++++++++++++++++++++ proto/taskProcessor/task_grpc.pb.go | 195 +++++++++++++ 3 files changed, 645 insertions(+) create mode 100644 proto/task.proto create mode 100644 proto/taskProcessor/task.pb.go create mode 100644 proto/taskProcessor/task_grpc.pb.go diff --git a/proto/task.proto b/proto/task.proto new file mode 100644 index 0000000..bb17f67 --- /dev/null +++ b/proto/task.proto @@ -0,0 +1,41 @@ +syntax = "proto3"; +import "google/protobuf/empty.proto"; + +package taskProcessor; +option go_package = "./taskProcessor"; + + +message Task{ + string merch_uuid = 1; + string origin_surugaya_link = 2; + string origin_mandarake_link = 3; +} + +message Result{ + string merch_uuid = 1; + string origin_name = 2; + uint32 price = 3; +} + +message ProcessorStatusRequest{} + +message ProcessorStatusResponse { + int64 appStart = 1; + int64 lastCheck = 2; + int32 tasksReceived = 3; + int32 tasksInProgress = 4; + int32 tasksFirstTry = 5; + int32 tasksDoneAfterRetry = 6; + int32 tasksFailed = 7; + string workStatus = 8; + int32 numCPUs = 9; + int32 checkPeriod = 10; + int32 retriesCount = 11; + int32 retriesMinutes = 12; +} + +service TaskProcessor { + rpc RequestTask(google.protobuf.Empty) returns (stream Task); + rpc SendResult(stream Result) returns (google.protobuf.Empty); + rpc ProcessorStatus(ProcessorStatusRequest) returns (ProcessorStatusResponse); +} \ No newline at end of file diff --git a/proto/taskProcessor/task.pb.go b/proto/taskProcessor/task.pb.go new file mode 100644 index 0000000..7ff9f0c --- /dev/null +++ b/proto/taskProcessor/task.pb.go @@ -0,0 +1,409 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.8 +// protoc v6.32.0 +// source: task.proto + +package taskProcessor + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Task struct { + state protoimpl.MessageState `protogen:"open.v1"` + MerchUuid string `protobuf:"bytes,1,opt,name=merch_uuid,json=merchUuid,proto3" json:"merch_uuid,omitempty"` + OriginSurugayaLink string `protobuf:"bytes,2,opt,name=origin_surugaya_link,json=originSurugayaLink,proto3" json:"origin_surugaya_link,omitempty"` + OriginMandarakeLink string `protobuf:"bytes,3,opt,name=origin_mandarake_link,json=originMandarakeLink,proto3" json:"origin_mandarake_link,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Task) Reset() { + *x = Task{} + mi := &file_task_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Task) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Task) ProtoMessage() {} + +func (x *Task) ProtoReflect() protoreflect.Message { + mi := &file_task_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Task.ProtoReflect.Descriptor instead. +func (*Task) Descriptor() ([]byte, []int) { + return file_task_proto_rawDescGZIP(), []int{0} +} + +func (x *Task) GetMerchUuid() string { + if x != nil { + return x.MerchUuid + } + return "" +} + +func (x *Task) GetOriginSurugayaLink() string { + if x != nil { + return x.OriginSurugayaLink + } + return "" +} + +func (x *Task) GetOriginMandarakeLink() string { + if x != nil { + return x.OriginMandarakeLink + } + return "" +} + +type Result struct { + state protoimpl.MessageState `protogen:"open.v1"` + MerchUuid string `protobuf:"bytes,1,opt,name=merch_uuid,json=merchUuid,proto3" json:"merch_uuid,omitempty"` + OriginName string `protobuf:"bytes,2,opt,name=origin_name,json=originName,proto3" json:"origin_name,omitempty"` + Price uint32 `protobuf:"varint,3,opt,name=price,proto3" json:"price,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Result) Reset() { + *x = Result{} + mi := &file_task_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Result) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Result) ProtoMessage() {} + +func (x *Result) ProtoReflect() protoreflect.Message { + mi := &file_task_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Result.ProtoReflect.Descriptor instead. +func (*Result) Descriptor() ([]byte, []int) { + return file_task_proto_rawDescGZIP(), []int{1} +} + +func (x *Result) GetMerchUuid() string { + if x != nil { + return x.MerchUuid + } + return "" +} + +func (x *Result) GetOriginName() string { + if x != nil { + return x.OriginName + } + return "" +} + +func (x *Result) GetPrice() uint32 { + if x != nil { + return x.Price + } + return 0 +} + +type ProcessorStatusRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ProcessorStatusRequest) Reset() { + *x = ProcessorStatusRequest{} + mi := &file_task_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ProcessorStatusRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ProcessorStatusRequest) ProtoMessage() {} + +func (x *ProcessorStatusRequest) ProtoReflect() protoreflect.Message { + mi := &file_task_proto_msgTypes[2] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ProcessorStatusRequest.ProtoReflect.Descriptor instead. +func (*ProcessorStatusRequest) Descriptor() ([]byte, []int) { + return file_task_proto_rawDescGZIP(), []int{2} +} + +type ProcessorStatusResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + AppStart int64 `protobuf:"varint,1,opt,name=appStart,proto3" json:"appStart,omitempty"` + LastCheck int64 `protobuf:"varint,2,opt,name=lastCheck,proto3" json:"lastCheck,omitempty"` + TasksReceived int32 `protobuf:"varint,3,opt,name=tasksReceived,proto3" json:"tasksReceived,omitempty"` + TasksInProgress int32 `protobuf:"varint,4,opt,name=tasksInProgress,proto3" json:"tasksInProgress,omitempty"` + TasksFirstTry int32 `protobuf:"varint,5,opt,name=tasksFirstTry,proto3" json:"tasksFirstTry,omitempty"` + TasksDoneAfterRetry int32 `protobuf:"varint,6,opt,name=tasksDoneAfterRetry,proto3" json:"tasksDoneAfterRetry,omitempty"` + TasksFailed int32 `protobuf:"varint,7,opt,name=tasksFailed,proto3" json:"tasksFailed,omitempty"` + WorkStatus string `protobuf:"bytes,8,opt,name=workStatus,proto3" json:"workStatus,omitempty"` + NumCPUs int32 `protobuf:"varint,9,opt,name=numCPUs,proto3" json:"numCPUs,omitempty"` + CheckPeriod int32 `protobuf:"varint,10,opt,name=checkPeriod,proto3" json:"checkPeriod,omitempty"` + RetriesCount int32 `protobuf:"varint,11,opt,name=retriesCount,proto3" json:"retriesCount,omitempty"` + RetriesMinutes int32 `protobuf:"varint,12,opt,name=retriesMinutes,proto3" json:"retriesMinutes,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ProcessorStatusResponse) Reset() { + *x = ProcessorStatusResponse{} + mi := &file_task_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ProcessorStatusResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ProcessorStatusResponse) ProtoMessage() {} + +func (x *ProcessorStatusResponse) ProtoReflect() protoreflect.Message { + mi := &file_task_proto_msgTypes[3] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ProcessorStatusResponse.ProtoReflect.Descriptor instead. +func (*ProcessorStatusResponse) Descriptor() ([]byte, []int) { + return file_task_proto_rawDescGZIP(), []int{3} +} + +func (x *ProcessorStatusResponse) GetAppStart() int64 { + if x != nil { + return x.AppStart + } + return 0 +} + +func (x *ProcessorStatusResponse) GetLastCheck() int64 { + if x != nil { + return x.LastCheck + } + return 0 +} + +func (x *ProcessorStatusResponse) GetTasksReceived() int32 { + if x != nil { + return x.TasksReceived + } + return 0 +} + +func (x *ProcessorStatusResponse) GetTasksInProgress() int32 { + if x != nil { + return x.TasksInProgress + } + return 0 +} + +func (x *ProcessorStatusResponse) GetTasksFirstTry() int32 { + if x != nil { + return x.TasksFirstTry + } + return 0 +} + +func (x *ProcessorStatusResponse) GetTasksDoneAfterRetry() int32 { + if x != nil { + return x.TasksDoneAfterRetry + } + return 0 +} + +func (x *ProcessorStatusResponse) GetTasksFailed() int32 { + if x != nil { + return x.TasksFailed + } + return 0 +} + +func (x *ProcessorStatusResponse) GetWorkStatus() string { + if x != nil { + return x.WorkStatus + } + return "" +} + +func (x *ProcessorStatusResponse) GetNumCPUs() int32 { + if x != nil { + return x.NumCPUs + } + return 0 +} + +func (x *ProcessorStatusResponse) GetCheckPeriod() int32 { + if x != nil { + return x.CheckPeriod + } + return 0 +} + +func (x *ProcessorStatusResponse) GetRetriesCount() int32 { + if x != nil { + return x.RetriesCount + } + return 0 +} + +func (x *ProcessorStatusResponse) GetRetriesMinutes() int32 { + if x != nil { + return x.RetriesMinutes + } + return 0 +} + +var File_task_proto protoreflect.FileDescriptor + +const file_task_proto_rawDesc = "" + + "\n" + + "\n" + + "task.proto\x12\rtaskProcessor\x1a\x1bgoogle/protobuf/empty.proto\"\x8b\x01\n" + + "\x04Task\x12\x1d\n" + + "\n" + + "merch_uuid\x18\x01 \x01(\tR\tmerchUuid\x120\n" + + "\x14origin_surugaya_link\x18\x02 \x01(\tR\x12originSurugayaLink\x122\n" + + "\x15origin_mandarake_link\x18\x03 \x01(\tR\x13originMandarakeLink\"^\n" + + "\x06Result\x12\x1d\n" + + "\n" + + "merch_uuid\x18\x01 \x01(\tR\tmerchUuid\x12\x1f\n" + + "\vorigin_name\x18\x02 \x01(\tR\n" + + "originName\x12\x14\n" + + "\x05price\x18\x03 \x01(\rR\x05price\"\x18\n" + + "\x16ProcessorStatusRequest\"\xc5\x03\n" + + "\x17ProcessorStatusResponse\x12\x1a\n" + + "\bappStart\x18\x01 \x01(\x03R\bappStart\x12\x1c\n" + + "\tlastCheck\x18\x02 \x01(\x03R\tlastCheck\x12$\n" + + "\rtasksReceived\x18\x03 \x01(\x05R\rtasksReceived\x12(\n" + + "\x0ftasksInProgress\x18\x04 \x01(\x05R\x0ftasksInProgress\x12$\n" + + "\rtasksFirstTry\x18\x05 \x01(\x05R\rtasksFirstTry\x120\n" + + "\x13tasksDoneAfterRetry\x18\x06 \x01(\x05R\x13tasksDoneAfterRetry\x12 \n" + + "\vtasksFailed\x18\a \x01(\x05R\vtasksFailed\x12\x1e\n" + + "\n" + + "workStatus\x18\b \x01(\tR\n" + + "workStatus\x12\x18\n" + + "\anumCPUs\x18\t \x01(\x05R\anumCPUs\x12 \n" + + "\vcheckPeriod\x18\n" + + " \x01(\x05R\vcheckPeriod\x12\"\n" + + "\fretriesCount\x18\v \x01(\x05R\fretriesCount\x12&\n" + + "\x0eretriesMinutes\x18\f \x01(\x05R\x0eretriesMinutes2\xee\x01\n" + + "\rTaskProcessor\x12<\n" + + "\vRequestTask\x12\x16.google.protobuf.Empty\x1a\x13.taskProcessor.Task0\x01\x12=\n" + + "\n" + + "SendResult\x12\x15.taskProcessor.Result\x1a\x16.google.protobuf.Empty(\x01\x12`\n" + + "\x0fProcessorStatus\x12%.taskProcessor.ProcessorStatusRequest\x1a&.taskProcessor.ProcessorStatusResponseB\x11Z\x0f./taskProcessorb\x06proto3" + +var ( + file_task_proto_rawDescOnce sync.Once + file_task_proto_rawDescData []byte +) + +func file_task_proto_rawDescGZIP() []byte { + file_task_proto_rawDescOnce.Do(func() { + file_task_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_task_proto_rawDesc), len(file_task_proto_rawDesc))) + }) + return file_task_proto_rawDescData +} + +var file_task_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_task_proto_goTypes = []any{ + (*Task)(nil), // 0: taskProcessor.Task + (*Result)(nil), // 1: taskProcessor.Result + (*ProcessorStatusRequest)(nil), // 2: taskProcessor.ProcessorStatusRequest + (*ProcessorStatusResponse)(nil), // 3: taskProcessor.ProcessorStatusResponse + (*emptypb.Empty)(nil), // 4: google.protobuf.Empty +} +var file_task_proto_depIdxs = []int32{ + 4, // 0: taskProcessor.TaskProcessor.RequestTask:input_type -> google.protobuf.Empty + 1, // 1: taskProcessor.TaskProcessor.SendResult:input_type -> taskProcessor.Result + 2, // 2: taskProcessor.TaskProcessor.ProcessorStatus:input_type -> taskProcessor.ProcessorStatusRequest + 0, // 3: taskProcessor.TaskProcessor.RequestTask:output_type -> taskProcessor.Task + 4, // 4: taskProcessor.TaskProcessor.SendResult:output_type -> google.protobuf.Empty + 3, // 5: taskProcessor.TaskProcessor.ProcessorStatus:output_type -> taskProcessor.ProcessorStatusResponse + 3, // [3:6] is the sub-list for method output_type + 0, // [0:3] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_task_proto_init() } +func file_task_proto_init() { + if File_task_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_task_proto_rawDesc), len(file_task_proto_rawDesc)), + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_task_proto_goTypes, + DependencyIndexes: file_task_proto_depIdxs, + MessageInfos: file_task_proto_msgTypes, + }.Build() + File_task_proto = out.File + file_task_proto_goTypes = nil + file_task_proto_depIdxs = nil +} diff --git a/proto/taskProcessor/task_grpc.pb.go b/proto/taskProcessor/task_grpc.pb.go new file mode 100644 index 0000000..487a7d8 --- /dev/null +++ b/proto/taskProcessor/task_grpc.pb.go @@ -0,0 +1,195 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v6.32.0 +// source: task.proto + +package taskProcessor + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + TaskProcessor_RequestTask_FullMethodName = "/taskProcessor.TaskProcessor/RequestTask" + TaskProcessor_SendResult_FullMethodName = "/taskProcessor.TaskProcessor/SendResult" + TaskProcessor_ProcessorStatus_FullMethodName = "/taskProcessor.TaskProcessor/ProcessorStatus" +) + +// TaskProcessorClient is the client API for TaskProcessor service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type TaskProcessorClient interface { + RequestTask(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Task], error) + SendResult(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[Result, emptypb.Empty], error) + ProcessorStatus(ctx context.Context, in *ProcessorStatusRequest, opts ...grpc.CallOption) (*ProcessorStatusResponse, error) +} + +type taskProcessorClient struct { + cc grpc.ClientConnInterface +} + +func NewTaskProcessorClient(cc grpc.ClientConnInterface) TaskProcessorClient { + return &taskProcessorClient{cc} +} + +func (c *taskProcessorClient) RequestTask(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Task], error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + stream, err := c.cc.NewStream(ctx, &TaskProcessor_ServiceDesc.Streams[0], TaskProcessor_RequestTask_FullMethodName, cOpts...) + if err != nil { + return nil, err + } + x := &grpc.GenericClientStream[emptypb.Empty, Task]{ClientStream: stream} + if err := x.ClientStream.SendMsg(in); err != nil { + return nil, err + } + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + return x, nil +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type TaskProcessor_RequestTaskClient = grpc.ServerStreamingClient[Task] + +func (c *taskProcessorClient) SendResult(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[Result, emptypb.Empty], error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + stream, err := c.cc.NewStream(ctx, &TaskProcessor_ServiceDesc.Streams[1], TaskProcessor_SendResult_FullMethodName, cOpts...) + if err != nil { + return nil, err + } + x := &grpc.GenericClientStream[Result, emptypb.Empty]{ClientStream: stream} + return x, nil +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type TaskProcessor_SendResultClient = grpc.ClientStreamingClient[Result, emptypb.Empty] + +func (c *taskProcessorClient) ProcessorStatus(ctx context.Context, in *ProcessorStatusRequest, opts ...grpc.CallOption) (*ProcessorStatusResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(ProcessorStatusResponse) + err := c.cc.Invoke(ctx, TaskProcessor_ProcessorStatus_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// TaskProcessorServer is the server API for TaskProcessor service. +// All implementations must embed UnimplementedTaskProcessorServer +// for forward compatibility. +type TaskProcessorServer interface { + RequestTask(*emptypb.Empty, grpc.ServerStreamingServer[Task]) error + SendResult(grpc.ClientStreamingServer[Result, emptypb.Empty]) error + ProcessorStatus(context.Context, *ProcessorStatusRequest) (*ProcessorStatusResponse, error) + mustEmbedUnimplementedTaskProcessorServer() +} + +// UnimplementedTaskProcessorServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedTaskProcessorServer struct{} + +func (UnimplementedTaskProcessorServer) RequestTask(*emptypb.Empty, grpc.ServerStreamingServer[Task]) error { + return status.Errorf(codes.Unimplemented, "method RequestTask not implemented") +} +func (UnimplementedTaskProcessorServer) SendResult(grpc.ClientStreamingServer[Result, emptypb.Empty]) error { + return status.Errorf(codes.Unimplemented, "method SendResult not implemented") +} +func (UnimplementedTaskProcessorServer) ProcessorStatus(context.Context, *ProcessorStatusRequest) (*ProcessorStatusResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ProcessorStatus not implemented") +} +func (UnimplementedTaskProcessorServer) mustEmbedUnimplementedTaskProcessorServer() {} +func (UnimplementedTaskProcessorServer) testEmbeddedByValue() {} + +// UnsafeTaskProcessorServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to TaskProcessorServer will +// result in compilation errors. +type UnsafeTaskProcessorServer interface { + mustEmbedUnimplementedTaskProcessorServer() +} + +func RegisterTaskProcessorServer(s grpc.ServiceRegistrar, srv TaskProcessorServer) { + // If the following call pancis, it indicates UnimplementedTaskProcessorServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&TaskProcessor_ServiceDesc, srv) +} + +func _TaskProcessor_RequestTask_Handler(srv interface{}, stream grpc.ServerStream) error { + m := new(emptypb.Empty) + if err := stream.RecvMsg(m); err != nil { + return err + } + return srv.(TaskProcessorServer).RequestTask(m, &grpc.GenericServerStream[emptypb.Empty, Task]{ServerStream: stream}) +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type TaskProcessor_RequestTaskServer = grpc.ServerStreamingServer[Task] + +func _TaskProcessor_SendResult_Handler(srv interface{}, stream grpc.ServerStream) error { + return srv.(TaskProcessorServer).SendResult(&grpc.GenericServerStream[Result, emptypb.Empty]{ServerStream: stream}) +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type TaskProcessor_SendResultServer = grpc.ClientStreamingServer[Result, emptypb.Empty] + +func _TaskProcessor_ProcessorStatus_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ProcessorStatusRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TaskProcessorServer).ProcessorStatus(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: TaskProcessor_ProcessorStatus_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TaskProcessorServer).ProcessorStatus(ctx, req.(*ProcessorStatusRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// TaskProcessor_ServiceDesc is the grpc.ServiceDesc for TaskProcessor service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var TaskProcessor_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "taskProcessor.TaskProcessor", + HandlerType: (*TaskProcessorServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "ProcessorStatus", + Handler: _TaskProcessor_ProcessorStatus_Handler, + }, + }, + Streams: []grpc.StreamDesc{ + { + StreamName: "RequestTask", + Handler: _TaskProcessor_RequestTask_Handler, + ServerStreams: true, + }, + { + StreamName: "SendResult", + Handler: _TaskProcessor_SendResult_Handler, + ClientStreams: true, + }, + }, + Metadata: "task.proto", +} From 6867d2d74e3fefd1b667089cee733b4c407223eb Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 1 Oct 2025 19:32:56 +0300 Subject: [PATCH 07/87] grpc server added --- api.env | 3 + cmd/main.go | 18 ++++-- config/config.go | 17 ++++- internal/api/merch/provider.go | 93 ++++++++++++++++++++++++++++ internal/app/handler.go | 54 +++++++++++----- internal/grpcService/handler.go | 106 ++++++++++++++++++++++++++++++++ internal/interfaces/task.go | 8 +++ internal/router/handler.go | 10 ++- internal/shared/task.go | 13 ++++ 9 files changed, 294 insertions(+), 28 deletions(-) create mode 100644 internal/api/merch/provider.go create mode 100644 internal/grpcService/handler.go create mode 100644 internal/interfaces/task.go create mode 100644 internal/shared/task.go diff --git a/api.env b/api.env index 8622ace..843383b 100644 --- a/api.env +++ b/api.env @@ -5,6 +5,9 @@ APP_API_PREFIX=/api/v2 APP_GIN_MODE=development APP_ALLOWED_ORIGINS=http://localhost:5173, +GRPC_SERVER_PORT=9050 +GRPC_CLIENT_PORT=9060 + DB_HOST= DB_PORT= DB_USER= diff --git a/cmd/main.go b/cmd/main.go index 16f80b1..f16c4e3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,6 +8,7 @@ import ( "merch-parser-api/internal/api/merch" "merch-parser-api/internal/api/user" "merch-parser-api/internal/app" + "merch-parser-api/internal/grpcService" "merch-parser-api/internal/interfaces" "merch-parser-api/internal/provider/auth" "merch-parser-api/internal/provider/token" @@ -66,6 +67,10 @@ func main() { }) log.Debug("Auth provider initialized") + tasksRepo := merch.NewTaskRepository(database) + tasksProvider := merch.NewTaskProvider(tasksRepo) + grpcServer := grpcService.NewGrpcServer(tasksProvider) + //register app modules users := user.NewHandler(user.Deps{ Auth: authProvider, @@ -87,11 +92,14 @@ func main() { //keep last appl := app.NewApp(app.Deps{ - Host: c.AppConf.Host, - Port: c.AppConf.Port, - ApiPrefix: c.AppConf.ApiPrefix, - RouterHandler: routerHandler, - Modules: modules, + Host: c.AppConf.Host, + Port: c.AppConf.Port, + ApiPrefix: c.AppConf.ApiPrefix, + RouterHandler: routerHandler, + Modules: modules, + GrpcServer: grpcServer, + GrpcServerPort: c.GrpcConf.GrpcServerPort, + GrpcClientPort: c.GrpcConf.GrpcClientPort, }) err = appl.Run(ctx) diff --git a/config/config.go b/config/config.go index 304e051..2646db5 100644 --- a/config/config.go +++ b/config/config.go @@ -3,9 +3,10 @@ package config import "strings" type Config struct { - AppConf AppConfig - DBConf DatabaseConfig - JWTConf JWTConfig + AppConf AppConfig + DBConf DatabaseConfig + JWTConf JWTConfig + GrpcConf GrpcConfig } type AppConfig struct { @@ -34,6 +35,11 @@ type JWTConfig struct { RefreshExpire string } +type GrpcConfig struct { + GrpcServerPort string + GrpcClientPort string +} + func NewConfig() *Config { return &Config{ AppConf: AppConfig{ @@ -61,5 +67,10 @@ func NewConfig() *Config { AccessExpire: getEnv("JWT_ACCESS_EXPIRE", ""), RefreshExpire: getEnv("JWT_REFRESH_EXPIRE", ""), }, + + GrpcConf: GrpcConfig{ + GrpcServerPort: getEnv("GRPC_SERVER_PORT", ""), + GrpcClientPort: getEnv("GRPC_CLIENT_PORT", ""), + }, } } diff --git a/internal/api/merch/provider.go b/internal/api/merch/provider.go new file mode 100644 index 0000000..f856fbd --- /dev/null +++ b/internal/api/merch/provider.go @@ -0,0 +1,93 @@ +package merch + +import ( + "gorm.io/gorm" + "merch-parser-api/internal/shared" +) + +type Link struct { + Surugaya []Surugaya + Mandarake []Mandarake +} + +type TaskProvider struct { + repo TaskRepository +} + +type TaskRepository interface { + GetLinks() (*Link, error) + InsertPrices() error +} + +type TaskRepo struct { + db *gorm.DB +} + +func NewTaskProvider(repo TaskRepository) *TaskProvider { + return &TaskProvider{ + repo: repo, + } +} + +func NewTaskRepository(db *gorm.DB) TaskRepository { + return &TaskRepo{db: db} +} + +func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) { + getLinks, err := p.repo.GetLinks() + if err != nil { + return nil, err + } + + taskMap := make(map[string]shared.Task) + + for _, item := range getLinks.Surugaya { + if task, exists := taskMap[item.MerchUuid]; exists { + task.OriginSurugayaLink = item.Link + taskMap[item.MerchUuid] = task + } else { + taskMap[item.MerchUuid] = shared.Task{ + MerchUuid: item.MerchUuid, + OriginSurugayaLink: item.Link, + } + } + } + + for _, item := range getLinks.Mandarake { + if task, exists := taskMap[item.MerchUuid]; exists { + task.OriginMandarakeLink = item.Link + taskMap[item.MerchUuid] = task + } else { + taskMap[item.MerchUuid] = shared.Task{ + MerchUuid: item.MerchUuid, + OriginMandarakeLink: item.Link, + } + } + } + return taskMap, nil +} + +func (p *TaskProvider) InsertPrices([]shared.TaskResult) error { + return nil +} + +func (r *TaskRepo) GetLinks() (*Link, error) { + var surugayaList []Surugaya + if err := r.db.Model(&Surugaya{}).Find(&surugayaList).Error; err != nil { + return nil, err + } + + var mandarakeList []Mandarake + if err := r.db.Model(&Mandarake{}).Find(&mandarakeList).Error; err != nil { + return nil, err + } + + return &Link{ + Surugaya: surugayaList, + Mandarake: mandarakeList, + }, nil +} + +func (r *TaskRepo) InsertPrices() error { + return nil +} diff --git a/internal/app/handler.go b/internal/app/handler.go index 09de7e0..0164c13 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -4,33 +4,46 @@ import ( "context" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" + "google.golang.org/grpc" "merch-parser-api/internal/interfaces" + "net" "net/http" "time" ) type App struct { - address string - apiPrefix string - modules []interfaces.Module - routerHandler interfaces.Router - router *gin.Engine + host string + address string + apiPrefix string + modules []interfaces.Module + routerHandler interfaces.Router + router *gin.Engine + grpcServer *grpc.Server + grpcServerPort string + grpcClientPort string } type Deps struct { - Host string - Port string - ApiPrefix string - Modules []interfaces.Module - RouterHandler interfaces.Router + Host string + Port string + ApiPrefix string + Modules []interfaces.Module + RouterHandler interfaces.Router + GrpcServer *grpc.Server + GrpcServerPort string + GrpcClientPort string } func NewApp(deps Deps) *App { app := &App{ - address: deps.Host + ":" + deps.Port, - apiPrefix: deps.ApiPrefix, - routerHandler: deps.RouterHandler, - modules: deps.Modules, + host: deps.Host, + address: deps.Host + ":" + deps.Port, + apiPrefix: deps.ApiPrefix, + routerHandler: deps.RouterHandler, + modules: deps.Modules, + grpcServer: deps.GrpcServer, + grpcServerPort: deps.GrpcServerPort, + grpcClientPort: deps.GrpcClientPort, } app.router = app.routerHandler.Set() @@ -62,6 +75,19 @@ func (a *App) Run(ctx context.Context) error { serverErr <- server.ListenAndServe() }() + go func() { + listener, err := net.Listen("tcp", net.JoinHostPort(a.host, a.grpcServerPort)) + if err != nil { + log.WithField("err", err).Fatal("gRPC Server | Listener") + } + + err = a.grpcServer.Serve(listener) + if err != nil { + log.WithField("err", err).Fatal("gRPC Server | Serve") + } + }() + log.Info("Starting gRPC server on port: ", a.grpcServerPort) + select { case <-ctx.Done(): log.Info("Shutting down server") diff --git a/internal/grpcService/handler.go b/internal/grpcService/handler.go new file mode 100644 index 0000000..6af20cf --- /dev/null +++ b/internal/grpcService/handler.go @@ -0,0 +1,106 @@ +package grpcService + +import ( + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "google.golang.org/protobuf/types/known/emptypb" + "io" + "merch-parser-api/internal/interfaces" + "merch-parser-api/internal/shared" + pb "merch-parser-api/proto/taskProcessor" + "time" +) + +type repoServer struct { + pb.UnimplementedTaskProcessorServer + taskProvider interfaces.TaskProvider +} + +func NewGrpcServer(taskProvider interfaces.TaskProvider) *grpc.Server { + srv := grpc.NewServer() + repoSrv := &repoServer{ + taskProvider: taskProvider, + } + + pb.RegisterTaskProcessorServer(srv, repoSrv) + return srv +} + +func (r *repoServer) RequestTask(_ *emptypb.Empty, stream pb.TaskProcessor_RequestTaskServer) error { + tasks, err := r.taskProvider.PrepareTasks() + if err != nil { + log.WithField("err", err).Error("gRPC Server | Request task error") + return err + } + + for _, task := range tasks { + if err = stream.Send(&pb.Task{ + MerchUuid: task.MerchUuid, + OriginSurugayaLink: task.OriginSurugayaLink, + OriginMandarakeLink: task.OriginMandarakeLink, + }); err != nil { + log.WithField("err", err).Error("gRPC Server | Stream send error") + return err + } + } + return nil +} + +func (r *repoServer) SendResult(stream pb.TaskProcessor_SendResultServer) error { + saveInterval := time.Second * 2 + batch := make([]shared.TaskResult, 0) + + ticker := time.NewTicker(saveInterval) + defer ticker.Stop() + + done := make(chan struct{}) + + go func() { + for { + select { + case <-done: + return + case <-ticker.C: + if len(batch) > 0 { + err := r.taskProvider.InsertPrices(batch) + if err != nil { + log.WithField("err", err).Error("gRPC Server | Batch insert") + } + } + } + } + }() + + for { + response, err := stream.Recv() + if err == io.EOF { + log.Debug("gRPC EOF") + break + } + + if err != nil { + log.WithField("err", err).Error("gRPC Server | Receive") + return err + } + + entry := shared.TaskResult{ + MerchUuid: response.MerchUuid, + Origin: response.OriginName, + Price: response.Price, + } + + batch = append(batch, entry) + log.WithField("response", entry).Debug("gRPC Server | Receive success") + } + + close(done) + if len(batch) > 0 { + err := r.taskProvider.InsertPrices(batch) + if err != nil { + log.WithField("err", err).Error("gRPC Server | Last data batch insert") + return err + } + } + + return nil +} diff --git a/internal/interfaces/task.go b/internal/interfaces/task.go new file mode 100644 index 0000000..dd8e299 --- /dev/null +++ b/internal/interfaces/task.go @@ -0,0 +1,8 @@ +package interfaces + +import "merch-parser-api/internal/shared" + +type TaskProvider interface { + PrepareTasks() (map[string]shared.Task, error) + InsertPrices([]shared.TaskResult) error +} diff --git a/internal/router/handler.go b/internal/router/handler.go index 70e882a..328ab93 100644 --- a/internal/router/handler.go +++ b/internal/router/handler.go @@ -7,17 +7,15 @@ import ( swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" "merch-parser-api/internal/interfaces" - "merch-parser-api/internal/shared" "net/http" "time" ) type router struct { - apiPrefix string - engine *gin.Engine - ginMode string - excludeRoutes map[string]shared.ExcludeRoute - tokenProv interfaces.JWTProvider + apiPrefix string + engine *gin.Engine + ginMode string + tokenProv interfaces.JWTProvider } type Deps struct { diff --git a/internal/shared/task.go b/internal/shared/task.go new file mode 100644 index 0000000..119babd --- /dev/null +++ b/internal/shared/task.go @@ -0,0 +1,13 @@ +package shared + +type Task struct { + MerchUuid string + OriginSurugayaLink string + OriginMandarakeLink string +} + +type TaskResult struct { + MerchUuid string + Origin string + Price uint32 +} From e4f55bb19580ea6eecb29b8f9ea409537b3abc67 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 3 Oct 2025 19:14:13 +0300 Subject: [PATCH 08/87] price type change --- internal/shared/task.go | 2 +- proto/task.proto | 4 ++-- proto/taskProcessor/task.pb.go | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/shared/task.go b/internal/shared/task.go index 119babd..b835d83 100644 --- a/internal/shared/task.go +++ b/internal/shared/task.go @@ -9,5 +9,5 @@ type Task struct { type TaskResult struct { MerchUuid string Origin string - Price uint32 + Price int32 } diff --git a/proto/task.proto b/proto/task.proto index bb17f67..35cce81 100644 --- a/proto/task.proto +++ b/proto/task.proto @@ -14,7 +14,7 @@ message Task{ message Result{ string merch_uuid = 1; string origin_name = 2; - uint32 price = 3; + int32 price = 3; } message ProcessorStatusRequest{} @@ -38,4 +38,4 @@ service TaskProcessor { rpc RequestTask(google.protobuf.Empty) returns (stream Task); rpc SendResult(stream Result) returns (google.protobuf.Empty); rpc ProcessorStatus(ProcessorStatusRequest) returns (ProcessorStatusResponse); -} \ No newline at end of file +} diff --git a/proto/taskProcessor/task.pb.go b/proto/taskProcessor/task.pb.go index 7ff9f0c..886cbfa 100644 --- a/proto/taskProcessor/task.pb.go +++ b/proto/taskProcessor/task.pb.go @@ -86,7 +86,7 @@ type Result struct { state protoimpl.MessageState `protogen:"open.v1"` MerchUuid string `protobuf:"bytes,1,opt,name=merch_uuid,json=merchUuid,proto3" json:"merch_uuid,omitempty"` OriginName string `protobuf:"bytes,2,opt,name=origin_name,json=originName,proto3" json:"origin_name,omitempty"` - Price uint32 `protobuf:"varint,3,opt,name=price,proto3" json:"price,omitempty"` + Price int32 `protobuf:"varint,3,opt,name=price,proto3" json:"price,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -135,7 +135,7 @@ func (x *Result) GetOriginName() string { return "" } -func (x *Result) GetPrice() uint32 { +func (x *Result) GetPrice() int32 { if x != nil { return x.Price } @@ -326,7 +326,7 @@ const file_task_proto_rawDesc = "" + "merch_uuid\x18\x01 \x01(\tR\tmerchUuid\x12\x1f\n" + "\vorigin_name\x18\x02 \x01(\tR\n" + "originName\x12\x14\n" + - "\x05price\x18\x03 \x01(\rR\x05price\"\x18\n" + + "\x05price\x18\x03 \x01(\x05R\x05price\"\x18\n" + "\x16ProcessorStatusRequest\"\xc5\x03\n" + "\x17ProcessorStatusResponse\x12\x1a\n" + "\bappStart\x18\x01 \x01(\x03R\bappStart\x12\x1c\n" + From acb8b8d4a030d085e77e17c8c8ab95096e9a7509 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 5 Oct 2025 15:30:34 +0300 Subject: [PATCH 09/87] update --- go.mod | 6 +++--- go.sum | 34 ++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 5a94d40..acd00c9 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/swaggo/swag v1.16.6 golang.org/x/crypto v0.42.0 google.golang.org/grpc v1.75.1 - google.golang.org/protobuf v1.36.9 + google.golang.org/protobuf v1.36.10 gorm.io/driver/postgres v1.6.0 gorm.io/gorm v1.31.0 ) @@ -55,7 +55,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/quic-go/qpack v0.5.1 // indirect - github.com/quic-go/quic-go v0.54.1 // indirect + github.com/quic-go/quic-go v0.55.0 // indirect github.com/rogpeppe/go-internal v1.14.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.0 // indirect @@ -68,5 +68,5 @@ require ( golang.org/x/sys v0.36.0 // indirect golang.org/x/text v0.29.0 // indirect golang.org/x/tools v0.37.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20250929231259-57b25ae835d4 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251002232023-7c0ddcbb5797 // indirect ) diff --git a/go.sum b/go.sum index b2b1730..3563ce4 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,10 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-openapi/jsonpointer v0.22.1 h1:sHYI1He3b9NqJ4wXLoJDKmUmHkWy/L7rtEo92JUxBNk= github.com/go-openapi/jsonpointer v0.22.1/go.mod h1:pQT9OsLkfz1yWoMgYFy4x3U5GY5nUlsOn1qSBH5MkCM= github.com/go-openapi/jsonreference v0.21.2 h1:Wxjda4M/BBQllegefXrY/9aq1fxBA8sI5M/lFU6tSWU= @@ -58,6 +62,8 @@ github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -98,8 +104,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI= github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg= -github.com/quic-go/quic-go v0.54.1 h1:4ZAWm0AhCb6+hE+l5Q1NAL0iRn/ZrMwqHRGQiFwj2eg= -github.com/quic-go/quic-go v0.54.1/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY= +github.com/quic-go/quic-go v0.55.0 h1:zccPQIqYCXDt5NmcEabyYvOnomjs8Tlwl7tISjJh9Mk= +github.com/quic-go/quic-go v0.55.0/go.mod h1:DR51ilwU1uE164KuWXhinFcKWGlEjzys2l8zUl5Ss1U= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -125,6 +131,18 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2 github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ= +go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I= +go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE= +go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E= +go.opentelemetry.io/otel/sdk v1.37.0 h1:ItB0QUqnjesGRvNcmAcU0LyvkVyGJ2xftD29bWdDvKI= +go.opentelemetry.io/otel/sdk v1.37.0/go.mod h1:VredYzxUvuo2q3WRcDnKDjbdvmO0sCzOvVAiY+yUkAg= +go.opentelemetry.io/otel/sdk/metric v1.37.0 h1:90lI228XrB9jCMuSdA0673aubgRobVZFhbjxHHspCPc= +go.opentelemetry.io/otel/sdk/metric v1.37.0/go.mod h1:cNen4ZWfiD37l5NhS+Keb5RXVWZWpRE+9WyVCpbo5ps= +go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4= +go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0= go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= @@ -173,14 +191,14 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE= golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 h1:pFyd6EwwL2TqFf8emdthzeX+gZE1ElRq3iM8pui4KBY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250929231259-57b25ae835d4 h1:i8QOKZfYg6AbGVZzUAY3LrNWCKF8O6zFisU9Wl9RER4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250929231259-57b25ae835d4/go.mod h1:HSkG/KdJWusxU1F6CNrwNDjBMgisKxGnc5dAZfT0mjQ= +gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= +gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251002232023-7c0ddcbb5797 h1:CirRxTOwnRWVLKzDNrs0CXAaVozJoR4G9xvdRecrdpk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251002232023-7c0ddcbb5797/go.mod h1:HSkG/KdJWusxU1F6CNrwNDjBMgisKxGnc5dAZfT0mjQ= google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI= google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ= -google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= -google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= From 6a21576c1c79815ea2e3425173c053796160c011 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 5 Oct 2025 15:31:00 +0300 Subject: [PATCH 10/87] insert prices method --- internal/api/merch/model.go | 2 +- internal/api/merch/origins.go | 19 ++++++++++++++- internal/api/merch/provider.go | 43 ++++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 893fb25..9322547 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -48,5 +48,5 @@ type Price struct { DeletedAt sql.NullTime `json:"deleted_at" gorm:"column:deleted_at"` MerchUuid string `json:"merch_uuid" gorm:"column:merch_uuid"` Price int `json:"price" gorm:"column:price"` - Origin Origin `json:"origin" gorm:"column:origin"` + Origin Origin `json:"origin" gorm:"column:origin;type:integer"` } diff --git a/internal/api/merch/origins.go b/internal/api/merch/origins.go index e29f0dc..22175e5 100644 --- a/internal/api/merch/origins.go +++ b/internal/api/merch/origins.go @@ -1,6 +1,10 @@ package merch -import "encoding/json" +import ( + "database/sql/driver" + "encoding/json" + "strings" +) type Origin int @@ -25,3 +29,16 @@ func (o Origin) String() string { func (o Origin) MarshalJSON() ([]byte, error) { return json.Marshal(o.String()) } + +func parseOrigin(s string) (Origin, bool) { + for i, name := range Origins { + if name == strings.ToLower(s) { + return Origin((i + 1) * 1000), true + } + } + return 0, false +} + +func (o Origin) Value() (driver.Value, error) { + return int(o), nil +} diff --git a/internal/api/merch/provider.go b/internal/api/merch/provider.go index f856fbd..541261a 100644 --- a/internal/api/merch/provider.go +++ b/internal/api/merch/provider.go @@ -1,8 +1,11 @@ package merch import ( + "database/sql" + log "github.com/sirupsen/logrus" "gorm.io/gorm" "merch-parser-api/internal/shared" + "time" ) type Link struct { @@ -15,8 +18,8 @@ type TaskProvider struct { } type TaskRepository interface { - GetLinks() (*Link, error) - InsertPrices() error + getLinks() (*Link, error) + insertPrices(prices []Price) error } type TaskRepo struct { @@ -34,7 +37,7 @@ func NewTaskRepository(db *gorm.DB) TaskRepository { } func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) { - getLinks, err := p.repo.GetLinks() + getLinks, err := p.repo.getLinks() if err != nil { return nil, err } @@ -67,11 +70,37 @@ func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) { return taskMap, nil } -func (p *TaskProvider) InsertPrices([]shared.TaskResult) error { +func (p *TaskProvider) InsertPrices(prices []shared.TaskResult) error { + if len(prices) == 0 { + log.WithField("msg", "no prices received").Debug("Merch provider | Insert prices") + return nil + } + + var insertPrices []Price + for _, item := range prices { + origin, ok := parseOrigin(item.Origin) + if !ok { + continue + } + + insertPrices = append(insertPrices, Price{ + CreatedAt: time.Now().UTC(), + UpdatedAt: sql.NullTime{Time: time.Time{}, Valid: false}, + DeletedAt: sql.NullTime{Time: time.Time{}, Valid: false}, + MerchUuid: item.MerchUuid, + Price: int(item.Price), + Origin: origin, + }) + } + + if err := p.repo.insertPrices(insertPrices); err != nil { + return err + } + return nil } -func (r *TaskRepo) GetLinks() (*Link, error) { +func (r *TaskRepo) getLinks() (*Link, error) { var surugayaList []Surugaya if err := r.db.Model(&Surugaya{}).Find(&surugayaList).Error; err != nil { return nil, err @@ -88,6 +117,6 @@ func (r *TaskRepo) GetLinks() (*Link, error) { }, nil } -func (r *TaskRepo) InsertPrices() error { - return nil +func (r *TaskRepo) insertPrices(prices []Price) error { + return r.db.Model(&Price{}).Create(&prices).Error } From ad880b3fb457eebbdb335c470ce561af4906ca9d Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 5 Oct 2025 16:24:48 +0300 Subject: [PATCH 11/87] where condition added --- internal/api/merch/provider.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/api/merch/provider.go b/internal/api/merch/provider.go index 541261a..ad50e20 100644 --- a/internal/api/merch/provider.go +++ b/internal/api/merch/provider.go @@ -102,12 +102,12 @@ func (p *TaskProvider) InsertPrices(prices []shared.TaskResult) error { func (r *TaskRepo) getLinks() (*Link, error) { var surugayaList []Surugaya - if err := r.db.Model(&Surugaya{}).Find(&surugayaList).Error; err != nil { + if err := r.db.Model(&Surugaya{}).Where("deleted_at IS NULL").Find(&surugayaList).Error; err != nil { return nil, err } var mandarakeList []Mandarake - if err := r.db.Model(&Mandarake{}).Find(&mandarakeList).Error; err != nil { + if err := r.db.Model(&Mandarake{}).Where("deleted_at IS NULL").Find(&mandarakeList).Error; err != nil { return nil, err } From b3df73d595fe854e62ff966186133c21df84d1ad Mon Sep 17 00:00:00 2001 From: nquidox Date: Mon, 6 Oct 2025 20:39:29 +0300 Subject: [PATCH 12/87] log added --- internal/api/merch/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/api/merch/provider.go b/internal/api/merch/provider.go index ad50e20..9a51ca4 100644 --- a/internal/api/merch/provider.go +++ b/internal/api/merch/provider.go @@ -67,6 +67,7 @@ func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) { } } } + log.WithField("data", taskMap).Info("Prepare tasks") return taskMap, nil } From 944d9482fd3b45b49850ef4c3aa7eea53603ed9c Mon Sep 17 00:00:00 2001 From: nquidox Date: Mon, 6 Oct 2025 20:40:10 +0300 Subject: [PATCH 13/87] update --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cddb6d9..3f8b9d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ .idea .env -/config/devConfig.go \ No newline at end of file +/config/devConfig.go +*.sh +!gen-swag.sh +.directory +*.tar From e09a3f1d554e20c8477c489aa50d11929023f66f Mon Sep 17 00:00:00 2001 From: nquidox Date: Mon, 6 Oct 2025 22:27:33 +0300 Subject: [PATCH 14/87] logs --- internal/api/merch/provider.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/api/merch/provider.go b/internal/api/merch/provider.go index 9a51ca4..27ab846 100644 --- a/internal/api/merch/provider.go +++ b/internal/api/merch/provider.go @@ -42,6 +42,11 @@ func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) { return nil, err } + log.WithFields(log.Fields{ + "surugaya links": len(getLinks.Surugaya), + "mandarake links": len(getLinks.Mandarake), + }).Info("gRPC Server | Prepare tasks") + taskMap := make(map[string]shared.Task) for _, item := range getLinks.Surugaya { @@ -67,7 +72,7 @@ func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) { } } } - log.WithField("data", taskMap).Info("Prepare tasks") + log.WithField("data", taskMap).Debug("Prepare tasks") return taskMap, nil } From ff44577015a2688d432e475df5586cb886a4e6f9 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 7 Oct 2025 20:40:31 +0300 Subject: [PATCH 15/87] check for empty link removed --- internal/api/merch/service.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index bbc5175..a0fe00f 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -76,10 +76,6 @@ func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error { return errors.New("no origin provided") } - if payload.Link == "" { - return errors.New("no link provided") - } - return s.repo.updateMerch(payload, userUuid) } From be5e3f6d48d7861c3704c4d3f2fd937b2840525d Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 8 Oct 2025 18:30:31 +0300 Subject: [PATCH 16/87] action update --- .forgejo/workflows/make-image.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 3dd5f21..db349a1 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -26,6 +26,6 @@ jobs: - name: Make image run: | docker buildx build --platform linux/amd64 \ - --tag repo.nqws.ru/merch-tracker/repo-app-v2:latest \ - --tag repo.nqws.ru/merch-tracker/repo-app-v2:${{ env.VERSION }} \ + --tag repo.nqws.ru/merch-tracker/mtv2-repo-app:latest \ + --tag repo.nqws.ru/merch-tracker/mtv2-repo-app:${{ env.VERSION }} \ --push . From bb231b1f9da80f28e3f2888e8b28889a1d24c316 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 16:37:39 +0300 Subject: [PATCH 17/87] action update --- .forgejo/workflows/make-image.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index db349a1..0d17b6a 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -4,12 +4,12 @@ on: - 'v[0-9]+*' env: - IMAGE_NAME: repo-app + IMAGE_NAME: mtv2-repo-app jobs: docker: name: Make image - runs-on: docker + runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 From d49e4ba2d2a2e7b9b335350b172e85a6243e7719 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 17:20:32 +0300 Subject: [PATCH 18/87] action update --- .forgejo/workflows/make-image.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 0d17b6a..bf2ea5c 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -14,6 +14,9 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Registry run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login repo.nqws.ru -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin From 73a7cc9d3a8c6fa33d949759748230c04ebe337c Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 17:28:41 +0300 Subject: [PATCH 19/87] action update --- .forgejo/workflows/make-image.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index bf2ea5c..b78fd39 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -2,6 +2,7 @@ on: push: tags: - 'v[0-9]+*' + workflow_dispatch: env: IMAGE_NAME: mtv2-repo-app From 00e01d5c6f39575ffdc294635c284420aaae5642 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 17:44:06 +0300 Subject: [PATCH 20/87] action update --- .forgejo/workflows/make-image.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index b78fd39..00f24f3 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -12,15 +12,20 @@ jobs: name: Make image runs-on: ubuntu-latest steps: + + - name: Login to Forgejo + uses: docker/login-action@v3 + with: + registry: forge.oxmix.net + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Checkout code uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Login to Docker Registry - run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login repo.nqws.ru -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin - - name: Extract version from tag id: extract_version run: | From 25a7389e631c241114abb0967ae3864afb67edca Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 18:26:51 +0300 Subject: [PATCH 21/87] action update --- .forgejo/workflows/make-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 00f24f3..6422a71 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -16,7 +16,7 @@ jobs: - name: Login to Forgejo uses: docker/login-action@v3 with: - registry: forge.oxmix.net + registry: repo.nqws.ru username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} From ec268439a12b16618619d15917eca41bb6f44d31 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 18:47:21 +0300 Subject: [PATCH 22/87] action update --- .forgejo/workflows/make-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 6422a71..5a210dc 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -10,7 +10,7 @@ env: jobs: docker: name: Make image - runs-on: ubuntu-latest + runs-on: ubuntu-latest:docker://ghcr.io/catthehacker/ubuntu:act-latest steps: - name: Login to Forgejo From 87f4a0bdffcb175529ff7d90f70c0ac1b951685a Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 18:51:34 +0300 Subject: [PATCH 23/87] action update --- .forgejo/workflows/make-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 5a210dc..2ac7017 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -8,7 +8,7 @@ env: IMAGE_NAME: mtv2-repo-app jobs: - docker: + build-and-push: name: Make image runs-on: ubuntu-latest:docker://ghcr.io/catthehacker/ubuntu:act-latest steps: From 56d945c698c4faeff048e570cb837cbc17becd0e Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 19:12:53 +0300 Subject: [PATCH 24/87] action update --- .forgejo/workflows/make-image.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 2ac7017..f1cf577 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -10,9 +10,8 @@ env: jobs: build-and-push: name: Make image - runs-on: ubuntu-latest:docker://ghcr.io/catthehacker/ubuntu:act-latest + runs-on: ubuntu-latest steps: - - name: Login to Forgejo uses: docker/login-action@v3 with: From 5bb7ce6a457528cdba998a47cbf879c644614839 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 19:28:21 +0300 Subject: [PATCH 25/87] action update --- .forgejo/workflows/make-image.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index f1cf577..ca06b9f 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -24,6 +24,8 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + with: + driver: docker - name: Extract version from tag id: extract_version From dcb7a4727fece860e4d55b37f20927069e1cf817 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 9 Oct 2025 19:48:37 +0300 Subject: [PATCH 26/87] action update --- .forgejo/workflows/make-image.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index ca06b9f..f1cf577 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -24,8 +24,6 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - with: - driver: docker - name: Extract version from tag id: extract_version From c668f87c96eb75bfdbcd8202146a7781dbbb9ad0 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 18:19:15 +0300 Subject: [PATCH 27/87] action update --- .forgejo/workflows/make-image.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index f1cf577..858f084 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -22,9 +22,6 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Extract version from tag id: extract_version run: | From ce45f425b6785d15731e304afb042e678f304f2a Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 18:24:22 +0300 Subject: [PATCH 28/87] action update --- .forgejo/workflows/make-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 858f084..c38f948 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -10,7 +10,7 @@ env: jobs: build-and-push: name: Make image - runs-on: ubuntu-latest + runs-on: host steps: - name: Login to Forgejo uses: docker/login-action@v3 From 3a529fbc5c420a880043466d90a91acddff55f04 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 18:30:55 +0300 Subject: [PATCH 29/87] action update --- .forgejo/workflows/make-image.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index c38f948..ecf4f0c 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -14,6 +14,8 @@ jobs: steps: - name: Login to Forgejo uses: docker/login-action@v3 + env: + PATH: /usr/bin:/usr/local/bin:/bin with: registry: repo.nqws.ru username: ${{ secrets.DOCKER_USERNAME }} From f98357f766a36c73bd4df70f929d5d2b42b3e4de Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 18:32:02 +0300 Subject: [PATCH 30/87] action update --- .forgejo/workflows/make-image.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index ecf4f0c..4db8af7 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -20,6 +20,7 @@ jobs: registry: repo.nqws.ru username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} + cli-path: /usr/bin/docker - name: Checkout code uses: actions/checkout@v4 From 831734fe23b136d6df857b92db40678ea9803f28 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 19:14:10 +0300 Subject: [PATCH 31/87] action update --- .forgejo/workflows/make-image.yml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 4db8af7..9f0fa59 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -10,18 +10,8 @@ env: jobs: build-and-push: name: Make image - runs-on: host + runs-on: docker steps: - - name: Login to Forgejo - uses: docker/login-action@v3 - env: - PATH: /usr/bin:/usr/local/bin:/bin - with: - registry: repo.nqws.ru - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - cli-path: /usr/bin/docker - - name: Checkout code uses: actions/checkout@v4 From a8c81f8ee1e41c432c13353ca4bf0a76fc15934a Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 19:36:24 +0300 Subject: [PATCH 32/87] action update --- .forgejo/workflows/make-image.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 9f0fa59..38041f8 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -11,6 +11,9 @@ jobs: build-and-push: name: Make image runs-on: docker + container: + image: docker:latest + options: --privileged -v /var/run/docker.sock:/var/run/docker.sock steps: - name: Checkout code uses: actions/checkout@v4 From aa846682c93d7807091ac714440141d9b3381895 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 19:39:21 +0300 Subject: [PATCH 33/87] action update --- .forgejo/workflows/make-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 38041f8..ab036a3 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -12,7 +12,7 @@ jobs: name: Make image runs-on: docker container: - image: docker:latest + image: node:20-bullseye options: --privileged -v /var/run/docker.sock:/var/run/docker.sock steps: - name: Checkout code From 1b3259a73bf6c1040d0612ef2f90738afcffbe7f Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 20:08:29 +0300 Subject: [PATCH 34/87] action update --- .forgejo/workflows/make-image.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index ab036a3..34ada13 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -10,9 +10,9 @@ env: jobs: build-and-push: name: Make image - runs-on: docker + runs-on: ubuntu-latest container: - image: node:20-bullseye + image: ubuntu-latest options: --privileged -v /var/run/docker.sock:/var/run/docker.sock steps: - name: Checkout code From a75d6240b38ff7d9720ae784febcde1a2c45714e Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 10 Oct 2025 21:17:49 +0300 Subject: [PATCH 35/87] action update --- .forgejo/workflows/make-image.yml | 18 +++++++++++++----- cmd/main.go | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.forgejo/workflows/make-image.yml b/.forgejo/workflows/make-image.yml index 34ada13..e7e5484 100644 --- a/.forgejo/workflows/make-image.yml +++ b/.forgejo/workflows/make-image.yml @@ -11,13 +11,21 @@ jobs: build-and-push: name: Make image runs-on: ubuntu-latest - container: - image: ubuntu-latest - options: --privileged -v /var/run/docker.sock:/var/run/docker.sock + steps: - name: Checkout code uses: actions/checkout@v4 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Forgejo + uses: docker/login-action@v3 + with: + registry: repo.nqws.ru + username: ${{ secrets.MAINTAINER_USERNAME }} + password: ${{ secrets.MAINTAINER_TOKEN }} + - name: Extract version from tag id: extract_version run: | @@ -27,6 +35,6 @@ jobs: - name: Make image run: | docker buildx build --platform linux/amd64 \ - --tag repo.nqws.ru/merch-tracker/mtv2-repo-app:latest \ - --tag repo.nqws.ru/merch-tracker/mtv2-repo-app:${{ env.VERSION }} \ + --tag repo.nqws.ru/${{ github.repository }}:latest \ + --tag repo.nqws.ru/${{ github.repository }}:${{ env.VERSION }} \ --push . diff --git a/cmd/main.go b/cmd/main.go index f16c4e3..462985b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,8 +27,8 @@ import ( func main() { log.Debug("Starting merch-parser-api") //setup config - //c := config.NewConfig() - c := config.DevConfig() + c := config.NewConfig() + //c := config.DevConfig() ctx := context.Background() //log level From 218e7d652fcbfa2d029be28ff4b2ee812c135473 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 12 Oct 2025 14:52:19 +0300 Subject: [PATCH 36/87] update --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 3145ef8..a99e5ed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o main "./cmd" FROM alpine:3.22 From 1a67c02e00abd18c248e1830426ee187a2ce2622 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 19:44:41 +0300 Subject: [PATCH 37/87] created media storage package + interface --- internal/interfaces/mediaStorage.go | 15 +++++++++ internal/mediaStorage/handler.go | 35 +++++++++++++++++++ internal/mediaStorage/service.go | 52 +++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 internal/interfaces/mediaStorage.go create mode 100644 internal/mediaStorage/handler.go create mode 100644 internal/mediaStorage/service.go diff --git a/internal/interfaces/mediaStorage.go b/internal/interfaces/mediaStorage.go new file mode 100644 index 0000000..ff185ed --- /dev/null +++ b/internal/interfaces/mediaStorage.go @@ -0,0 +1,15 @@ +package interfaces + +import ( + "context" + "io" + "net/url" + "time" +) + +type MediaStorage interface { + СreateBucketIfNotExists(bucketName string) error + Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error + Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) + Delete(ctx context.Context, bucket, object string) error +} diff --git a/internal/mediaStorage/handler.go b/internal/mediaStorage/handler.go new file mode 100644 index 0000000..90732d0 --- /dev/null +++ b/internal/mediaStorage/handler.go @@ -0,0 +1,35 @@ +package mediaStorage + +import ( + "fmt" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + log "github.com/sirupsen/logrus" +) + +type Handler struct { + *Service +} + +type Deps struct { + Host string + Port string + User string + Password string +} + +func NewHandler(deps Deps) *Handler { + endpoint := fmt.Sprintf("%s:%s", deps.Host, deps.Port) + minioClient, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(deps.User, deps.Password, ""), + Secure: false, + }) + + if err != nil { + log.WithError(err).Fatal("Media storage | Failed to create minio client") + } + + return &Handler{ + newService(minioClient), + } +} diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go new file mode 100644 index 0000000..1ae0e2b --- /dev/null +++ b/internal/mediaStorage/service.go @@ -0,0 +1,52 @@ +package mediaStorage + +import ( + "context" + "errors" + "fmt" + "github.com/minio/minio-go/v7" + log "github.com/sirupsen/logrus" + "io" + "net/url" + "time" +) + +type Service struct { + client *minio.Client +} + +func newService(client *minio.Client) *Service { + return &Service{ + client: client, + } +} + +func (s *Service) СreateBucketIfNotExists(bucketName string) error { + ctx := context.Background() + err := s.client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{}) + if err != nil { + var minioErr minio.ErrorResponse + if errors.As(err, &minioErr) { + if minioErr.Code == "BucketAlreadyExists" || minioErr.Code == "BucketAlreadyOwnedByYou" { + log.Infof("Media storage | Bucket %s already exists, skipping creation", bucketName) + return nil + } + } + return fmt.Errorf("failed to create bucket: %w", err) + } + log.Infof("Media storage | Bucket %s created successfully", bucketName) + return nil +} + +func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error { + _, err := s.client.PutObject(ctx, bucket, object, reader, size, minio.PutObjectOptions{ContentType: "image/jpeg"}) + return err +} + +func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) { + return s.client.PresignedGetObject(ctx, bucket, object, expires, params) +} + +func (s *Service) Delete(ctx context.Context, bucket, object string) error { + return s.client.RemoveObject(ctx, bucket, object, minio.RemoveObjectOptions{}) +} From 262d02e91526cbedf6db938982a1a12ea79a97b7 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 19:44:52 +0300 Subject: [PATCH 38/87] update --- go.mod | 31 +++++++++++++++++++---------- go.sum | 63 +++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index acd00c9..2a71658 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,13 @@ require ( github.com/gin-gonic/gin v1.11.0 github.com/golang-jwt/jwt/v5 v5.3.0 github.com/google/uuid v1.6.0 + github.com/minio/minio-go/v7 v7.0.95 github.com/sirupsen/logrus v1.9.3 github.com/swaggo/files v1.0.1 github.com/swaggo/gin-swagger v1.6.1 github.com/swaggo/swag v1.16.6 - golang.org/x/crypto v0.42.0 - google.golang.org/grpc v1.75.1 + golang.org/x/crypto v0.43.0 + google.golang.org/grpc v1.76.0 google.golang.org/protobuf v1.36.10 gorm.io/driver/postgres v1.6.0 gorm.io/gorm v1.31.0 @@ -24,8 +25,11 @@ require ( github.com/bytedance/sonic v1.14.1 // indirect github.com/bytedance/sonic/loader v0.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect + github.com/disintegration/imaging v1.6.2 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.10 // indirect github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-ini/ini v1.67.0 // indirect github.com/go-openapi/jsonpointer v0.22.1 // indirect github.com/go-openapi/jsonreference v0.21.2 // indirect github.com/go-openapi/spec v0.22.0 // indirect @@ -38,7 +42,7 @@ require ( github.com/go-openapi/swag/yamlutils v0.25.1 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.27.0 // indirect + github.com/go-playground/validator/v10 v10.28.0 // indirect github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.18.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect @@ -48,25 +52,32 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.18.0 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/minio/crc64nvme v1.0.2 // indirect + github.com/minio/md5-simd v1.1.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/philhofer/fwd v1.2.0 // indirect github.com/quic-go/qpack v0.5.1 // indirect github.com/quic-go/quic-go v0.55.0 // indirect github.com/rogpeppe/go-internal v1.14.1 // indirect + github.com/rs/xid v1.6.0 // indirect + github.com/tinylib/msgp v1.3.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.0 // indirect go.uber.org/mock v0.6.0 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect - golang.org/x/arch v0.21.0 // indirect - golang.org/x/mod v0.28.0 // indirect - golang.org/x/net v0.44.0 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect + golang.org/x/mod v0.29.0 // indirect + golang.org/x/net v0.46.0 // indirect golang.org/x/sync v0.17.0 // indirect - golang.org/x/sys v0.36.0 // indirect - golang.org/x/text v0.29.0 // indirect - golang.org/x/tools v0.37.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20251002232023-7c0ddcbb5797 // indirect + golang.org/x/sys v0.37.0 // indirect + golang.org/x/text v0.30.0 // indirect + golang.org/x/tools v0.38.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff // indirect ) diff --git a/go.sum b/go.sum index 3563ce4..5aafef6 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,10 @@ github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gE github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0= github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQY= @@ -21,6 +25,8 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= @@ -54,8 +60,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4= -github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo= +github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688= +github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= @@ -83,6 +89,9 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -93,6 +102,12 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg= +github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU= +github.com/minio/minio-go/v7 v7.0.95/go.mod h1:wOOX3uxS334vImCNRVyIDdXX9OsXDm89ToynKgqUKlo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -100,6 +115,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM= +github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI= @@ -108,6 +125,8 @@ github.com/quic-go/quic-go v0.55.0 h1:zccPQIqYCXDt5NmcEabyYvOnomjs8Tlwl7tISjJh9M github.com/quic-go/quic-go v0.55.0/go.mod h1:DR51ilwU1uE164KuWXhinFcKWGlEjzys2l8zUl5Ss1U= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -126,6 +145,8 @@ github.com/swaggo/gin-swagger v1.6.1 h1:Ri06G4gc9N4t4k8hekMigJ9zKTFSlqj/9paAQCQs github.com/swaggo/gin-swagger v1.6.1/go.mod h1:LQ+hJStHakCWRiK/YNYtJOu4mR2FP+pxLnILT/qNiTw= github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI= github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg= +github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww= +github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= @@ -147,21 +168,23 @@ go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= -golang.org/x/arch v0.21.0 h1:iTC9o7+wP6cPWpDWkivCvQFGAHDQ59SrSxsLPcnkArw= -golang.org/x/arch v0.21.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI= -golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8= +golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= +golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U= -golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI= +golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA= +golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I= -golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= +golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4= +golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= @@ -174,8 +197,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= -golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= +golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -183,20 +206,20 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk= -golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4= +golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= +golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE= -golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w= +golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ= +golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251002232023-7c0ddcbb5797 h1:CirRxTOwnRWVLKzDNrs0CXAaVozJoR4G9xvdRecrdpk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251002232023-7c0ddcbb5797/go.mod h1:HSkG/KdJWusxU1F6CNrwNDjBMgisKxGnc5dAZfT0mjQ= -google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI= -google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff h1:A90eA31Wq6HOMIQlLfzFwzqGKBTuaVztYu/g8sn+8Zc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= +google.golang.org/grpc v1.76.0 h1:UnVkv1+uMLYXoIz6o7chp59WfQUYA2ex/BXQ9rHZu7A= +google.golang.org/grpc v1.76.0/go.mod h1:Ju12QI8M6iQJtbcsV+awF5a4hfJMLi4X0JLo94ULZ6c= google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From e708c92d18dcbb2b1d18893df2f0746f953982ad Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 19:45:49 +0300 Subject: [PATCH 39/87] media storage config + env --- api.env | 5 +++++ cmd/main.go | 17 +++++++++++++++-- config/config.go | 23 +++++++++++++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/api.env b/api.env index 843383b..0a477e8 100644 --- a/api.env +++ b/api.env @@ -8,6 +8,11 @@ APP_ALLOWED_ORIGINS=http://localhost:5173, GRPC_SERVER_PORT=9050 GRPC_CLIENT_PORT=9060 +MEDIA_STORAGE_USER= +MEDIA_STORAGE_PASS= +MEDIA_STORAGE_HOST= +MEDIA_STORAGE_PORT= + DB_HOST= DB_PORT= DB_USER= diff --git a/cmd/main.go b/cmd/main.go index 462985b..ddea055 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,6 +10,7 @@ import ( "merch-parser-api/internal/app" "merch-parser-api/internal/grpcService" "merch-parser-api/internal/interfaces" + "merch-parser-api/internal/mediaStorage" "merch-parser-api/internal/provider/auth" "merch-parser-api/internal/provider/token" "merch-parser-api/internal/router" @@ -27,8 +28,8 @@ import ( func main() { log.Debug("Starting merch-parser-api") //setup config - c := config.NewConfig() - //c := config.DevConfig() + //c := config.NewConfig() + c := config.DevConfig() ctx := context.Background() //log level @@ -51,6 +52,17 @@ func main() { utilsProvider := utils.NewUtils() log.Debug("Utils provider initialized") + mediaProvider := mediaStorage.NewHandler(mediaStorage.Deps{ + Host: c.MediaConf.Host, + Port: c.MediaConf.Port, + User: c.MediaConf.User, + Password: c.MediaConf.Password, + }) + log.WithFields(log.Fields{ + "address": c.MediaConf.Host + ":" + c.MediaConf.Port, + "provider": mediaProvider, + }).Debug("Media storage | Minio client created") + //deps providers routerHandler := router.NewRouter(router.Deps{ ApiPrefix: c.AppConf.ApiPrefix, @@ -82,6 +94,7 @@ func main() { merchModule := merch.NewHandler(merch.Deps{ DB: database, Utils: utilsProvider, + Media: mediaProvider, }) //collect modules diff --git a/config/config.go b/config/config.go index 2646db5..d57e264 100644 --- a/config/config.go +++ b/config/config.go @@ -3,10 +3,11 @@ package config import "strings" type Config struct { - AppConf AppConfig - DBConf DatabaseConfig - JWTConf JWTConfig - GrpcConf GrpcConfig + AppConf AppConfig + DBConf DatabaseConfig + JWTConf JWTConfig + GrpcConf GrpcConfig + MediaConf MediaConfig } type AppConfig struct { @@ -40,6 +41,13 @@ type GrpcConfig struct { GrpcClientPort string } +type MediaConfig struct { + Host string + Port string + User string + Password string +} + func NewConfig() *Config { return &Config{ AppConf: AppConfig{ @@ -72,5 +80,12 @@ func NewConfig() *Config { GrpcServerPort: getEnv("GRPC_SERVER_PORT", ""), GrpcClientPort: getEnv("GRPC_CLIENT_PORT", ""), }, + + MediaConf: MediaConfig{ + Host: getEnv("MEDIA_STORAGE_HOST", ""), + Port: getEnv("MEDIA_STORAGE_PORT", ""), + User: getEnv("MEDIA_STORAGE_USER", ""), + Password: getEnv("MEDIA_STORAGE_PASSWORD", ""), + }, } } From dec89435a3dbd3879b5985d317864a5c95a107d0 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 19:46:10 +0300 Subject: [PATCH 40/87] merch images crud --- internal/api/merch/controller.go | 159 ++++++++++++++++++++++-- internal/api/merch/dto.go | 4 + internal/api/merch/handler.go | 19 ++- internal/api/merch/repository.go | 14 +++ internal/api/merch/service.go | 204 ++++++++++++++++++++++++++++++- 5 files changed, 387 insertions(+), 13 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 2b23c34..f0d8e8b 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -1,23 +1,27 @@ package merch import ( + "context" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-parser-api/internal/interfaces" "merch-parser-api/pkg/responses" "net/http" "strings" + "time" ) type controller struct { service *service utils interfaces.Utils + expires time.Duration } -func newController(service *service, utils interfaces.Utils) *controller { +func newController(service *service, utils interfaces.Utils, expires time.Duration) *controller { return &controller{ service: service, utils: utils, + expires: expires, } } @@ -34,6 +38,10 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref chartsGroup.GET("", h.controller.getChartsPrices) chartsGroup.GET("/:uuid", h.controller.getDistinctPrices) + imagesGroup := merchGroup.Group("/images") + imagesGroup.POST("/:uuid", h.controller.uploadMerchImage) + imagesGroup.GET("/:uuid", h.controller.getMerchImage) + imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage) } // @Summary Добавить новый мерч @@ -134,10 +142,10 @@ func (co *controller) getAllMerch(c *gin.Context) { // @Description Обновить информацию про мерч по его uuid в json-е // @Tags Merch // @Security BearerAuth -// @Param body body UpdateMerchDTO true "merch_uuid" +// @Param body body UpdateMerchDTO true "merch_uuid" // @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/ [put] func (co *controller) updateMerch(c *gin.Context) { var payload UpdateMerchDTO @@ -227,10 +235,10 @@ func (co *controller) getChartsPrices(c *gin.Context) { // @Tags Merch // @Security BearerAuth // @Param uuid path string true "merch_uuid" -// @Param days query string false "period in days" -// @Success 200 {object} PricesResponse -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Param days query string false "period in days" +// @Success 200 {object} PricesResponse +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /prices/{uuid} [get] func (co *controller) getDistinctPrices(c *gin.Context) { daysQuery := strings.ToLower(c.DefaultQuery("days", "")) @@ -257,3 +265,138 @@ func (co *controller) getDistinctPrices(c *gin.Context) { c.JSON(http.StatusOK, response) } + +// @Summary Загрузить картинки по merch_uuid и query параметрам +// @Description Загрузить картинки по merch_uuid и query параметрам +// @Tags Merch images +// @Security BearerAuth +// @Accept multipart/form-data +// @Produce json +// @Param uuid path string true "Merch UUID" +// @Param file formData file true "Image file" +// @Param imageType formData string true "Image type: thumbnail, full or all" Enums(thumbnail, full, all) +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/images/{uuid} [post] +func (co *controller) uploadMerchImage(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 + } + + merchUuid := c.Param("uuid") + if merchUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"}) + log.Error("Merch | Failed to get single merch") + return + } + + imageType := c.PostForm("imageType") + types := map[string]struct{}{"thumbnail": {}, "full": {}, "all": {}} + if _, allowed := types[imageType]; !allowed { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "imageType must be one of: thumbnail, full, all"}) + log.WithError(err).Error("Merch | imageType must be one of: thumbnail, full, all") + return + } + + file, err := c.FormFile("file") + if err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "file is required"}) + log.WithError(err).Error("Merch | File is required") + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) + defer cancel() + + err = co.service.uploadMerchImage(ctx, userUuid, merchUuid, imageType, file) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error("Merch | Failed to upload merch image") + return + } + + c.Status(http.StatusOK) +} + +// @Summary Получить картинки по merch_uuid и query параметрам +// @Description Получить картинки по merch_uuid и query параметрам +// @Tags Merch images +// @Security BearerAuth +// @Param uuid path string true "merch_uuid" +// @Param type query string true "image type" +// @Success 200 {object} ImageLink +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/images/{uuid} [get] +func (co *controller) getMerchImage(c *gin.Context) { + typeQuery := strings.ToLower(c.Query("type")) + if typeQuery == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "Image type query param 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 + } + + merchUuid := c.Param("uuid") + if merchUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"}) + log.WithError(err).Error("Merch | Failed to get single merch") + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) + defer cancel() + + link, err := co.service.getMerchImage(ctx, userUuid, merchUuid, typeQuery) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error("Merch | Failed to get merch image") + return + } + c.JSON(http.StatusOK, ImageLink{Link: link.String()}) +} + +// @Summary Удалить (безвозвратно) картинки по merch_uuid и query параметрам +// @Description Удалить (безвозвратно) картинки по merch_uuid и query параметрам +// @Tags Merch images +// @Security BearerAuth +// @Param uuid path string true "merch_uuid" +// @Param type query string true "image type" +// @Success 200 {object} PricesResponse +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/images/{uuid} [delete] +func (co *controller) deleteMerchImage(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 + } + + merchUuid := c.Param("uuid") + if merchUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"}) + log.WithError(err).Error("Merch | Failed to get single merch") + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) + defer cancel() + + if err := co.service.deleteMerchImage(ctx, userUuid, merchUuid); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error("Merch | Failed to delete merch image") + return + } + c.Status(http.StatusOK) +} diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index 6e872a2..3f94688 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -53,3 +53,7 @@ type UpdateMerchDTO struct { Origin string `json:"origin"` Link string `json:"link"` } + +type ImageLink struct { + Link string `json:"link"` +} diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index 274ccdf..e61e4e0 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -1,8 +1,10 @@ package merch import ( + log "github.com/sirupsen/logrus" "gorm.io/gorm" "merch-parser-api/internal/interfaces" + "time" ) type Handler struct { @@ -14,12 +16,25 @@ type Handler struct { type Deps struct { DB *gorm.DB Utils interfaces.Utils + Media interfaces.MediaStorage } func NewHandler(deps Deps) *Handler { + packageBucketName := "user-merch-images" + expires := time.Minute * 1 + r := NewRepo(deps.DB) - s := newService(r) - c := newController(s, deps.Utils) + s := newService(r, deps.Media, packageBucketName, expires) + c := newController(s, deps.Utils, expires) + + media := deps.Media + log.WithFields(log.Fields{ + "addr": media, + }).Debug("Merch handler constructor | Media provider") + + if err := media.СreateBucketIfNotExists(packageBucketName); err != nil { + log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") + } return &Handler{ repo: r, diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 61a3bc5..3c4ccfc 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -21,6 +21,7 @@ func NewRepo(db *gorm.DB) *Repo { type repository interface { addMerch(bundle merchBundle) error + merchRecordExists(userUuid, merchUuid string) (bool, error) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) getAllMerch(userUuid string) ([]ListResponse, error) @@ -54,6 +55,19 @@ func (r *Repo) addMerch(bundle merchBundle) error { return nil } +func (r *Repo) merchRecordExists(userUuid, merchUuid string) (bool, error) { + var exists bool + err := r.db.Raw(` + SELECT EXISTS ( + SELECT 1 + FROM merch + WHERE user_uuid = ? + AND merch_uuid = ? + );`, userUuid, merchUuid).Scan(&exists).Error + + return exists, err +} + func (r *Repo) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) { var merch Merch if err := r.db. diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index a0fe00f..de751ec 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -1,22 +1,49 @@ package merch import ( + "bytes" + "context" "database/sql" "errors" + "fmt" + "github.com/disintegration/imaging" "github.com/google/uuid" + log "github.com/sirupsen/logrus" + "image" + "image/jpeg" + "io" + "merch-parser-api/internal/interfaces" + "mime/multipart" + "net/url" + "path/filepath" + "strings" "time" ) type service struct { - repo repository + repo repository + media interfaces.MediaStorage + bucketName string + expires time.Duration } -func newService(repo repository) *service { +func newService(repo repository, media interfaces.MediaStorage, bucketName string, expires time.Duration) *service { return &service{ - repo: repo, + repo: repo, + media: media, + bucketName: bucketName, + expires: expires, } } +type uploadImageParams struct { + ctx context.Context + src io.Reader + imageType string + object string + quality int +} + func (s *service) addMerch(payload MerchDTO, userUuid string) error { merchUuid := uuid.NewString() @@ -80,6 +107,13 @@ func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error { } func (s *service) deleteMerch(userUuid, merchUuid string) error { + ctx, cancel := context.WithTimeout(context.Background(), s.expires) + defer cancel() + + if err := s.deleteMerchImage(ctx, userUuid, merchUuid); err != nil { + return err + } + return s.repo.deleteMerch(userUuid, merchUuid) } @@ -176,3 +210,167 @@ func (s *service) getDistinctPrices(userUuid, merchUuid, days string) (PricesRes Origins: []OriginWithPrices{originSurugaya, originMandarake}, }, nil } + +func (s *service) uploadMerchImage(ctx context.Context, userUuid, merchUuid, imageType string, file *multipart.FileHeader) error { + exists, err := s.repo.merchRecordExists(userUuid, merchUuid) + if err != nil { + return err + } + + if !exists { + return fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) + } + + rawExt := filepath.Ext(file.Filename) + if rawExt == "" { + return errors.New("no file extension") + } + + ext := strings.ToLower(rawExt[1:]) + allowedTypes := map[string]struct{}{"jpeg": {}, "jpg": {}, "png": {}, "gif": {}} + if _, ok := allowedTypes[ext]; !ok { + return errors.New("invalid file type") + } + + getSrc := func() (io.ReadCloser, error) { + f, err := file.Open() + if err != nil { + log.WithError(err).Error("Merch | Failed to open file") + return nil, err + } + return f, nil + } + + switch imageType { + case "thumbnail": + src, err := getSrc() + if err != nil { + return err + } + return s._uploadToStorage(uploadImageParams{ + ctx: ctx, + src: src, + imageType: "thumbnail", + object: fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid), + quality: 80, + }) + + case "full": + src, err := getSrc() + if err != nil { + return err + } + return s._uploadToStorage(uploadImageParams{ + ctx: ctx, + src: src, + imageType: "full", + object: fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid), + quality: 90, + }) + + case "all": + src, err := getSrc() + if err != nil { + return err + } + if err = s._uploadToStorage(uploadImageParams{ + ctx: ctx, + src: src, + imageType: "thumbnail", + object: fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid), + quality: 80, + }); err != nil { + log.WithError(err).Error("Merch | Upload thumbnail and full image") + return err + } + + src2, err := getSrc() + if err != nil { + return err + } + if err = s._uploadToStorage(uploadImageParams{ + ctx: ctx, + src: src2, + imageType: "full", + object: fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid), + quality: 90, + }); err != nil { + log.WithError(err).Error("Merch | Upload thumbnail and full image") + return err + } + default: + return errors.New("invalid file type") + } + return nil +} + +func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageType string) (*url.URL, error) { + exists, err := s.repo.merchRecordExists(userUuid, merchUuid) + if err != nil { + return nil, err + } + + if !exists { + return nil, fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) + } + + var object string + switch imageType { + case "thumbnail": + object = fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid) + case "full": + object = fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid) + default: + return nil, fmt.Errorf("unknown image type %s", imageType) + } + + return s.media.Get(ctx, s.bucketName, object, s.expires, nil) +} + +func (s *service) deleteMerchImage(ctx context.Context, userUuid, merchUuid string) error { + exists, err := s.repo.merchRecordExists(userUuid, merchUuid) + if err != nil { + return err + } + + if !exists { + return fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) + } + + if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid)); err != nil { + return err + } + + if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid)); err != nil { + return err + } + + return nil +} + +func (s *service) _uploadToStorage(params uploadImageParams) error { + img, _, err := image.Decode(params.src) + if err != nil { + return fmt.Errorf("failed to decode image: %w", err) + } + + if params.imageType == "thumbnail" { + img = imaging.Resize(img, 300, 300, imaging.Lanczos) + } + + var buf bytes.Buffer + if err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: params.quality}); err != nil { + return fmt.Errorf("failed to encode full image: %w", err) + } + + err = s.media.Upload(params.ctx, s.bucketName, params.object, &buf, -1) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "img type": "full", + }).Error("Merch | Failed to upload file to media storage") + return err + } + + return nil +} From d23529e08973ab17bf35f5e0b3e973eda5c9bcc1 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 19:46:22 +0300 Subject: [PATCH 41/87] swagger docs update --- docs/docs.go | 261 ++++++++++++++++++++++++++++++++++++++-------- docs/swagger.json | 261 ++++++++++++++++++++++++++++++++++++++-------- docs/swagger.yaml | 132 +++++++++++++++++++++-- 3 files changed, 559 insertions(+), 95 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index ed5e263..4901f76 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -95,6 +95,207 @@ const docTemplate = `{ } } } + }, + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Обновить информацию про мерч по его uuid в json-е", + "tags": [ + "Merch" + ], + "summary": "Обновить информацию про мерч", + "parameters": [ + { + "description": "merch_uuid", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.UpdateMerchDTO" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/images/{uuid}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить картинки по merch_uuid и query параметрам", + "tags": [ + "Merch images" + ], + "summary": "Получить картинки по merch_uuid и query параметрам", + "parameters": [ + { + "type": "string", + "description": "merch_uuid", + "name": "uuid", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "image type", + "name": "type", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/merch.PricesResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Загрузить картинки по merch_uuid и query параметрам", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Merch images" + ], + "summary": "Загрузить картинки по merch_uuid и query параметрам", + "parameters": [ + { + "type": "string", + "description": "Merch UUID", + "name": "uuid", + "in": "path", + "required": true + }, + { + "type": "file", + "description": "Image file", + "name": "file", + "in": "formData", + "required": true + }, + { + "enum": [ + "thumbnail", + "full", + "all" + ], + "type": "string", + "description": "Image type: thumbnail, full or all", + "name": "imageType", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "tags": [ + "Merch images" + ], + "summary": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "parameters": [ + { + "type": "string", + "description": "merch_uuid", + "name": "uuid", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "image type", + "name": "type", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/merch.PricesResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } } }, "/merch/{uuid}": { @@ -139,49 +340,6 @@ const docTemplate = `{ } } }, - "put": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Обновить информацию про мерч по его uuid в json-е", - "tags": [ - "Merch" - ], - "summary": "Обновить информацию про мерч", - "parameters": [ - { - "description": "merch_uuid", - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.MerchDTO" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/merch.MerchDTO" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - }, "delete": { "security": [ { @@ -706,6 +864,23 @@ const docTemplate = `{ } } }, + "merch.UpdateMerchDTO": { + "type": "object", + "properties": { + "link": { + "type": "string" + }, + "merch_uuid": { + "type": "string" + }, + "name": { + "type": "string" + }, + "origin": { + "type": "string" + } + } + }, "responses.ErrorResponse400": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 068cc35..a1b5b73 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -87,6 +87,207 @@ } } } + }, + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Обновить информацию про мерч по его uuid в json-е", + "tags": [ + "Merch" + ], + "summary": "Обновить информацию про мерч", + "parameters": [ + { + "description": "merch_uuid", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.UpdateMerchDTO" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/images/{uuid}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить картинки по merch_uuid и query параметрам", + "tags": [ + "Merch images" + ], + "summary": "Получить картинки по merch_uuid и query параметрам", + "parameters": [ + { + "type": "string", + "description": "merch_uuid", + "name": "uuid", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "image type", + "name": "type", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/merch.PricesResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Загрузить картинки по merch_uuid и query параметрам", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Merch images" + ], + "summary": "Загрузить картинки по merch_uuid и query параметрам", + "parameters": [ + { + "type": "string", + "description": "Merch UUID", + "name": "uuid", + "in": "path", + "required": true + }, + { + "type": "file", + "description": "Image file", + "name": "file", + "in": "formData", + "required": true + }, + { + "enum": [ + "thumbnail", + "full", + "all" + ], + "type": "string", + "description": "Image type: thumbnail, full or all", + "name": "imageType", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "tags": [ + "Merch images" + ], + "summary": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "parameters": [ + { + "type": "string", + "description": "merch_uuid", + "name": "uuid", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "image type", + "name": "type", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/merch.PricesResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } } }, "/merch/{uuid}": { @@ -131,49 +332,6 @@ } } }, - "put": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Обновить информацию про мерч по его uuid в json-е", - "tags": [ - "Merch" - ], - "summary": "Обновить информацию про мерч", - "parameters": [ - { - "description": "merch_uuid", - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.MerchDTO" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/merch.MerchDTO" - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - }, "delete": { "security": [ { @@ -698,6 +856,23 @@ } } }, + "merch.UpdateMerchDTO": { + "type": "object", + "properties": { + "link": { + "type": "string" + }, + "merch_uuid": { + "type": "string" + }, + "name": { + "type": "string" + }, + "origin": { + "type": "string" + } + } + }, "responses.ErrorResponse400": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 20ed305..2d9d9bc 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -55,6 +55,17 @@ definitions: link: type: string type: object + merch.UpdateMerchDTO: + properties: + link: + type: string + merch_uuid: + type: string + name: + type: string + origin: + type: string + type: object responses.ErrorResponse400: properties: error: @@ -173,6 +184,31 @@ paths: summary: Получить все записи мерча tags: - Merch + put: + description: Обновить информацию про мерч по его uuid в json-е + parameters: + - description: merch_uuid + in: body + name: body + required: true + schema: + $ref: '#/definitions/merch.UpdateMerchDTO' + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Обновить информацию про мерч + tags: + - Merch /merch/{uuid}: delete: description: Пометить мерч как удаленный по его uuid @@ -226,20 +262,25 @@ paths: summary: Получить всю информацию про мерч tags: - Merch - put: - description: Обновить информацию про мерч по его uuid в json-е + /merch/images/{uuid}: + delete: + description: Удалить (безвозвратно) картинки по merch_uuid и query параметрам parameters: - description: merch_uuid - in: body - name: body + in: path + name: uuid required: true - schema: - $ref: '#/definitions/merch.MerchDTO' + type: string + - description: image type + in: query + name: type + required: true + type: string responses: "200": description: OK schema: - $ref: '#/definitions/merch.MerchDTO' + $ref: '#/definitions/merch.PricesResponse' "400": description: Bad Request schema: @@ -250,9 +291,82 @@ paths: $ref: '#/definitions/responses.ErrorResponse500' security: - BearerAuth: [] - summary: Обновить информацию про мерч + summary: Удалить (безвозвратно) картинки по merch_uuid и query параметрам tags: - - Merch + - Merch images + get: + description: Получить картинки по merch_uuid и query параметрам + parameters: + - description: merch_uuid + in: path + name: uuid + required: true + type: string + - description: image type + in: query + name: type + required: true + type: string + responses: + "200": + description: OK + schema: + $ref: '#/definitions/merch.PricesResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Получить картинки по merch_uuid и query параметрам + tags: + - Merch images + post: + consumes: + - multipart/form-data + description: Загрузить картинки по merch_uuid и query параметрам + parameters: + - description: Merch UUID + in: path + name: uuid + required: true + type: string + - description: Image file + in: formData + name: file + required: true + type: file + - description: 'Image type: thumbnail, full or all' + enum: + - thumbnail + - full + - all + in: formData + name: imageType + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Загрузить картинки по merch_uuid и query параметрам + tags: + - Merch images /prices: get: description: Получить цены мерча за период From b4693137013fb4ecaa6a8c21223726238d27266c Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 20:18:19 +0300 Subject: [PATCH 42/87] update --- Dockerfile | 2 +- go.mod | 10 +++++----- go.sum | 15 ++++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index a99e5ed..53e5c6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o main "./cmd" +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main "./cmd" FROM alpine:3.22 diff --git a/go.mod b/go.mod index 2a71658..9fbbf8e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module merch-parser-api go 1.25.1 require ( + github.com/disintegration/imaging v1.6.2 github.com/gin-contrib/cors v1.7.6 github.com/gin-gonic/gin v1.11.0 github.com/golang-jwt/jwt/v5 v5.3.0 @@ -25,7 +26,6 @@ require ( github.com/bytedance/sonic v1.14.1 // indirect github.com/bytedance/sonic/loader v0.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect - github.com/disintegration/imaging v1.6.2 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.10 // indirect github.com/gin-contrib/sse v1.1.0 // indirect @@ -56,7 +56,7 @@ require ( github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/minio/crc64nvme v1.0.2 // indirect + github.com/minio/crc64nvme v1.1.1 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect @@ -66,18 +66,18 @@ require ( github.com/quic-go/quic-go v0.55.0 // indirect github.com/rogpeppe/go-internal v1.14.1 // indirect github.com/rs/xid v1.6.0 // indirect - github.com/tinylib/msgp v1.3.0 // indirect + github.com/tinylib/msgp v1.4.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.0 // indirect go.uber.org/mock v0.6.0 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/arch v0.22.0 // indirect - golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect + golang.org/x/image v0.32.0 // indirect golang.org/x/mod v0.29.0 // indirect golang.org/x/net v0.46.0 // indirect golang.org/x/sync v0.17.0 // indirect golang.org/x/sys v0.37.0 // indirect golang.org/x/text v0.30.0 // indirect golang.org/x/tools v0.38.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f // indirect ) diff --git a/go.sum b/go.sum index 5aafef6..bdf49df 100644 --- a/go.sum +++ b/go.sum @@ -102,8 +102,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg= -github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI= +github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU= @@ -145,8 +145,8 @@ github.com/swaggo/gin-swagger v1.6.1 h1:Ri06G4gc9N4t4k8hekMigJ9zKTFSlqj/9paAQCQs github.com/swaggo/gin-swagger v1.6.1/go.mod h1:LQ+hJStHakCWRiK/YNYtJOu4mR2FP+pxLnILT/qNiTw= github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI= github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg= -github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww= -github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= +github.com/tinylib/msgp v1.4.0 h1:SYOeDRiydzOw9kSiwdYp9UcBgPFtLU2WDHaJXyHruf8= +github.com/tinylib/msgp v1.4.0/go.mod h1:cvjFkb4RiC8qSBOPMGPSzSAx47nAsfhLVTCZZNuHv5o= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= @@ -174,8 +174,9 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= -golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.32.0 h1:6lZQWq75h7L5IWNk0r+SCpUJ6tUVd3v4ZHnbRKLkUDQ= +golang.org/x/image v0.32.0/go.mod h1:/R37rrQmKXtO6tYXAjtDLwQgFLHmhW+V6ayXlxzP2Pc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA= golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= @@ -216,8 +217,8 @@ golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff h1:A90eA31Wq6HOMIQlLfzFwzqGKBTuaVztYu/g8sn+8Zc= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f h1:1FTH6cpXFsENbPR5Bu8NQddPSaUUE6NA2XdZdDSAJK4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= google.golang.org/grpc v1.76.0 h1:UnVkv1+uMLYXoIz6o7chp59WfQUYA2ex/BXQ9rHZu7A= google.golang.org/grpc v1.76.0/go.mod h1:Ju12QI8M6iQJtbcsV+awF5a4hfJMLi4X0JLo94ULZ6c= google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= From a29d5a876694335901270dba4f88348135896e3d Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 20:28:15 +0300 Subject: [PATCH 43/87] update --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 53e5c6a..ce293ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main "./cmd" +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main ./cmd FROM alpine:3.22 From 1fc273c62ffafc8679a2e541d5ab043e868b98b7 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 20:47:15 +0300 Subject: [PATCH 44/87] update --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ce293ba..53e5c6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main ./cmd +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main "./cmd" FROM alpine:3.22 From 38193e89433bceddc8ad49aa82254e9a79c0d26a Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 20:48:05 +0300 Subject: [PATCH 45/87] conf fix --- cmd/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ddea055..8a2e607 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -28,8 +28,7 @@ import ( func main() { log.Debug("Starting merch-parser-api") //setup config - //c := config.NewConfig() - c := config.DevConfig() + c := config.NewConfig() ctx := context.Background() //log level From 95b75d0067511faeb2ffc44960e44f847081fd9d Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 15 Oct 2025 21:34:32 +0300 Subject: [PATCH 46/87] change from creation to exists check --- internal/api/merch/handler.go | 3 ++- internal/interfaces/mediaStorage.go | 2 +- internal/mediaStorage/service.go | 20 ++++++-------------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index e61e4e0..b9b8227 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -32,7 +32,8 @@ func NewHandler(deps Deps) *Handler { "addr": media, }).Debug("Merch handler constructor | Media provider") - if err := media.СreateBucketIfNotExists(packageBucketName); err != nil { + exists, err := media.CheckBucketExists(packageBucketName) + if err != nil || !exists { log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") } diff --git a/internal/interfaces/mediaStorage.go b/internal/interfaces/mediaStorage.go index ff185ed..ef8e178 100644 --- a/internal/interfaces/mediaStorage.go +++ b/internal/interfaces/mediaStorage.go @@ -8,7 +8,7 @@ import ( ) type MediaStorage interface { - СreateBucketIfNotExists(bucketName string) error + CheckBucketExists(bucketName string) (bool, error) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) Delete(ctx context.Context, bucket, object string) error diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index 1ae0e2b..39fedce 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -2,8 +2,6 @@ package mediaStorage import ( "context" - "errors" - "fmt" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" "io" @@ -21,21 +19,15 @@ func newService(client *minio.Client) *Service { } } -func (s *Service) СreateBucketIfNotExists(bucketName string) error { +func (s *Service) CheckBucketExists(bucketName string) (bool, error) { ctx := context.Background() - err := s.client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{}) + exists, err := s.client.BucketExists(ctx, bucketName) if err != nil { - var minioErr minio.ErrorResponse - if errors.As(err, &minioErr) { - if minioErr.Code == "BucketAlreadyExists" || minioErr.Code == "BucketAlreadyOwnedByYou" { - log.Infof("Media storage | Bucket %s already exists, skipping creation", bucketName) - return nil - } - } - return fmt.Errorf("failed to create bucket: %w", err) + log.WithError(err).Fatal("Media storage | Failed to check bucket existence") + return exists, err } - log.Infof("Media storage | Bucket %s created successfully", bucketName) - return nil + log.Infof("Media storage | Bucket %s exists", bucketName) + return exists, nil } func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error { From 2d2afffcaff325fa3e79dcf4e3f88ab6a6254de7 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 16 Oct 2025 15:41:52 +0300 Subject: [PATCH 47/87] create client log + secure mode env --- config/config.go | 2 ++ internal/mediaStorage/handler.go | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index d57e264..9ec076a 100644 --- a/config/config.go +++ b/config/config.go @@ -46,6 +46,7 @@ type MediaConfig struct { Port string User string Password string + Secure string } func NewConfig() *Config { @@ -86,6 +87,7 @@ func NewConfig() *Config { Port: getEnv("MEDIA_STORAGE_PORT", ""), User: getEnv("MEDIA_STORAGE_USER", ""), Password: getEnv("MEDIA_STORAGE_PASSWORD", ""), + Secure: getEnv("MEDIA_STORAGE_SECURE", ""), }, } } diff --git a/internal/mediaStorage/handler.go b/internal/mediaStorage/handler.go index 90732d0..ab1331e 100644 --- a/internal/mediaStorage/handler.go +++ b/internal/mediaStorage/handler.go @@ -16,19 +16,29 @@ type Deps struct { Port string User string Password string + Secure string } func NewHandler(deps Deps) *Handler { + secureMode := false + if deps.Secure == "true" { + secureMode = true + } + endpoint := fmt.Sprintf("%s:%s", deps.Host, deps.Port) minioClient, err := minio.New(endpoint, &minio.Options{ Creds: credentials.NewStaticV4(deps.User, deps.Password, ""), - Secure: false, + Secure: secureMode, }) - if err != nil { log.WithError(err).Fatal("Media storage | Failed to create minio client") } + log.WithFields(log.Fields{ + "endpoint": endpoint, + "secure": secureMode, + }).Debug("Media storage | Created minio client") + return &Handler{ newService(minioClient), } From f6bb64759102c4e48dda352dc208afdb6b7ec810 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 16 Oct 2025 20:55:24 +0300 Subject: [PATCH 48/87] update --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 53e5c6a..33390eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,7 +28,7 @@ COPY --from=builder /app/main /usr/local/bin/app RUN chmod +x /usr/local/bin/app -RUN adduser -D -s /bin/bash appuser -USER appuser +#RUN adduser -D -s /bin/bash appuser +#USER appuser ENTRYPOINT ["app"] \ No newline at end of file From 2feda33e26e2ee77692f65ae84b6b08f91ea2b33 Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 16 Oct 2025 21:00:23 +0300 Subject: [PATCH 49/87] dev --- internal/api/merch/handler.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index b9b8227..a83f913 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -32,10 +32,10 @@ func NewHandler(deps Deps) *Handler { "addr": media, }).Debug("Merch handler constructor | Media provider") - exists, err := media.CheckBucketExists(packageBucketName) - if err != nil || !exists { - log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") - } + //exists, err := media.CheckBucketExists(packageBucketName) + //if err != nil || !exists { + // log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") + //} return &Handler{ repo: r, From d1542b274ee5a4e57d443f9e69b3e88ec53c3bdc Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 17 Oct 2025 23:46:34 +0300 Subject: [PATCH 50/87] switch to ubuntu --- Dockerfile | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 33390eb..c05cbe3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,5 @@ FROM golang:1.25.1-alpine3.22 AS builder -RUN apk add --no-cache \ - bash \ - curl \ - git \ - ca-certificates - - -RUN apk add --no-cache tzdata - WORKDIR /app COPY go.mod go.sum ./ RUN go mod download @@ -16,19 +7,11 @@ COPY . . RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main "./cmd" -FROM alpine:3.22 - -RUN apk add --no-cache \ - bash \ - curl \ - ca-certificates \ - tzdata +FROM ubuntu:24.04 COPY --from=builder /app/main /usr/local/bin/app RUN chmod +x /usr/local/bin/app -#RUN adduser -D -s /bin/bash appuser -#USER appuser ENTRYPOINT ["app"] \ No newline at end of file From bc6621154b02cd9039b647c16870fad6f054b83b Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 17 Oct 2025 23:47:48 +0300 Subject: [PATCH 51/87] return etag --- internal/api/merch/controller.go | 2 +- internal/api/merch/dto.go | 1 + internal/api/merch/handler.go | 8 ++++---- internal/api/merch/service.go | 26 +++++++++++++++++++------- internal/interfaces/mediaStorage.go | 1 + internal/mediaStorage/service.go | 8 ++++++++ 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index f0d8e8b..d4e05f8 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -362,7 +362,7 @@ func (co *controller) getMerchImage(c *gin.Context) { log.WithError(err).Error("Merch | Failed to get merch image") return } - c.JSON(http.StatusOK, ImageLink{Link: link.String()}) + c.JSON(http.StatusOK, link) } // @Summary Удалить (безвозвратно) картинки по merch_uuid и query параметрам diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index 3f94688..bc1646e 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -56,4 +56,5 @@ type UpdateMerchDTO struct { type ImageLink struct { Link string `json:"link"` + ETag string `json:"etag"` } diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index a83f913..b9b8227 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -32,10 +32,10 @@ func NewHandler(deps Deps) *Handler { "addr": media, }).Debug("Merch handler constructor | Media provider") - //exists, err := media.CheckBucketExists(packageBucketName) - //if err != nil || !exists { - // log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") - //} + exists, err := media.CheckBucketExists(packageBucketName) + if err != nil || !exists { + log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") + } return &Handler{ repo: r, diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index de751ec..eb0c85d 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -14,7 +14,6 @@ import ( "io" "merch-parser-api/internal/interfaces" "mime/multipart" - "net/url" "path/filepath" "strings" "time" @@ -304,14 +303,14 @@ func (s *service) uploadMerchImage(ctx context.Context, userUuid, merchUuid, ima return nil } -func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageType string) (*url.URL, error) { +func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageType string) (ImageLink, error) { exists, err := s.repo.merchRecordExists(userUuid, merchUuid) if err != nil { - return nil, err + return ImageLink{}, err } if !exists { - return nil, fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) + return ImageLink{}, fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) } var object string @@ -321,10 +320,23 @@ func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageT case "full": object = fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid) default: - return nil, fmt.Errorf("unknown image type %s", imageType) + return ImageLink{}, fmt.Errorf("unknown image type %s", imageType) } - return s.media.Get(ctx, s.bucketName, object, s.expires, nil) + link, err := s.media.Get(ctx, s.bucketName, object, s.expires, nil) + if err != nil { + return ImageLink{}, err + } + + etag, err := s.media.GetObjectEtag(ctx, s.bucketName, object) + if err != nil { + return ImageLink{}, err + } + + return ImageLink{ + Link: link.String(), + ETag: etag, + }, nil } func (s *service) deleteMerchImage(ctx context.Context, userUuid, merchUuid string) error { @@ -367,7 +379,7 @@ func (s *service) _uploadToStorage(params uploadImageParams) error { if err != nil { log.WithFields(log.Fields{ "error": err, - "img type": "full", + "img type": params.imageType, }).Error("Merch | Failed to upload file to media storage") return err } diff --git a/internal/interfaces/mediaStorage.go b/internal/interfaces/mediaStorage.go index ef8e178..bf46f47 100644 --- a/internal/interfaces/mediaStorage.go +++ b/internal/interfaces/mediaStorage.go @@ -12,4 +12,5 @@ type MediaStorage interface { Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) Delete(ctx context.Context, bucket, object string) error + GetObjectEtag(ctx context.Context, bucketName, object string) (string, error) } diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index 39fedce..3a5b7aa 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -42,3 +42,11 @@ func (s *Service) Get(ctx context.Context, bucket, object string, expires time.D func (s *Service) Delete(ctx context.Context, bucket, object string) error { return s.client.RemoveObject(ctx, bucket, object, minio.RemoveObjectOptions{}) } + +func (s *Service) GetObjectEtag(ctx context.Context, bucketName, object string) (string, error) { + info, err := s.client.StatObject(ctx, bucketName, object, minio.StatObjectOptions{}) + if err != nil { + return "", err + } + return info.ETag, nil +} From c2304f6a7dafe8ed73734545f187ff92d062a26e Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 17 Oct 2025 23:48:05 +0300 Subject: [PATCH 52/87] update --- api.env | 1 + 1 file changed, 1 insertion(+) diff --git a/api.env b/api.env index 0a477e8..add3486 100644 --- a/api.env +++ b/api.env @@ -12,6 +12,7 @@ MEDIA_STORAGE_USER= MEDIA_STORAGE_PASS= MEDIA_STORAGE_HOST= MEDIA_STORAGE_PORT= +MEDIA_STORAGE_SECURE=false DB_HOST= DB_PORT= From f561869b08071a5ee56c358835c03daa41d0351f Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 18 Oct 2025 14:11:58 +0300 Subject: [PATCH 53/87] swagger docs update --- docs/docs.go | 25 +++++++++++++------------ docs/swagger.json | 25 +++++++++++++------------ docs/swagger.yaml | 16 ++++++++-------- internal/api/merch/controller.go | 3 +-- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 4901f76..769c22b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -169,7 +169,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/merch.PricesResponse" + "$ref": "#/definitions/merch.ImageLink" } }, "400": { @@ -267,21 +267,11 @@ const docTemplate = `{ "name": "uuid", "in": "path", "required": true - }, - { - "type": "string", - "description": "image type", - "name": "type", - "in": "query", - "required": true } ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/merch.PricesResponse" - } + "description": "OK" }, "400": { "description": "Bad Request", @@ -778,6 +768,17 @@ const docTemplate = `{ } }, "definitions": { + "merch.ImageLink": { + "type": "object", + "properties": { + "etag": { + "type": "string" + }, + "link": { + "type": "string" + } + } + }, "merch.ListResponse": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index a1b5b73..d333c5a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -161,7 +161,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/merch.PricesResponse" + "$ref": "#/definitions/merch.ImageLink" } }, "400": { @@ -259,21 +259,11 @@ "name": "uuid", "in": "path", "required": true - }, - { - "type": "string", - "description": "image type", - "name": "type", - "in": "query", - "required": true } ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/merch.PricesResponse" - } + "description": "OK" }, "400": { "description": "Bad Request", @@ -770,6 +760,17 @@ } }, "definitions": { + "merch.ImageLink": { + "type": "object", + "properties": { + "etag": { + "type": "string" + }, + "link": { + "type": "string" + } + } + }, "merch.ListResponse": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2d9d9bc..c3f516b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,5 +1,12 @@ basePath: /api/v2 definitions: + merch.ImageLink: + properties: + etag: + type: string + link: + type: string + type: object merch.ListResponse: properties: merch_uuid: @@ -271,16 +278,9 @@ paths: name: uuid required: true type: string - - description: image type - in: query - name: type - required: true - type: string responses: "200": description: OK - schema: - $ref: '#/definitions/merch.PricesResponse' "400": description: Bad Request schema: @@ -311,7 +311,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/merch.PricesResponse' + $ref: '#/definitions/merch.ImageLink' "400": description: Bad Request schema: diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index d4e05f8..6d7cd5d 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -370,8 +370,7 @@ func (co *controller) getMerchImage(c *gin.Context) { // @Tags Merch images // @Security BearerAuth // @Param uuid path string true "merch_uuid" -// @Param type query string true "image type" -// @Success 200 {object} PricesResponse +// @Success 200 // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/images/{uuid} [delete] From 0348dda5cd5a5c5426790b3dcd9574453af9be4a Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 18 Oct 2025 14:57:43 +0300 Subject: [PATCH 54/87] switch back to alpine --- Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c05cbe3..941ced8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,11 @@ COPY . . RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o main "./cmd" -FROM ubuntu:24.04 +FROM alpine:3.22 + +RUN apk add --no-cache \ + tzdata \ + ca-certificates COPY --from=builder /app/main /usr/local/bin/app From bb305eab9ed9ea3518d385b12912261f74a2122f Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 18 Oct 2025 15:30:59 +0300 Subject: [PATCH 55/87] replace domain for links --- api.env | 3 ++- cmd/main.go | 2 ++ config/config.go | 2 ++ internal/api/merch/service.go | 2 +- internal/interfaces/mediaStorage.go | 2 +- internal/mediaStorage/handler.go | 3 ++- internal/mediaStorage/service.go | 23 +++++++++++++++++++---- 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/api.env b/api.env index add3486..563fd50 100644 --- a/api.env +++ b/api.env @@ -9,9 +9,10 @@ GRPC_SERVER_PORT=9050 GRPC_CLIENT_PORT=9060 MEDIA_STORAGE_USER= -MEDIA_STORAGE_PASS= +MEDIA_STORAGE_PASSWORD= MEDIA_STORAGE_HOST= MEDIA_STORAGE_PORT= +MEDIA_STORAGE_DOMAIN= MEDIA_STORAGE_SECURE=false DB_HOST= diff --git a/cmd/main.go b/cmd/main.go index 8a2e607..d5e74cc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -56,6 +56,8 @@ func main() { Port: c.MediaConf.Port, User: c.MediaConf.User, Password: c.MediaConf.Password, + Domain: c.MediaConf.Domain, + Secure: c.MediaConf.Secure, }) log.WithFields(log.Fields{ "address": c.MediaConf.Host + ":" + c.MediaConf.Port, diff --git a/config/config.go b/config/config.go index 9ec076a..0e71b33 100644 --- a/config/config.go +++ b/config/config.go @@ -46,6 +46,7 @@ type MediaConfig struct { Port string User string Password string + Domain string Secure string } @@ -87,6 +88,7 @@ func NewConfig() *Config { Port: getEnv("MEDIA_STORAGE_PORT", ""), User: getEnv("MEDIA_STORAGE_USER", ""), Password: getEnv("MEDIA_STORAGE_PASSWORD", ""), + Domain: getEnv("MEDIA_STORAGE_DOMAIN", ""), Secure: getEnv("MEDIA_STORAGE_SECURE", ""), }, } diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index eb0c85d..31aced3 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -334,7 +334,7 @@ func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageT } return ImageLink{ - Link: link.String(), + Link: link, ETag: etag, }, nil } diff --git a/internal/interfaces/mediaStorage.go b/internal/interfaces/mediaStorage.go index bf46f47..b332a5f 100644 --- a/internal/interfaces/mediaStorage.go +++ b/internal/interfaces/mediaStorage.go @@ -10,7 +10,7 @@ import ( type MediaStorage interface { CheckBucketExists(bucketName string) (bool, error) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error - Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) + Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) Delete(ctx context.Context, bucket, object string) error GetObjectEtag(ctx context.Context, bucketName, object string) (string, error) } diff --git a/internal/mediaStorage/handler.go b/internal/mediaStorage/handler.go index ab1331e..e6293ba 100644 --- a/internal/mediaStorage/handler.go +++ b/internal/mediaStorage/handler.go @@ -16,6 +16,7 @@ type Deps struct { Port string User string Password string + Domain string Secure string } @@ -40,6 +41,6 @@ func NewHandler(deps Deps) *Handler { }).Debug("Media storage | Created minio client") return &Handler{ - newService(minioClient), + newService(minioClient, deps.Domain, endpoint), } } diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index 3a5b7aa..a55b6fc 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -2,20 +2,25 @@ package mediaStorage import ( "context" + "fmt" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" "io" "net/url" + "strings" "time" ) type Service struct { - client *minio.Client + client *minio.Client + domain string + endpoint string } -func newService(client *minio.Client) *Service { +func newService(client *minio.Client, domain, endpoint string) *Service { return &Service{ client: client, + domain: domain, } } @@ -35,8 +40,18 @@ func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.R return err } -func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) { - return s.client.PresignedGetObject(ctx, bucket, object, expires, params) +func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) { + presigned, err := s.client.PresignedGetObject(ctx, bucket, object, expires, params) + if err != nil { + return "", err + } + + link := presigned.String() + if s.domain != "" { + link = strings.Replace(link, fmt.Sprintf("http://%s", s.endpoint), s.domain, 1) + } + + return link, nil } func (s *Service) Delete(ctx context.Context, bucket, object string) error { From f3d123ee3bacda8d4c7343e427814c2c7feea254 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 18 Oct 2025 16:07:56 +0300 Subject: [PATCH 56/87] replace domain for links --- internal/api/merch/handler.go | 2 +- internal/mediaStorage/service.go | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index b9b8227..a442f9a 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -21,7 +21,7 @@ type Deps struct { func NewHandler(deps Deps) *Handler { packageBucketName := "user-merch-images" - expires := time.Minute * 1 + expires := time.Minute * 5 r := NewRepo(deps.DB) s := newService(r, deps.Media, packageBucketName, expires) diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index a55b6fc..3a3d7c5 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -7,7 +7,6 @@ import ( log "github.com/sirupsen/logrus" "io" "net/url" - "strings" "time" ) @@ -46,12 +45,21 @@ func (s *Service) Get(ctx context.Context, bucket, object string, expires time.D return "", err } - link := presigned.String() - if s.domain != "" { - link = strings.Replace(link, fmt.Sprintf("http://%s", s.endpoint), s.domain, 1) + u, err := url.Parse(presigned.String()) + if err != nil { + return "", err } - return link, nil + if s.domain != "" { + domainURL, err := url.Parse(s.domain) + if err != nil { + return "", fmt.Errorf("invalid domain URL: %w", err) + } + u.Scheme = domainURL.Scheme + u.Host = domainURL.Host + } + + return u.String(), nil } func (s *Service) Delete(ctx context.Context, bucket, object string) error { From 947220b65c0fa4534d52114c4cf98fc8651dda7d Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 18 Oct 2025 16:38:21 +0300 Subject: [PATCH 57/87] endpoint env refactor --- api.env | 4 +--- cmd/main.go | 6 ++---- config/config.go | 8 ++------ internal/mediaStorage/handler.go | 11 ++++------- internal/mediaStorage/service.go | 20 ++------------------ 5 files changed, 11 insertions(+), 38 deletions(-) diff --git a/api.env b/api.env index 563fd50..8bb1d03 100644 --- a/api.env +++ b/api.env @@ -8,11 +8,9 @@ APP_ALLOWED_ORIGINS=http://localhost:5173, GRPC_SERVER_PORT=9050 GRPC_CLIENT_PORT=9060 +MEDIA_STORAGE_ENDPOINT= MEDIA_STORAGE_USER= MEDIA_STORAGE_PASSWORD= -MEDIA_STORAGE_HOST= -MEDIA_STORAGE_PORT= -MEDIA_STORAGE_DOMAIN= MEDIA_STORAGE_SECURE=false DB_HOST= diff --git a/cmd/main.go b/cmd/main.go index d5e74cc..3fb0010 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -52,15 +52,13 @@ func main() { log.Debug("Utils provider initialized") mediaProvider := mediaStorage.NewHandler(mediaStorage.Deps{ - Host: c.MediaConf.Host, - Port: c.MediaConf.Port, + Endpoint: c.MediaConf.Endpoint, User: c.MediaConf.User, Password: c.MediaConf.Password, - Domain: c.MediaConf.Domain, Secure: c.MediaConf.Secure, }) log.WithFields(log.Fields{ - "address": c.MediaConf.Host + ":" + c.MediaConf.Port, + "endpoint": c.MediaConf.Endpoint, "provider": mediaProvider, }).Debug("Media storage | Minio client created") diff --git a/config/config.go b/config/config.go index 0e71b33..5f2d4d5 100644 --- a/config/config.go +++ b/config/config.go @@ -42,11 +42,9 @@ type GrpcConfig struct { } type MediaConfig struct { - Host string - Port string + Endpoint string User string Password string - Domain string Secure string } @@ -84,11 +82,9 @@ func NewConfig() *Config { }, MediaConf: MediaConfig{ - Host: getEnv("MEDIA_STORAGE_HOST", ""), - Port: getEnv("MEDIA_STORAGE_PORT", ""), + Endpoint: getEnv("MEDIA_STORAGE_ENDPOINT", ""), User: getEnv("MEDIA_STORAGE_USER", ""), Password: getEnv("MEDIA_STORAGE_PASSWORD", ""), - Domain: getEnv("MEDIA_STORAGE_DOMAIN", ""), Secure: getEnv("MEDIA_STORAGE_SECURE", ""), }, } diff --git a/internal/mediaStorage/handler.go b/internal/mediaStorage/handler.go index e6293ba..c1586eb 100644 --- a/internal/mediaStorage/handler.go +++ b/internal/mediaStorage/handler.go @@ -1,7 +1,6 @@ package mediaStorage import ( - "fmt" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" log "github.com/sirupsen/logrus" @@ -12,8 +11,7 @@ type Handler struct { } type Deps struct { - Host string - Port string + Endpoint string User string Password string Domain string @@ -26,8 +24,7 @@ func NewHandler(deps Deps) *Handler { secureMode = true } - endpoint := fmt.Sprintf("%s:%s", deps.Host, deps.Port) - minioClient, err := minio.New(endpoint, &minio.Options{ + minioClient, err := minio.New(deps.Endpoint, &minio.Options{ Creds: credentials.NewStaticV4(deps.User, deps.Password, ""), Secure: secureMode, }) @@ -36,11 +33,11 @@ func NewHandler(deps Deps) *Handler { } log.WithFields(log.Fields{ - "endpoint": endpoint, + "endpoint": deps.Endpoint, "secure": secureMode, }).Debug("Media storage | Created minio client") return &Handler{ - newService(minioClient, deps.Domain, endpoint), + newService(minioClient), } } diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index 3a3d7c5..6c4b03a 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -2,7 +2,6 @@ package mediaStorage import ( "context" - "fmt" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" "io" @@ -16,10 +15,9 @@ type Service struct { endpoint string } -func newService(client *minio.Client, domain, endpoint string) *Service { +func newService(client *minio.Client) *Service { return &Service{ client: client, - domain: domain, } } @@ -45,21 +43,7 @@ func (s *Service) Get(ctx context.Context, bucket, object string, expires time.D return "", err } - u, err := url.Parse(presigned.String()) - if err != nil { - return "", err - } - - if s.domain != "" { - domainURL, err := url.Parse(s.domain) - if err != nil { - return "", fmt.Errorf("invalid domain URL: %w", err) - } - u.Scheme = domainURL.Scheme - u.Host = domainURL.Host - } - - return u.String(), nil + return presigned.String(), nil } func (s *Service) Delete(ctx context.Context, bucket, object string) error { From 3298602a23bd9c343664d8d4a18cb8b9780fef64 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 19 Oct 2025 19:43:33 +0300 Subject: [PATCH 58/87] switch from pre-signed to public images --- internal/api/merch/controller.go | 2 +- internal/api/merch/helper.go | 12 +++++++++ internal/api/merch/service.go | 32 +++++++++++++++-------- internal/interfaces/mediaStorage.go | 3 ++- internal/mediaStorage/handler.go | 3 +-- internal/mediaStorage/service.go | 39 ++++++++++++++++++++++++----- 6 files changed, 71 insertions(+), 20 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 6d7cd5d..3fc8124 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -356,7 +356,7 @@ func (co *controller) getMerchImage(c *gin.Context) { ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) defer cancel() - link, err := co.service.getMerchImage(ctx, userUuid, merchUuid, typeQuery) + link, err := co.service.getPublicImageLink(ctx, userUuid, merchUuid, typeQuery) if err != nil { c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) log.WithError(err).Error("Merch | Failed to get merch image") diff --git a/internal/api/merch/helper.go b/internal/api/merch/helper.go index 704cbbd..58e1866 100644 --- a/internal/api/merch/helper.go +++ b/internal/api/merch/helper.go @@ -1,6 +1,7 @@ package merch import ( + "fmt" "strconv" "time" ) @@ -17,3 +18,14 @@ func getPeriod(days string) time.Time { return time.Now().UTC().Add(-(time.Duration(daysInt) * time.Hour * 24)) } + +func (s *service) makeObject(userUuid, merchUuid, imageType string) (string, error) { + switch imageType { + case "thumbnail": + return fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid), nil + case "full": + return fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid), nil + default: + return "", fmt.Errorf("unknown image type %s", imageType) + } +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 31aced3..481ca60 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -303,7 +303,24 @@ func (s *service) uploadMerchImage(ctx context.Context, userUuid, merchUuid, ima return nil } -func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageType string) (ImageLink, error) { +func (s *service) getPublicImageLink(ctx context.Context, userUuid, merchUuid, imageType string) (ImageLink, error) { + object, err := s.makeObject(userUuid, merchUuid, imageType) + if err != nil { + return ImageLink{}, err + } + + link, etag, err := s.media.GetPublicLink(ctx, s.bucketName, object) + if err != nil { + return ImageLink{}, err + } + + return ImageLink{ + Link: link, + ETag: etag, + }, nil +} + +func (s *service) getPresignedImageLink(ctx context.Context, userUuid, merchUuid, imageType string) (ImageLink, error) { exists, err := s.repo.merchRecordExists(userUuid, merchUuid) if err != nil { return ImageLink{}, err @@ -313,17 +330,12 @@ func (s *service) getMerchImage(ctx context.Context, userUuid, merchUuid, imageT return ImageLink{}, fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) } - var object string - switch imageType { - case "thumbnail": - object = fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid) - case "full": - object = fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid) - default: - return ImageLink{}, fmt.Errorf("unknown image type %s", imageType) + object, err := s.makeObject(userUuid, merchUuid, imageType) + if err != nil { + return ImageLink{}, err } - link, err := s.media.Get(ctx, s.bucketName, object, s.expires, nil) + link, err := s.media.GetPresignedLink(ctx, s.bucketName, object, s.expires, nil) if err != nil { return ImageLink{}, err } diff --git a/internal/interfaces/mediaStorage.go b/internal/interfaces/mediaStorage.go index b332a5f..64fb50b 100644 --- a/internal/interfaces/mediaStorage.go +++ b/internal/interfaces/mediaStorage.go @@ -10,7 +10,8 @@ import ( type MediaStorage interface { CheckBucketExists(bucketName string) (bool, error) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error - Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) + GetPublicLink(ctx context.Context, bucket, object string) (string, string, error) + GetPresignedLink(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) Delete(ctx context.Context, bucket, object string) error GetObjectEtag(ctx context.Context, bucketName, object string) (string, error) } diff --git a/internal/mediaStorage/handler.go b/internal/mediaStorage/handler.go index c1586eb..ca88475 100644 --- a/internal/mediaStorage/handler.go +++ b/internal/mediaStorage/handler.go @@ -14,7 +14,6 @@ type Deps struct { Endpoint string User string Password string - Domain string Secure string } @@ -38,6 +37,6 @@ func NewHandler(deps Deps) *Handler { }).Debug("Media storage | Created minio client") return &Handler{ - newService(minioClient), + newService(minioClient, deps.Endpoint, secureMode), } } diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index 6c4b03a..a67a20c 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -2,22 +2,26 @@ package mediaStorage import ( "context" + "fmt" "github.com/minio/minio-go/v7" log "github.com/sirupsen/logrus" "io" "net/url" + "strings" "time" ) type Service struct { - client *minio.Client - domain string - endpoint string + client *minio.Client + endpoint string + secureMode bool } -func newService(client *minio.Client) *Service { +func newService(client *minio.Client, endpoint string, secureMode bool) *Service { return &Service{ - client: client, + client: client, + endpoint: endpoint, + secureMode: secureMode, } } @@ -37,7 +41,30 @@ func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.R return err } -func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) { +func (s *Service) GetPublicLink(ctx context.Context, bucket, object string) (string, string, error) { + stat, err := s.client.StatObject(ctx, bucket, object, minio.StatObjectOptions{}) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "key": bucket + "/" + object, + }).Error("Media storage | Failed to get public link") + return "", "", err + } + + var scheme string + if s.secureMode { + scheme = "https" + } else { + scheme = "http" + } + + link := fmt.Sprintf("%s://%s/%s/%s", scheme, strings.TrimRight(s.endpoint, "/"), bucket, object) + log.WithFields(log.Fields{"link": link}).Debug("Media storage | Get public link") + + return link, stat.ETag, nil +} + +func (s *Service) GetPresignedLink(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) { presigned, err := s.client.PresignedGetObject(ctx, bucket, object, expires, params) if err != nil { return "", err From dae627f4adcf1a9d31a94230b071351057bbe959 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:52:46 +0300 Subject: [PATCH 59/87] image storage contract added --- proto/imageStorage.proto | 27 +++ proto/imageStorage/imageStorage.pb.go | 261 +++++++++++++++++++++ proto/imageStorage/imageStorage_grpc.pb.go | 160 +++++++++++++ 3 files changed, 448 insertions(+) create mode 100644 proto/imageStorage.proto create mode 100644 proto/imageStorage/imageStorage.pb.go create mode 100644 proto/imageStorage/imageStorage_grpc.pb.go diff --git a/proto/imageStorage.proto b/proto/imageStorage.proto new file mode 100644 index 0000000..97a5095 --- /dev/null +++ b/proto/imageStorage.proto @@ -0,0 +1,27 @@ +syntax="proto3"; + +import "google/protobuf/empty.proto"; + +package imageStorage; +option go_package = "imageStorage/pkg/proto/imageStorage"; + +message UploadMerchImageRequest{ + bytes imageData = 1; + string userUuid = 2; + string merchUuid = 3; +} + +message UploadMerchImageResponse { + string fullImage = 1; + string thumbnail = 2; +} + +message DeleteImageRequest { + string userUuid = 1; + string merchUuid = 2; +} + +service ImageStorage { + rpc UploadImage(UploadMerchImageRequest) returns (UploadMerchImageResponse); + rpc DeleteImage(DeleteImageRequest) returns (google.protobuf.Empty); +} \ No newline at end of file diff --git a/proto/imageStorage/imageStorage.pb.go b/proto/imageStorage/imageStorage.pb.go new file mode 100644 index 0000000..be1d5e5 --- /dev/null +++ b/proto/imageStorage/imageStorage.pb.go @@ -0,0 +1,261 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.8 +// protoc v6.32.1 +// source: imageStorage.proto + +package imageStorage + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type UploadMerchImageRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + ImageData []byte `protobuf:"bytes,1,opt,name=imageData,proto3" json:"imageData,omitempty"` + UserUuid string `protobuf:"bytes,2,opt,name=userUuid,proto3" json:"userUuid,omitempty"` + MerchUuid string `protobuf:"bytes,3,opt,name=merchUuid,proto3" json:"merchUuid,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *UploadMerchImageRequest) Reset() { + *x = UploadMerchImageRequest{} + mi := &file_imageStorage_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *UploadMerchImageRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UploadMerchImageRequest) ProtoMessage() {} + +func (x *UploadMerchImageRequest) ProtoReflect() protoreflect.Message { + mi := &file_imageStorage_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UploadMerchImageRequest.ProtoReflect.Descriptor instead. +func (*UploadMerchImageRequest) Descriptor() ([]byte, []int) { + return file_imageStorage_proto_rawDescGZIP(), []int{0} +} + +func (x *UploadMerchImageRequest) GetImageData() []byte { + if x != nil { + return x.ImageData + } + return nil +} + +func (x *UploadMerchImageRequest) GetUserUuid() string { + if x != nil { + return x.UserUuid + } + return "" +} + +func (x *UploadMerchImageRequest) GetMerchUuid() string { + if x != nil { + return x.MerchUuid + } + return "" +} + +type UploadMerchImageResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + FullImage string `protobuf:"bytes,1,opt,name=fullImage,proto3" json:"fullImage,omitempty"` + Thumbnail string `protobuf:"bytes,2,opt,name=thumbnail,proto3" json:"thumbnail,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *UploadMerchImageResponse) Reset() { + *x = UploadMerchImageResponse{} + mi := &file_imageStorage_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *UploadMerchImageResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UploadMerchImageResponse) ProtoMessage() {} + +func (x *UploadMerchImageResponse) ProtoReflect() protoreflect.Message { + mi := &file_imageStorage_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UploadMerchImageResponse.ProtoReflect.Descriptor instead. +func (*UploadMerchImageResponse) Descriptor() ([]byte, []int) { + return file_imageStorage_proto_rawDescGZIP(), []int{1} +} + +func (x *UploadMerchImageResponse) GetFullImage() string { + if x != nil { + return x.FullImage + } + return "" +} + +func (x *UploadMerchImageResponse) GetThumbnail() string { + if x != nil { + return x.Thumbnail + } + return "" +} + +type DeleteImageRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + UserUuid string `protobuf:"bytes,1,opt,name=userUuid,proto3" json:"userUuid,omitempty"` + MerchUuid string `protobuf:"bytes,2,opt,name=merchUuid,proto3" json:"merchUuid,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *DeleteImageRequest) Reset() { + *x = DeleteImageRequest{} + mi := &file_imageStorage_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *DeleteImageRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DeleteImageRequest) ProtoMessage() {} + +func (x *DeleteImageRequest) ProtoReflect() protoreflect.Message { + mi := &file_imageStorage_proto_msgTypes[2] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DeleteImageRequest.ProtoReflect.Descriptor instead. +func (*DeleteImageRequest) Descriptor() ([]byte, []int) { + return file_imageStorage_proto_rawDescGZIP(), []int{2} +} + +func (x *DeleteImageRequest) GetUserUuid() string { + if x != nil { + return x.UserUuid + } + return "" +} + +func (x *DeleteImageRequest) GetMerchUuid() string { + if x != nil { + return x.MerchUuid + } + return "" +} + +var File_imageStorage_proto protoreflect.FileDescriptor + +const file_imageStorage_proto_rawDesc = "" + + "\n" + + "\x12imageStorage.proto\x12\fimageStorage\x1a\x1bgoogle/protobuf/empty.proto\"q\n" + + "\x17UploadMerchImageRequest\x12\x1c\n" + + "\timageData\x18\x01 \x01(\fR\timageData\x12\x1a\n" + + "\buserUuid\x18\x02 \x01(\tR\buserUuid\x12\x1c\n" + + "\tmerchUuid\x18\x03 \x01(\tR\tmerchUuid\"V\n" + + "\x18UploadMerchImageResponse\x12\x1c\n" + + "\tfullImage\x18\x01 \x01(\tR\tfullImage\x12\x1c\n" + + "\tthumbnail\x18\x02 \x01(\tR\tthumbnail\"N\n" + + "\x12DeleteImageRequest\x12\x1a\n" + + "\buserUuid\x18\x01 \x01(\tR\buserUuid\x12\x1c\n" + + "\tmerchUuid\x18\x02 \x01(\tR\tmerchUuid2\xb5\x01\n" + + "\fImageStorage\x12\\\n" + + "\vUploadImage\x12%.imageStorage.UploadMerchImageRequest\x1a&.imageStorage.UploadMerchImageResponse\x12G\n" + + "\vDeleteImage\x12 .imageStorage.DeleteImageRequest\x1a\x16.google.protobuf.EmptyB%Z#imageStorage/pkg/proto/imageStorageb\x06proto3" + +var ( + file_imageStorage_proto_rawDescOnce sync.Once + file_imageStorage_proto_rawDescData []byte +) + +func file_imageStorage_proto_rawDescGZIP() []byte { + file_imageStorage_proto_rawDescOnce.Do(func() { + file_imageStorage_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_imageStorage_proto_rawDesc), len(file_imageStorage_proto_rawDesc))) + }) + return file_imageStorage_proto_rawDescData +} + +var file_imageStorage_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_imageStorage_proto_goTypes = []any{ + (*UploadMerchImageRequest)(nil), // 0: imageStorage.UploadMerchImageRequest + (*UploadMerchImageResponse)(nil), // 1: imageStorage.UploadMerchImageResponse + (*DeleteImageRequest)(nil), // 2: imageStorage.DeleteImageRequest + (*emptypb.Empty)(nil), // 3: google.protobuf.Empty +} +var file_imageStorage_proto_depIdxs = []int32{ + 0, // 0: imageStorage.ImageStorage.UploadImage:input_type -> imageStorage.UploadMerchImageRequest + 2, // 1: imageStorage.ImageStorage.DeleteImage:input_type -> imageStorage.DeleteImageRequest + 1, // 2: imageStorage.ImageStorage.UploadImage:output_type -> imageStorage.UploadMerchImageResponse + 3, // 3: imageStorage.ImageStorage.DeleteImage:output_type -> google.protobuf.Empty + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_imageStorage_proto_init() } +func file_imageStorage_proto_init() { + if File_imageStorage_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_imageStorage_proto_rawDesc), len(file_imageStorage_proto_rawDesc)), + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_imageStorage_proto_goTypes, + DependencyIndexes: file_imageStorage_proto_depIdxs, + MessageInfos: file_imageStorage_proto_msgTypes, + }.Build() + File_imageStorage_proto = out.File + file_imageStorage_proto_goTypes = nil + file_imageStorage_proto_depIdxs = nil +} diff --git a/proto/imageStorage/imageStorage_grpc.pb.go b/proto/imageStorage/imageStorage_grpc.pb.go new file mode 100644 index 0000000..761d8cb --- /dev/null +++ b/proto/imageStorage/imageStorage_grpc.pb.go @@ -0,0 +1,160 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v6.32.1 +// source: imageStorage.proto + +package imageStorage + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + ImageStorage_UploadImage_FullMethodName = "/imageStorage.ImageStorage/UploadImage" + ImageStorage_DeleteImage_FullMethodName = "/imageStorage.ImageStorage/DeleteImage" +) + +// ImageStorageClient is the client API for ImageStorage service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type ImageStorageClient interface { + UploadImage(ctx context.Context, in *UploadMerchImageRequest, opts ...grpc.CallOption) (*UploadMerchImageResponse, error) + DeleteImage(ctx context.Context, in *DeleteImageRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) +} + +type imageStorageClient struct { + cc grpc.ClientConnInterface +} + +func NewImageStorageClient(cc grpc.ClientConnInterface) ImageStorageClient { + return &imageStorageClient{cc} +} + +func (c *imageStorageClient) UploadImage(ctx context.Context, in *UploadMerchImageRequest, opts ...grpc.CallOption) (*UploadMerchImageResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(UploadMerchImageResponse) + err := c.cc.Invoke(ctx, ImageStorage_UploadImage_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *imageStorageClient) DeleteImage(ctx context.Context, in *DeleteImageRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, ImageStorage_DeleteImage_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ImageStorageServer is the server API for ImageStorage service. +// All implementations must embed UnimplementedImageStorageServer +// for forward compatibility. +type ImageStorageServer interface { + UploadImage(context.Context, *UploadMerchImageRequest) (*UploadMerchImageResponse, error) + DeleteImage(context.Context, *DeleteImageRequest) (*emptypb.Empty, error) + mustEmbedUnimplementedImageStorageServer() +} + +// UnimplementedImageStorageServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedImageStorageServer struct{} + +func (UnimplementedImageStorageServer) UploadImage(context.Context, *UploadMerchImageRequest) (*UploadMerchImageResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method UploadImage not implemented") +} +func (UnimplementedImageStorageServer) DeleteImage(context.Context, *DeleteImageRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method DeleteImage not implemented") +} +func (UnimplementedImageStorageServer) mustEmbedUnimplementedImageStorageServer() {} +func (UnimplementedImageStorageServer) testEmbeddedByValue() {} + +// UnsafeImageStorageServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ImageStorageServer will +// result in compilation errors. +type UnsafeImageStorageServer interface { + mustEmbedUnimplementedImageStorageServer() +} + +func RegisterImageStorageServer(s grpc.ServiceRegistrar, srv ImageStorageServer) { + // If the following call pancis, it indicates UnimplementedImageStorageServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&ImageStorage_ServiceDesc, srv) +} + +func _ImageStorage_UploadImage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(UploadMerchImageRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ImageStorageServer).UploadImage(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: ImageStorage_UploadImage_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ImageStorageServer).UploadImage(ctx, req.(*UploadMerchImageRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _ImageStorage_DeleteImage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(DeleteImageRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ImageStorageServer).DeleteImage(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: ImageStorage_DeleteImage_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ImageStorageServer).DeleteImage(ctx, req.(*DeleteImageRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ImageStorage_ServiceDesc is the grpc.ServiceDesc for ImageStorage service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ImageStorage_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "imageStorage.ImageStorage", + HandlerType: (*ImageStorageServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "UploadImage", + Handler: _ImageStorage_UploadImage_Handler, + }, + { + MethodName: "DeleteImage", + Handler: _ImageStorage_DeleteImage_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "imageStorage.proto", +} From e90852cc950a5845bff08705c8ef2ffb6ade0a3f Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:53:14 +0300 Subject: [PATCH 60/87] factor out tp methods --- internal/grpcService/handler.go | 92 +------------------------- internal/grpcService/taskProcessor.go | 95 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 90 deletions(-) create mode 100644 internal/grpcService/taskProcessor.go diff --git a/internal/grpcService/handler.go b/internal/grpcService/handler.go index 6af20cf..1ec9841 100644 --- a/internal/grpcService/handler.go +++ b/internal/grpcService/handler.go @@ -1,106 +1,18 @@ package grpcService import ( - log "github.com/sirupsen/logrus" "google.golang.org/grpc" - "google.golang.org/protobuf/types/known/emptypb" - "io" "merch-parser-api/internal/interfaces" - "merch-parser-api/internal/shared" pb "merch-parser-api/proto/taskProcessor" - "time" ) -type repoServer struct { - pb.UnimplementedTaskProcessorServer - taskProvider interfaces.TaskProvider -} - func NewGrpcServer(taskProvider interfaces.TaskProvider) *grpc.Server { srv := grpc.NewServer() + repoSrv := &repoServer{ taskProvider: taskProvider, } - pb.RegisterTaskProcessorServer(srv, repoSrv) + return srv } - -func (r *repoServer) RequestTask(_ *emptypb.Empty, stream pb.TaskProcessor_RequestTaskServer) error { - tasks, err := r.taskProvider.PrepareTasks() - if err != nil { - log.WithField("err", err).Error("gRPC Server | Request task error") - return err - } - - for _, task := range tasks { - if err = stream.Send(&pb.Task{ - MerchUuid: task.MerchUuid, - OriginSurugayaLink: task.OriginSurugayaLink, - OriginMandarakeLink: task.OriginMandarakeLink, - }); err != nil { - log.WithField("err", err).Error("gRPC Server | Stream send error") - return err - } - } - return nil -} - -func (r *repoServer) SendResult(stream pb.TaskProcessor_SendResultServer) error { - saveInterval := time.Second * 2 - batch := make([]shared.TaskResult, 0) - - ticker := time.NewTicker(saveInterval) - defer ticker.Stop() - - done := make(chan struct{}) - - go func() { - for { - select { - case <-done: - return - case <-ticker.C: - if len(batch) > 0 { - err := r.taskProvider.InsertPrices(batch) - if err != nil { - log.WithField("err", err).Error("gRPC Server | Batch insert") - } - } - } - } - }() - - for { - response, err := stream.Recv() - if err == io.EOF { - log.Debug("gRPC EOF") - break - } - - if err != nil { - log.WithField("err", err).Error("gRPC Server | Receive") - return err - } - - entry := shared.TaskResult{ - MerchUuid: response.MerchUuid, - Origin: response.OriginName, - Price: response.Price, - } - - batch = append(batch, entry) - log.WithField("response", entry).Debug("gRPC Server | Receive success") - } - - close(done) - if len(batch) > 0 { - err := r.taskProvider.InsertPrices(batch) - if err != nil { - log.WithField("err", err).Error("gRPC Server | Last data batch insert") - return err - } - } - - return nil -} diff --git a/internal/grpcService/taskProcessor.go b/internal/grpcService/taskProcessor.go new file mode 100644 index 0000000..93986bb --- /dev/null +++ b/internal/grpcService/taskProcessor.go @@ -0,0 +1,95 @@ +package grpcService + +import ( + log "github.com/sirupsen/logrus" + "google.golang.org/protobuf/types/known/emptypb" + "io" + "merch-parser-api/internal/interfaces" + "merch-parser-api/internal/shared" + pb "merch-parser-api/proto/taskProcessor" + "time" +) + +type repoServer struct { + pb.UnimplementedTaskProcessorServer + taskProvider interfaces.TaskProvider +} + +func (r *repoServer) RequestTask(_ *emptypb.Empty, stream pb.TaskProcessor_RequestTaskServer) error { + tasks, err := r.taskProvider.PrepareTasks() + if err != nil { + log.WithField("err", err).Error("gRPC Server | Request task error") + return err + } + + for _, task := range tasks { + if err = stream.Send(&pb.Task{ + MerchUuid: task.MerchUuid, + OriginSurugayaLink: task.OriginSurugayaLink, + OriginMandarakeLink: task.OriginMandarakeLink, + }); err != nil { + log.WithField("err", err).Error("gRPC Server | Stream send error") + return err + } + } + return nil +} + +func (r *repoServer) SendResult(stream pb.TaskProcessor_SendResultServer) error { + saveInterval := time.Second * 2 + batch := make([]shared.TaskResult, 0) + + ticker := time.NewTicker(saveInterval) + defer ticker.Stop() + + done := make(chan struct{}) + + go func() { + for { + select { + case <-done: + return + case <-ticker.C: + if len(batch) > 0 { + err := r.taskProvider.InsertPrices(batch) + if err != nil { + log.WithField("err", err).Error("gRPC Server | Batch insert") + } + } + } + } + }() + + for { + response, err := stream.Recv() + if err == io.EOF { + log.Debug("gRPC EOF") + break + } + + if err != nil { + log.WithField("err", err).Error("gRPC Server | Receive") + return err + } + + entry := shared.TaskResult{ + MerchUuid: response.MerchUuid, + Origin: response.OriginName, + Price: response.Price, + } + + batch = append(batch, entry) + log.WithField("response", entry).Debug("gRPC Server | Receive success") + } + + close(done) + if len(batch) > 0 { + err := r.taskProvider.InsertPrices(batch) + if err != nil { + log.WithField("err", err).Error("gRPC Server | Last data batch insert") + return err + } + } + + return nil +} From a3fbd3b8e05df5580ba0041347841043fff9365e Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:53:32 +0300 Subject: [PATCH 61/87] error handling --- internal/mediaStorage/service.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/mediaStorage/service.go b/internal/mediaStorage/service.go index a67a20c..72c2cc1 100644 --- a/internal/mediaStorage/service.go +++ b/internal/mediaStorage/service.go @@ -44,6 +44,9 @@ func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.R func (s *Service) GetPublicLink(ctx context.Context, bucket, object string) (string, string, error) { stat, err := s.client.StatObject(ctx, bucket, object, minio.StatObjectOptions{}) if err != nil { + if err.Error() == minio.ToErrorResponse(err).Error() { + return "", "", nil + } log.WithFields(log.Fields{ "error": err, "key": bucket + "/" + object, From fa8990ed8c499baa93dc45473133749b5bea5456 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:53:49 +0300 Subject: [PATCH 62/87] new image provider --- internal/imagesProvider/handler.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 internal/imagesProvider/handler.go diff --git a/internal/imagesProvider/handler.go b/internal/imagesProvider/handler.go new file mode 100644 index 0000000..622dec2 --- /dev/null +++ b/internal/imagesProvider/handler.go @@ -0,0 +1,27 @@ +package imagesProvider + +import ( + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + is "merch-parser-api/proto/imageStorage" +) + +type Handler struct{} + +func NewClient(address string) is.ImageStorageClient { + var opts []grpc.DialOption + insec := grpc.WithTransportCredentials(insecure.NewCredentials()) + opts = append(opts, insec) + + conn, err := grpc.NewClient(address, opts...) + if err != nil { + log.Fatal(err) + } + + log.WithFields(log.Fields{ + "address": address, + }).Debug("gRPC | API client") + + return is.NewImageStorageClient(conn) +} From f5ca21ca68a43cc4463982486656fb37d29c2c70 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:54:10 +0300 Subject: [PATCH 63/87] deprecated comment --- internal/interfaces/mediaStorage.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/interfaces/mediaStorage.go b/internal/interfaces/mediaStorage.go index 64fb50b..5ad375a 100644 --- a/internal/interfaces/mediaStorage.go +++ b/internal/interfaces/mediaStorage.go @@ -7,6 +7,8 @@ import ( "time" ) +// MinIO service replaced by imagesProvider + type MediaStorage interface { CheckBucketExists(bucketName string) (bool, error) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error From 212ce0a5c4c755e61a6f6194725e9f418cda48bc Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:54:34 +0300 Subject: [PATCH 64/87] image storage added --- cmd/main.go | 10 ++- config/config.go | 11 +++ internal/api/merch/controller.go | 114 ++++++++++++++++++------------- internal/api/merch/handler.go | 16 +++-- internal/api/merch/repository.go | 1 + internal/api/merch/service.go | 103 +++++++++++++++++++++++++--- 6 files changed, 190 insertions(+), 65 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 3fb0010..8c0cb26 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -9,6 +9,7 @@ import ( "merch-parser-api/internal/api/user" "merch-parser-api/internal/app" "merch-parser-api/internal/grpcService" + "merch-parser-api/internal/imagesProvider" "merch-parser-api/internal/interfaces" "merch-parser-api/internal/mediaStorage" "merch-parser-api/internal/provider/auth" @@ -62,6 +63,8 @@ func main() { "provider": mediaProvider, }).Debug("Media storage | Minio client created") + imageProvider := imagesProvider.NewClient(c.ImageConf.Host + ":" + c.ImageConf.Port) + //deps providers routerHandler := router.NewRouter(router.Deps{ ApiPrefix: c.AppConf.ApiPrefix, @@ -91,9 +94,10 @@ func main() { }) merchModule := merch.NewHandler(merch.Deps{ - DB: database, - Utils: utilsProvider, - Media: mediaProvider, + DB: database, + Utils: utilsProvider, + Media: mediaProvider, + ImageStorage: imageProvider, }) //collect modules diff --git a/config/config.go b/config/config.go index 5f2d4d5..33be20e 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ type Config struct { JWTConf JWTConfig GrpcConf GrpcConfig MediaConf MediaConfig + ImageConf ImageStorageConfig } type AppConfig struct { @@ -48,6 +49,11 @@ type MediaConfig struct { Secure string } +type ImageStorageConfig struct { + Host string + Port string +} + func NewConfig() *Config { return &Config{ AppConf: AppConfig{ @@ -87,5 +93,10 @@ func NewConfig() *Config { Password: getEnv("MEDIA_STORAGE_PASSWORD", ""), Secure: getEnv("MEDIA_STORAGE_SECURE", ""), }, + + ImageConf: ImageStorageConfig{ + Host: getEnv("IMAGE_STORAGE_HOST", ""), + Port: getEnv("IMAGE_STORAGE_PORT", ""), + }, } } diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 3fc8124..06fd408 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -266,16 +266,15 @@ func (co *controller) getDistinctPrices(c *gin.Context) { c.JSON(http.StatusOK, response) } -// @Summary Загрузить картинки по merch_uuid и query параметрам -// @Description Загрузить картинки по merch_uuid и query параметрам +// @Summary Загрузить картинку по merch_uuid +// @Description Загрузить картинку по merch_uuid. В ответ будут выданы ссылки на созданные картинки. // @Tags Merch images // @Security BearerAuth // @Accept multipart/form-data // @Produce json -// @Param uuid path string true "Merch UUID" -// @Param file formData file true "Image file" -// @Param imageType formData string true "Image type: thumbnail, full or all" Enums(thumbnail, full, all) -// @Success 200 +// @Param uuid path string true "Merch UUID" +// @Param file formData file true "Image file" +// @Success 200 {object} imageStorage.UploadMerchImageResponse // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/images/{uuid} [post] @@ -294,13 +293,14 @@ func (co *controller) uploadMerchImage(c *gin.Context) { return } - imageType := c.PostForm("imageType") - types := map[string]struct{}{"thumbnail": {}, "full": {}, "all": {}} - if _, allowed := types[imageType]; !allowed { - c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "imageType must be one of: thumbnail, full, all"}) - log.WithError(err).Error("Merch | imageType must be one of: thumbnail, full, all") - return - } + //Uncomment for MinIO use + //imageType := c.PostForm("imageType") + //types := map[string]struct{}{"thumbnail": {}, "full": {}, "all": {}} + //if _, allowed := types[imageType]; !allowed { + // c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "imageType must be one of: thumbnail, full, all"}) + // log.WithError(err).Error("Merch | imageType must be one of: thumbnail, full, all") + // return + //} file, err := c.FormFile("file") if err != nil { @@ -312,14 +312,17 @@ func (co *controller) uploadMerchImage(c *gin.Context) { ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) defer cancel() - err = co.service.uploadMerchImage(ctx, userUuid, merchUuid, imageType, file) + //Uncomment for MinIO use + //err = co.service.uploadMerchImage(ctx, userUuid, merchUuid, imageType, file) + response, err := co.service.mtUploadMerchImage(ctx, userUuid, merchUuid, file) if err != nil { c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) log.WithError(err).Error("Merch | Failed to upload merch image") return } - c.Status(http.StatusOK) + //c.Status(http.StatusOK) + c.JSON(http.StatusOK, response) } // @Summary Получить картинки по merch_uuid и query параметрам @@ -333,43 +336,53 @@ func (co *controller) uploadMerchImage(c *gin.Context) { // @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/images/{uuid} [get] func (co *controller) getMerchImage(c *gin.Context) { - typeQuery := strings.ToLower(c.Query("type")) - if typeQuery == "" { - c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "Image type query param is empty"}) - return - } + //Uncomment for MinIO use - 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 - } - - merchUuid := c.Param("uuid") - if merchUuid == "" { - c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"}) - log.WithError(err).Error("Merch | Failed to get single merch") - return - } - - ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) - defer cancel() - - link, err := co.service.getPublicImageLink(ctx, userUuid, merchUuid, typeQuery) - if err != nil { - c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) - log.WithError(err).Error("Merch | Failed to get merch image") - return - } - c.JSON(http.StatusOK, link) + //typeQuery := strings.ToLower(c.Query("type")) + //if typeQuery == "" { + // c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "Image type query param 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 + //} + // + //merchUuid := c.Param("uuid") + //if merchUuid == "" { + // c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"}) + // log.WithError(err).Error("Merch | Failed to get single merch") + // return + //} + // + //ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) + //defer cancel() + // + //link, err := co.service.getPublicImageLink(ctx, userUuid, merchUuid, typeQuery) + //if err != nil { + // c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + // log.WithError(err).Error("Merch | Failed to get merch image") + // return + //} + // + //if link.Link == "" { + // log.Debug("Merch | No image") + // c.Status(http.StatusNoContent) + // return + //} + // + //c.JSON(http.StatusOK, link) + c.JSON(http.StatusNotImplemented, gin.H{"msg": "Method deprecated. Request images from image storage."}) } -// @Summary Удалить (безвозвратно) картинки по merch_uuid и query параметрам -// @Description Удалить (безвозвратно) картинки по merch_uuid и query параметрам +// @Summary Удалить (безвозвратно) картинки по merch_uuid +// @Description Удалить (безвозвратно) картинки по merch_uuid // @Tags Merch images // @Security BearerAuth -// @Param uuid path string true "merch_uuid" +// @Param uuid path string true "merch_uuid" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 @@ -385,14 +398,17 @@ func (co *controller) deleteMerchImage(c *gin.Context) { merchUuid := c.Param("uuid") if merchUuid == "" { c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "MerchUuid is empty"}) - log.WithError(err).Error("Merch | Failed to get single merch") + log.WithError(err).Error("Merch | Failed to get merch uuid") return } ctx, cancel := context.WithTimeout(c.Request.Context(), co.expires) defer cancel() - if err := co.service.deleteMerchImage(ctx, userUuid, merchUuid); err != nil { + //Uncomment for MinIO use + //if err := co.service.deleteMerchImage(ctx, userUuid, merchUuid); err != nil { + + if err := co.service.mtDeleteMerchImage(ctx, userUuid, merchUuid); err != nil { c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) log.WithError(err).Error("Merch | Failed to delete merch image") return diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index a442f9a..6918be0 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -4,6 +4,7 @@ import ( log "github.com/sirupsen/logrus" "gorm.io/gorm" "merch-parser-api/internal/interfaces" + is "merch-parser-api/proto/imageStorage" "time" ) @@ -14,9 +15,10 @@ type Handler struct { } type Deps struct { - DB *gorm.DB - Utils interfaces.Utils - Media interfaces.MediaStorage + DB *gorm.DB + Utils interfaces.Utils + Media interfaces.MediaStorage + ImageStorage is.ImageStorageClient } func NewHandler(deps Deps) *Handler { @@ -24,7 +26,13 @@ func NewHandler(deps Deps) *Handler { expires := time.Minute * 5 r := NewRepo(deps.DB) - s := newService(r, deps.Media, packageBucketName, expires) + s := newService(serviceDeps{ + repo: r, + media: deps.Media, + bucketName: packageBucketName, + expires: expires, + imageStorage: deps.ImageStorage, + }) c := newController(s, deps.Utils, expires) media := deps.Media diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 3c4ccfc..98d2695 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -63,6 +63,7 @@ func (r *Repo) merchRecordExists(userUuid, merchUuid string) (bool, error) { FROM merch WHERE user_uuid = ? AND merch_uuid = ? + AND deleted_at IS NULL );`, userUuid, merchUuid).Scan(&exists).Error return exists, err diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 481ca60..e749d88 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -13,6 +13,7 @@ import ( "image/jpeg" "io" "merch-parser-api/internal/interfaces" + is "merch-parser-api/proto/imageStorage" "mime/multipart" "path/filepath" "strings" @@ -20,18 +21,28 @@ import ( ) type service struct { - repo repository - media interfaces.MediaStorage - bucketName string - expires time.Duration + repo repository + media interfaces.MediaStorage + bucketName string + expires time.Duration + imageStorage is.ImageStorageClient } -func newService(repo repository, media interfaces.MediaStorage, bucketName string, expires time.Duration) *service { +type serviceDeps struct { + repo repository + media interfaces.MediaStorage + bucketName string + expires time.Duration + imageStorage is.ImageStorageClient +} + +func newService(deps serviceDeps) *service { return &service{ - repo: repo, - media: media, - bucketName: bucketName, - expires: expires, + repo: deps.repo, + media: deps.media, + bucketName: deps.bucketName, + expires: deps.expires, + imageStorage: deps.imageStorage, } } @@ -210,6 +221,9 @@ func (s *service) getDistinctPrices(userUuid, merchUuid, days string) (PricesRes }, nil } +// uploadMerchImage +// Deprecated. +// Use only with MinIO storage. Use mtUploadMerchImage for merch-tracker images storage. func (s *service) uploadMerchImage(ctx context.Context, userUuid, merchUuid, imageType string, file *multipart.FileHeader) error { exists, err := s.repo.merchRecordExists(userUuid, merchUuid) if err != nil { @@ -303,6 +317,9 @@ func (s *service) uploadMerchImage(ctx context.Context, userUuid, merchUuid, ima return nil } +// getPublicImageLink +// Deprecated. +// Use only with MinIO storage. func (s *service) getPublicImageLink(ctx context.Context, userUuid, merchUuid, imageType string) (ImageLink, error) { object, err := s.makeObject(userUuid, merchUuid, imageType) if err != nil { @@ -320,6 +337,9 @@ func (s *service) getPublicImageLink(ctx context.Context, userUuid, merchUuid, i }, nil } +// getPresignedImageLink +// Deprecated. +// Use only with MinIO storage. func (s *service) getPresignedImageLink(ctx context.Context, userUuid, merchUuid, imageType string) (ImageLink, error) { exists, err := s.repo.merchRecordExists(userUuid, merchUuid) if err != nil { @@ -351,6 +371,9 @@ func (s *service) getPresignedImageLink(ctx context.Context, userUuid, merchUuid }, nil } +// deleteMerchImage +// Deprecated. +// Use only with MinIO storage. func (s *service) deleteMerchImage(ctx context.Context, userUuid, merchUuid string) error { exists, err := s.repo.merchRecordExists(userUuid, merchUuid) if err != nil { @@ -398,3 +421,65 @@ func (s *service) _uploadToStorage(params uploadImageParams) error { return nil } + +// mtUploadMerchImage +// Upload new/rewrite existing image to merch-tracker images storage +func (s *service) mtUploadMerchImage(ctx context.Context, userUuid, merchUuid string, file *multipart.FileHeader) (*is.UploadMerchImageResponse, error) { + const uploadMerchImage = "Merch service | Upload merch image" + + exists, err := s.repo.merchRecordExists(userUuid, merchUuid) + if err != nil { + log.WithError(err).Error(uploadMerchImage) + return nil, err + } + + if !exists { + err = fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) + log.WithError(err).Error(uploadMerchImage) + return nil, err + } + + f, err := file.Open() + if err != nil { + log.WithError(err).Error(uploadMerchImage) + return nil, err + } + defer f.Close() + + data, err := io.ReadAll(f) + if err != nil { + log.WithError(err).Error(uploadMerchImage) + return nil, err + } + + response, err := s.imageStorage.UploadImage(ctx, &is.UploadMerchImageRequest{ + ImageData: data, + UserUuid: userUuid, + MerchUuid: merchUuid, + }) + if err != nil { + log.WithError(err).Error(uploadMerchImage) + return nil, err + } + + return response, nil +} + +// mtDeleteMerchImage +// Delete all merch images for given user and merch uuid-s from merch-tracker images storage +func (s *service) mtDeleteMerchImage(ctx context.Context, userUuid, merchUuid string) error { + exists, err := s.repo.merchRecordExists(userUuid, merchUuid) + if err != nil { + return err + } + + if !exists { + return fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) + } + + s.imageStorage.DeleteImage(ctx, &is.DeleteImageRequest{ + UserUuid: userUuid, + MerchUuid: merchUuid, + }) + return nil +} From f13012b742c4990b46dc46f252c6cf1f01924247 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:54:43 +0300 Subject: [PATCH 65/87] update --- api.env | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api.env b/api.env index 8bb1d03..2baadc0 100644 --- a/api.env +++ b/api.env @@ -8,6 +8,9 @@ APP_ALLOWED_ORIGINS=http://localhost:5173, GRPC_SERVER_PORT=9050 GRPC_CLIENT_PORT=9060 +IMAGE_STORAGE_HOST= +IMAGE_STORAGE_PORT= + MEDIA_STORAGE_ENDPOINT= MEDIA_STORAGE_USER= MEDIA_STORAGE_PASSWORD= From 37a1dfbf52b0d327aac40dc3f57725af46bb270c Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:54:51 +0300 Subject: [PATCH 66/87] swagger docs update --- docs/docs.go | 36 +++++++++++++++++++----------------- docs/swagger.json | 36 +++++++++++++++++++----------------- docs/swagger.yaml | 27 ++++++++++++++------------- 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 769c22b..8e556d2 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -192,7 +192,7 @@ const docTemplate = `{ "BearerAuth": [] } ], - "description": "Загрузить картинки по merch_uuid и query параметрам", + "description": "Загрузить картинку по merch_uuid. В ответ будут выданы ссылки на созданные картинки.", "consumes": [ "multipart/form-data" ], @@ -202,7 +202,7 @@ const docTemplate = `{ "tags": [ "Merch images" ], - "summary": "Загрузить картинки по merch_uuid и query параметрам", + "summary": "Загрузить картинку по merch_uuid", "parameters": [ { "type": "string", @@ -217,23 +217,14 @@ const docTemplate = `{ "name": "file", "in": "formData", "required": true - }, - { - "enum": [ - "thumbnail", - "full", - "all" - ], - "type": "string", - "description": "Image type: thumbnail, full or all", - "name": "imageType", - "in": "formData", - "required": true } ], "responses": { "200": { - "description": "OK" + "description": "OK", + "schema": { + "$ref": "#/definitions/imageStorage.UploadMerchImageResponse" + } }, "400": { "description": "Bad Request", @@ -255,11 +246,11 @@ const docTemplate = `{ "BearerAuth": [] } ], - "description": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "description": "Удалить (безвозвратно) картинки по merch_uuid", "tags": [ "Merch images" ], - "summary": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "summary": "Удалить (безвозвратно) картинки по merch_uuid", "parameters": [ { "type": "string", @@ -768,6 +759,17 @@ const docTemplate = `{ } }, "definitions": { + "imageStorage.UploadMerchImageResponse": { + "type": "object", + "properties": { + "fullImage": { + "type": "string" + }, + "thumbnail": { + "type": "string" + } + } + }, "merch.ImageLink": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index d333c5a..ea5dbf2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -184,7 +184,7 @@ "BearerAuth": [] } ], - "description": "Загрузить картинки по merch_uuid и query параметрам", + "description": "Загрузить картинку по merch_uuid. В ответ будут выданы ссылки на созданные картинки.", "consumes": [ "multipart/form-data" ], @@ -194,7 +194,7 @@ "tags": [ "Merch images" ], - "summary": "Загрузить картинки по merch_uuid и query параметрам", + "summary": "Загрузить картинку по merch_uuid", "parameters": [ { "type": "string", @@ -209,23 +209,14 @@ "name": "file", "in": "formData", "required": true - }, - { - "enum": [ - "thumbnail", - "full", - "all" - ], - "type": "string", - "description": "Image type: thumbnail, full or all", - "name": "imageType", - "in": "formData", - "required": true } ], "responses": { "200": { - "description": "OK" + "description": "OK", + "schema": { + "$ref": "#/definitions/imageStorage.UploadMerchImageResponse" + } }, "400": { "description": "Bad Request", @@ -247,11 +238,11 @@ "BearerAuth": [] } ], - "description": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "description": "Удалить (безвозвратно) картинки по merch_uuid", "tags": [ "Merch images" ], - "summary": "Удалить (безвозвратно) картинки по merch_uuid и query параметрам", + "summary": "Удалить (безвозвратно) картинки по merch_uuid", "parameters": [ { "type": "string", @@ -760,6 +751,17 @@ } }, "definitions": { + "imageStorage.UploadMerchImageResponse": { + "type": "object", + "properties": { + "fullImage": { + "type": "string" + }, + "thumbnail": { + "type": "string" + } + } + }, "merch.ImageLink": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index c3f516b..1afef07 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,5 +1,12 @@ basePath: /api/v2 definitions: + imageStorage.UploadMerchImageResponse: + properties: + fullImage: + type: string + thumbnail: + type: string + type: object merch.ImageLink: properties: etag: @@ -271,7 +278,7 @@ paths: - Merch /merch/images/{uuid}: delete: - description: Удалить (безвозвратно) картинки по merch_uuid и query параметрам + description: Удалить (безвозвратно) картинки по merch_uuid parameters: - description: merch_uuid in: path @@ -291,7 +298,7 @@ paths: $ref: '#/definitions/responses.ErrorResponse500' security: - BearerAuth: [] - summary: Удалить (безвозвратно) картинки по merch_uuid и query параметрам + summary: Удалить (безвозвратно) картинки по merch_uuid tags: - Merch images get: @@ -328,7 +335,8 @@ paths: post: consumes: - multipart/form-data - description: Загрузить картинки по merch_uuid и query параметрам + description: Загрузить картинку по merch_uuid. В ответ будут выданы ссылки на + созданные картинки. parameters: - description: Merch UUID in: path @@ -340,20 +348,13 @@ paths: name: file required: true type: file - - description: 'Image type: thumbnail, full or all' - enum: - - thumbnail - - full - - all - in: formData - name: imageType - required: true - type: string produces: - application/json responses: "200": description: OK + schema: + $ref: '#/definitions/imageStorage.UploadMerchImageResponse' "400": description: Bad Request schema: @@ -364,7 +365,7 @@ paths: $ref: '#/definitions/responses.ErrorResponse500' security: - BearerAuth: [] - summary: Загрузить картинки по merch_uuid и query параметрам + summary: Загрузить картинку по merch_uuid tags: - Merch images /prices: From 7937d182db6d8d19daf4466b39c76b6003e23766 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 19:55:49 +0300 Subject: [PATCH 67/87] swagger docs update --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 9fbbf8e..be8c94b 100644 --- a/go.mod +++ b/go.mod @@ -52,7 +52,7 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/compress v1.18.0 // indirect + github.com/klauspost/compress v1.18.1 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -79,5 +79,5 @@ require ( golang.org/x/sys v0.37.0 // indirect golang.org/x/text v0.30.0 // indirect golang.org/x/tools v0.38.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8 // indirect ) diff --git a/go.sum b/go.sum index bdf49df..84f9202 100644 --- a/go.sum +++ b/go.sum @@ -89,8 +89,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= -github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co= +github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0= github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= @@ -217,8 +217,8 @@ golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f h1:1FTH6cpXFsENbPR5Bu8NQddPSaUUE6NA2XdZdDSAJK4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8 h1:M1rk8KBnUsBDg1oPGHNCxG4vc1f49epmTO7xscSajMk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= google.golang.org/grpc v1.76.0 h1:UnVkv1+uMLYXoIz6o7chp59WfQUYA2ex/BXQ9rHZu7A= google.golang.org/grpc v1.76.0/go.mod h1:Ju12QI8M6iQJtbcsV+awF5a4hfJMLi4X0JLo94ULZ6c= google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= From 489f749ce3b3d609b4e7813399c1727dd2042817 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 26 Oct 2025 21:59:49 +0300 Subject: [PATCH 68/87] minio disabled --- cmd/main.go | 6 +++--- internal/api/merch/handler.go | 29 ++++++++++++++--------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8c0cb26..ce397b1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -94,9 +94,9 @@ func main() { }) merchModule := merch.NewHandler(merch.Deps{ - DB: database, - Utils: utilsProvider, - Media: mediaProvider, + DB: database, + Utils: utilsProvider, + //Media: mediaProvider, ImageStorage: imageProvider, }) diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index 6918be0..0239179 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -1,7 +1,6 @@ package merch import ( - log "github.com/sirupsen/logrus" "gorm.io/gorm" "merch-parser-api/internal/interfaces" is "merch-parser-api/proto/imageStorage" @@ -15,9 +14,9 @@ type Handler struct { } type Deps struct { - DB *gorm.DB - Utils interfaces.Utils - Media interfaces.MediaStorage + DB *gorm.DB + Utils interfaces.Utils + //Media interfaces.MediaStorage ImageStorage is.ImageStorageClient } @@ -27,23 +26,23 @@ func NewHandler(deps Deps) *Handler { r := NewRepo(deps.DB) s := newService(serviceDeps{ - repo: r, - media: deps.Media, + repo: r, + //media: deps.Media, bucketName: packageBucketName, expires: expires, imageStorage: deps.ImageStorage, }) c := newController(s, deps.Utils, expires) - media := deps.Media - log.WithFields(log.Fields{ - "addr": media, - }).Debug("Merch handler constructor | Media provider") - - exists, err := media.CheckBucketExists(packageBucketName) - if err != nil || !exists { - log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") - } + //media := deps.Media + //log.WithFields(log.Fields{ + // "addr": media, + //}).Debug("Merch handler constructor | Media provider") + // + //exists, err := media.CheckBucketExists(packageBucketName) + //if err != nil || !exists { + // log.WithError(err).Fatal("Merch handler constructor | Failed to ensure bucket exists") + //} return &Handler{ repo: r, From 475ff9919bce9247f40c917e10dd13165dbf9ec1 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 18:22:40 +0300 Subject: [PATCH 69/87] tables added --- migrations.sql | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/migrations.sql b/migrations.sql index c4df99c..bb270bc 100644 --- a/migrations.sql +++ b/migrations.sql @@ -55,4 +55,22 @@ CREATE TABLE prices( merch_uuid VARCHAR(36) NOT NULL, price INT NULL, origin INT -); \ No newline at end of file +); + +CREATE TABLE labels( + id BIGSERIAL PRIMARY KEY, + created_at TIMESTAMP WITH TIME ZONE NOT NULL, + updated_at TIMESTAMP WITH TIME ZONE NULL, + deleted_at TIMESTAMP WITH TIME ZONE NULL, + user_uuid VARCHAR(36) NOT NULL, + label_uuid VARCHAR(36) NOT NULL, + name VARCHAR(255), + color VARCHAR(32), + bg_color VARCHAR(32) +); + +CREATE TABLE card_label ( + user_uuid VARCHAR(36) NOT NULL, + label_uuid VARCHAR(36) NOT NULL, + merch_uuid VARCHAR(36) NOT NULL +); From 9895b86666df24a816bb88be33b2a8c8bcffa15d Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:06:32 +0300 Subject: [PATCH 70/87] labels crud added --- internal/api/merch/controller.go | 223 +++++++++++++++++++++++++++++++ internal/api/merch/dto.go | 12 ++ internal/api/merch/model.go | 18 +++ internal/api/merch/repository.go | 48 +++++++ internal/api/merch/service.go | 87 ++++++++++++ 5 files changed, 388 insertions(+) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 06fd408..ddf3bd6 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -2,6 +2,7 @@ package merch import ( "context" + "errors" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-parser-api/internal/interfaces" @@ -42,6 +43,14 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref imagesGroup.POST("/:uuid", h.controller.uploadMerchImage) imagesGroup.GET("/:uuid", h.controller.getMerchImage) imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage) + + labelsGroup := merchGroup.Group("/labels") + labelsGroup.POST("/", h.controller.createLabel) + labelsGroup.GET("/", h.controller.getLabels) + labelsGroup.PUT("/:uuid", h.controller.updateLabel) + labelsGroup.DELETE("/:uuid", h.controller.deleteLabel) + labelsGroup.POST("/attach", h.controller.attachLabel) + labelsGroup.POST("/detach", h.controller.detachLabel) } // @Summary Добавить новый мерч @@ -415,3 +424,217 @@ func (co *controller) deleteMerchImage(c *gin.Context) { } c.Status(http.StatusOK) } + +// @Summary Создать новую метку для товара +// @Description Создать новую метку для товара +// @Tags Merch labels +// @Security BearerAuth +// @Param payload body LabelDTO true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels [post] +func (co *controller) createLabel(c *gin.Context) { + const logMsg = "Merch | Create label" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + var payload LabelDTO + if err = c.ShouldBindJSON(&payload); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.createLabel(payload, userUuid); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + c.Status(http.StatusOK) +} + +// @Summary Получить все метки товаров +// @Description Получить все метки товаров +// @Tags Merch labels +// @Security BearerAuth +// @Success 200 {array} LabelDTO +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels [get] +func (co *controller) getLabels(c *gin.Context) { + const logMsg = "Merch | Get labels" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + response, err := co.service.getLabels(userUuid) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + c.JSON(http.StatusOK, response) +} + +// @Summary Изменить метку +// @Description Изменить метку +// @Tags Merch labels +// @Security BearerAuth +// @Param uuid path string true "label uuid" +// @Param payload body LabelDTO true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/{uuid} [put] +func (co *controller) updateLabel(c *gin.Context) { + const logMsg = "Merch | Update label" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + labelUuid := c.Param("uuid") + if labelUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "label uuid is empty"}) + log.WithError(err).Error(logMsg) + return + } + + var payload LabelDTO + if err = c.ShouldBindJSON(&payload); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if labelUuid != payload.LabelUuid { + err = errors.New("label uuid is different") + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.updateLabel(userUuid, payload); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.Status(http.StatusOK) +} + +// @Summary Пометить метку как удаленную +// @Description Пометить метку как удаленную +// @Tags Merch labels +// @Security BearerAuth +// @Param uuid path string true "label uuid" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/{uuid} [delete] +func (co *controller) deleteLabel(c *gin.Context) { + const logMsg = "Merch | Delete label" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + labelUuid := c.Param("uuid") + if labelUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "label uuid is empty"}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.deleteLabel(userUuid, labelUuid); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.Status(http.StatusOK) +} + +// @Summary Прикрепить метку к товару +// @Description Прикрепить метку к товару +// @Tags Merch labels +// @Security BearerAuth +// @Param payload body LabelLink true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/attach [post] +func (co *controller) attachLabel(c *gin.Context) { + const logMsg = "Merch | Attach label" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + var payload LabelLink + if err = c.ShouldBindJSON(&payload); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.attachLabel(userUuid, payload); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.Status(http.StatusOK) +} + +// @Summary Удалить привязку метки к товару +// @Description Удалить привязку метки к товару +// @Tags Merch labels +// @Security BearerAuth +// @Param payload body LabelLink true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/detach [post] +func (co *controller) detachLabel(c *gin.Context) { + const logMsg = "Merch | Detach label" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + var payload LabelLink + if err = c.ShouldBindJSON(&payload); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.detachLabel(userUuid, payload); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.Status(http.StatusOK) +} diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index bc1646e..fc1b077 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -58,3 +58,15 @@ type ImageLink struct { Link string `json:"link"` ETag string `json:"etag"` } + +type LabelDTO struct { + LabelUuid string `json:"label_uuid"` + Name string `json:"name"` + Color string `json:"color"` + BgColor string `json:"bg_color"` +} + +type LabelLink struct { + MerchUuid string `json:"merch_uuid"` + LabelUuid string `json:"label_uuid"` +} diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 9322547..088cb39 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -50,3 +50,21 @@ type Price struct { Price int `json:"price" gorm:"column:price"` Origin Origin `json:"origin" gorm:"column:origin;type:integer"` } + +type Label struct { + Id uint `json:"-" gorm:"primary_key"` + CreatedAt time.Time `json:"created_at" gorm:"column:created_at"` + UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"` + DeletedAt sql.NullTime `json:"deleted_at" gorm:"column:deleted_at"` + LabelUuid string `json:"label_uuid" gorm:"column:label_uuid"` + UserUuid string `json:"user_uuid" gorm:"column:user_uuid"` + Name string `json:"name" gorm:"column:name"` + Color string `json:"color" gorm:"column:color"` + BgColor string `json:"bg_color" gorm:"column:bg_color"` +} + +type CardLabel struct { + LabelUuid string `json:"label_uuid"` + UserUuid string `json:"user_uuid"` + MerchUuid string `json:"merch_uuid"` +} diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 98d2695..24ff87b 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -32,6 +32,7 @@ type repository interface { getAllUserMerch(userUuid string) ([]Merch, error) prices + labels } type prices interface { @@ -39,6 +40,15 @@ type prices interface { getDistinctPrices(userUuid, merchUuid string, period time.Time) (prices []Price, err error) } +type labels interface { + createLabel(label Label) error + getLabels(userUuid string) ([]Label, error) + updateLabel(userUuid, labelUuid string, label map[string]string) error + deleteLabel(userUuid, labelUuid string) error + attachLabel(label CardLabel) error + detachLabel(label CardLabel) error +} + func (r *Repo) addMerch(bundle merchBundle) error { if err := r.db.Model(&Merch{}).Create(bundle.Merch).Error; err != nil { return err @@ -239,3 +249,41 @@ func (r *Repo) upsertOrigin(model any) error { DoUpdates: clause.AssignmentColumns([]string{"link"}), }).Create(model).Error } + +func (r *Repo) createLabel(label Label) error { + return r.db.Model(&Label{}).Create(label).Error +} +func (r *Repo) getLabels(userUuid string) ([]Label, error) { + var labels []Label + + if err := r.db. + Where("user_uuid = ?", userUuid). + Where("deleted_at IS NULL"). + Find(labels).Error; err != nil { + return nil, err + } + + return labels, nil +} + +func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]string) error { + return r.db.Model(&Label{}). + Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid). + Updates(label).Error +} + +func (r *Repo) deleteLabel(userUuid, labelUuid string) error { + return r.db.Model(&Label{}). + Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid). + Update("deleted_at", time.Now().UTC()).Error +} + +func (r *Repo) attachLabel(label CardLabel) error { + return r.db.Model(&CardLabel{}).Create(&label).Error +} + +func (r *Repo) detachLabel(label CardLabel) error { + return r.db. + Where("userUuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). + Delete(&CardLabel{}).Error +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index e749d88..bf49893 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -483,3 +483,90 @@ func (s *service) mtDeleteMerchImage(ctx context.Context, userUuid, merchUuid st }) return nil } + +func (s *service) createLabel(label LabelDTO, userUuid string) error { + now := time.Now().UTC() + + if label.Name == "" { + return fmt.Errorf("label name is required") + } + + newLabel := Label{ + CreatedAt: now, + UpdatedAt: now, + DeletedAt: sql.NullTime{Time: time.Time{}, Valid: false}, + LabelUuid: uuid.NewString(), + UserUuid: userUuid, + Name: label.Name, + Color: label.Color, + BgColor: label.BgColor, + } + + return s.repo.createLabel(newLabel) +} +func (s *service) getLabels(userUuid string) ([]LabelDTO, error) { + stored, err := s.repo.getLabels(userUuid) + if err != nil { + return nil, err + } + + response := make([]LabelDTO, 0, len(stored)) + for _, label := range stored { + response = append(response, LabelDTO{ + LabelUuid: label.LabelUuid, + Name: label.Name, + Color: label.Color, + BgColor: label.BgColor, + }) + } + + return response, nil +} +func (s *service) updateLabel(userUuid string, label LabelDTO) error { + updateMap := make(map[string]string, 3) + + if label.Name != "" { + updateMap["name"] = label.Name + } + + if label.Color != "" { + updateMap["color"] = label.Color + } + + if label.BgColor != "" { + updateMap["bgcolor"] = label.BgColor + } + + return s.repo.updateLabel(userUuid, label.LabelUuid, updateMap) +} + +func (s *service) deleteLabel(userUuid, labelUuid string) error { + return s.repo.deleteLabel(userUuid, labelUuid) +} + +func (s *service) attachLabel(userUuid string, label LabelLink) error { + if label.LabelUuid == "" || label.MerchUuid == "" { + return fmt.Errorf("both label and merch uuid-s are required") + } + + attach := CardLabel{ + LabelUuid: label.LabelUuid, + UserUuid: userUuid, + MerchUuid: label.MerchUuid, + } + + return s.repo.attachLabel(attach) +} + +func (s *service) detachLabel(userUuid string, label LabelLink) error { + if label.LabelUuid == "" || label.MerchUuid == "" { + return fmt.Errorf("both label and merch uuid-s are required") + } + + detach := CardLabel{ + LabelUuid: label.LabelUuid, + UserUuid: userUuid, + MerchUuid: label.MerchUuid, + } + return s.repo.detachLabel(detach) +} From 844561ef70ac62c9a952d7ecda7b5592a4e0e362 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:28:40 +0300 Subject: [PATCH 71/87] update --- migrations.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations.sql b/migrations.sql index bb270bc..b5fd81c 100644 --- a/migrations.sql +++ b/migrations.sql @@ -69,7 +69,7 @@ CREATE TABLE labels( bg_color VARCHAR(32) ); -CREATE TABLE card_label ( +CREATE TABLE card_labels ( user_uuid VARCHAR(36) NOT NULL, label_uuid VARCHAR(36) NOT NULL, merch_uuid VARCHAR(36) NOT NULL From f7ec1bce1e38563e20a9cf5b552c405689894b56 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:29:14 +0300 Subject: [PATCH 72/87] small fixes --- internal/api/merch/controller.go | 26 +++++++++----------------- internal/api/merch/dto.go | 10 ++++++++-- internal/api/merch/model.go | 8 ++++++++ internal/api/merch/repository.go | 16 +++++++++------- internal/api/merch/service.go | 12 ++++++------ 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index ddf3bd6..7f0ae7a 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -2,7 +2,6 @@ package merch import ( "context" - "errors" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-parser-api/internal/interfaces" @@ -45,8 +44,8 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage) labelsGroup := merchGroup.Group("/labels") - labelsGroup.POST("/", h.controller.createLabel) - labelsGroup.GET("/", h.controller.getLabels) + labelsGroup.POST("", h.controller.createLabel) + labelsGroup.GET("", h.controller.getLabels) labelsGroup.PUT("/:uuid", h.controller.updateLabel) labelsGroup.DELETE("/:uuid", h.controller.deleteLabel) labelsGroup.POST("/attach", h.controller.attachLabel) @@ -431,8 +430,8 @@ func (co *controller) deleteMerchImage(c *gin.Context) { // @Security BearerAuth // @Param payload body LabelDTO true "payload" // @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/labels [post] func (co *controller) createLabel(c *gin.Context) { const logMsg = "Merch | Create label" @@ -464,9 +463,9 @@ func (co *controller) createLabel(c *gin.Context) { // @Description Получить все метки товаров // @Tags Merch labels // @Security BearerAuth -// @Success 200 {array} LabelDTO -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Success 200 {array} LabelsList +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/labels [get] func (co *controller) getLabels(c *gin.Context) { const logMsg = "Merch | Get labels" @@ -492,7 +491,7 @@ func (co *controller) getLabels(c *gin.Context) { // @Description Изменить метку // @Tags Merch labels // @Security BearerAuth -// @Param uuid path string true "label uuid" +// @Param uuid path string true "label uuid" // @Param payload body LabelDTO true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -522,14 +521,7 @@ func (co *controller) updateLabel(c *gin.Context) { return } - if labelUuid != payload.LabelUuid { - err = errors.New("label uuid is different") - c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) - log.WithError(err).Error(logMsg) - return - } - - if err = co.service.updateLabel(userUuid, payload); err != nil { + if err = co.service.updateLabel(userUuid, labelUuid, payload); err != nil { c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) log.WithError(err).Error(logMsg) return diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index fc1b077..32e72e4 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -60,10 +60,16 @@ type ImageLink struct { } type LabelDTO struct { + Name string `json:"name"` + Color string `json:"color,omitempty"` + BgColor string `json:"bg_color,omitempty"` +} + +type LabelsList struct { LabelUuid string `json:"label_uuid"` Name string `json:"name"` - Color string `json:"color"` - BgColor string `json:"bg_color"` + Color string `json:"color,omitempty"` + BgColor string `json:"bg_color,omitempty"` } type LabelLink struct { diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 088cb39..935a194 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -63,8 +63,16 @@ type Label struct { BgColor string `json:"bg_color" gorm:"column:bg_color"` } +func (Label) TableName() string { + return "labels" +} + type CardLabel struct { LabelUuid string `json:"label_uuid"` UserUuid string `json:"user_uuid"` MerchUuid string `json:"merch_uuid"` } + +func (CardLabel) TableName() string { + return "card_labels" +} diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 24ff87b..5b3c308 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -43,7 +43,7 @@ type prices interface { type labels interface { createLabel(label Label) error getLabels(userUuid string) ([]Label, error) - updateLabel(userUuid, labelUuid string, label map[string]string) error + updateLabel(userUuid, labelUuid string, label map[string]any) error deleteLabel(userUuid, labelUuid string) error attachLabel(label CardLabel) error detachLabel(label CardLabel) error @@ -251,22 +251,24 @@ func (r *Repo) upsertOrigin(model any) error { } func (r *Repo) createLabel(label Label) error { - return r.db.Model(&Label{}).Create(label).Error + return r.db.Model(&Label{}).Create(&label).Error } + func (r *Repo) getLabels(userUuid string) ([]Label, error) { - var labels []Label + var labelsList []Label if err := r.db. + Model(&Label{}). Where("user_uuid = ?", userUuid). Where("deleted_at IS NULL"). - Find(labels).Error; err != nil { + Find(&labelsList).Error; err != nil { return nil, err } - return labels, nil + return labelsList, nil } -func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]string) error { +func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]any) error { return r.db.Model(&Label{}). Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid). Updates(label).Error @@ -284,6 +286,6 @@ func (r *Repo) attachLabel(label CardLabel) error { func (r *Repo) detachLabel(label CardLabel) error { return r.db. - Where("userUuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). + Where("user_uuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). Delete(&CardLabel{}).Error } diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index bf49893..c6902ff 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -504,15 +504,15 @@ func (s *service) createLabel(label LabelDTO, userUuid string) error { return s.repo.createLabel(newLabel) } -func (s *service) getLabels(userUuid string) ([]LabelDTO, error) { +func (s *service) getLabels(userUuid string) ([]LabelsList, error) { stored, err := s.repo.getLabels(userUuid) if err != nil { return nil, err } - response := make([]LabelDTO, 0, len(stored)) + response := make([]LabelsList, 0, len(stored)) for _, label := range stored { - response = append(response, LabelDTO{ + response = append(response, LabelsList{ LabelUuid: label.LabelUuid, Name: label.Name, Color: label.Color, @@ -522,8 +522,8 @@ func (s *service) getLabels(userUuid string) ([]LabelDTO, error) { return response, nil } -func (s *service) updateLabel(userUuid string, label LabelDTO) error { - updateMap := make(map[string]string, 3) +func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error { + updateMap := make(map[string]any, 3) if label.Name != "" { updateMap["name"] = label.Name @@ -537,7 +537,7 @@ func (s *service) updateLabel(userUuid string, label LabelDTO) error { updateMap["bgcolor"] = label.BgColor } - return s.repo.updateLabel(userUuid, label.LabelUuid, updateMap) + return s.repo.updateLabel(userUuid, labelUuid, updateMap) } func (s *service) deleteLabel(userUuid, labelUuid string) error { From 565e019a67004ff2324585adf71df928741dd51d Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:30:05 +0300 Subject: [PATCH 73/87] swagger docs update --- docs/docs.go | 290 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 290 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 182 +++++++++++++++++++++++++++++ 3 files changed, 762 insertions(+) diff --git a/docs/docs.go b/docs/docs.go index 8e556d2..0111bc4 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -279,6 +279,254 @@ const docTemplate = `{ } } }, + "/merch/labels": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить все метки товаров", + "tags": [ + "Merch labels" + ], + "summary": "Получить все метки товаров", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/merch.LabelsList" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Создать новую метку для товара", + "tags": [ + "Merch labels" + ], + "summary": "Создать новую метку для товара", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelDTO" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/labels/attach": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Прикрепить метку к товару", + "tags": [ + "Merch labels" + ], + "summary": "Прикрепить метку к товару", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelLink" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/labels/detach": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Удалить привязку метки к товару", + "tags": [ + "Merch labels" + ], + "summary": "Удалить привязку метки к товару", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelLink" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/labels/{uuid}": { + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Изменить метку", + "tags": [ + "Merch labels" + ], + "summary": "Изменить метку", + "parameters": [ + { + "type": "string", + "description": "label uuid", + "name": "uuid", + "in": "path", + "required": true + }, + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelDTO" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Пометить метку как удаленную", + "tags": [ + "Merch labels" + ], + "summary": "Пометить метку как удаленную", + "parameters": [ + { + "type": "string", + "description": "label uuid", + "name": "uuid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, "/merch/{uuid}": { "get": { "security": [ @@ -781,6 +1029,48 @@ const docTemplate = `{ } } }, + "merch.LabelDTO": { + "type": "object", + "properties": { + "bg_color": { + "type": "string" + }, + "color": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, + "merch.LabelLink": { + "type": "object", + "properties": { + "label_uuid": { + "type": "string" + }, + "merch_uuid": { + "type": "string" + } + } + }, + "merch.LabelsList": { + "type": "object", + "properties": { + "bg_color": { + "type": "string" + }, + "color": { + "type": "string" + }, + "label_uuid": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, "merch.ListResponse": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index ea5dbf2..7b17bd7 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -271,6 +271,254 @@ } } }, + "/merch/labels": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить все метки товаров", + "tags": [ + "Merch labels" + ], + "summary": "Получить все метки товаров", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/merch.LabelsList" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Создать новую метку для товара", + "tags": [ + "Merch labels" + ], + "summary": "Создать новую метку для товара", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelDTO" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/labels/attach": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Прикрепить метку к товару", + "tags": [ + "Merch labels" + ], + "summary": "Прикрепить метку к товару", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelLink" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/labels/detach": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Удалить привязку метки к товару", + "tags": [ + "Merch labels" + ], + "summary": "Удалить привязку метки к товару", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelLink" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, + "/merch/labels/{uuid}": { + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Изменить метку", + "tags": [ + "Merch labels" + ], + "summary": "Изменить метку", + "parameters": [ + { + "type": "string", + "description": "label uuid", + "name": "uuid", + "in": "path", + "required": true + }, + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.LabelDTO" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Пометить метку как удаленную", + "tags": [ + "Merch labels" + ], + "summary": "Пометить метку как удаленную", + "parameters": [ + { + "type": "string", + "description": "label uuid", + "name": "uuid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, "/merch/{uuid}": { "get": { "security": [ @@ -773,6 +1021,48 @@ } } }, + "merch.LabelDTO": { + "type": "object", + "properties": { + "bg_color": { + "type": "string" + }, + "color": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, + "merch.LabelLink": { + "type": "object", + "properties": { + "label_uuid": { + "type": "string" + }, + "merch_uuid": { + "type": "string" + } + } + }, + "merch.LabelsList": { + "type": "object", + "properties": { + "bg_color": { + "type": "string" + }, + "color": { + "type": "string" + }, + "label_uuid": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, "merch.ListResponse": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 1afef07..3dadf18 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -14,6 +14,33 @@ definitions: link: type: string type: object + merch.LabelDTO: + properties: + bg_color: + type: string + color: + type: string + name: + type: string + type: object + merch.LabelLink: + properties: + label_uuid: + type: string + merch_uuid: + type: string + type: object + merch.LabelsList: + properties: + bg_color: + type: string + color: + type: string + label_uuid: + type: string + name: + type: string + type: object merch.ListResponse: properties: merch_uuid: @@ -368,6 +395,161 @@ paths: summary: Загрузить картинку по merch_uuid tags: - Merch images + /merch/labels: + get: + description: Получить все метки товаров + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/merch.LabelsList' + type: array + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Получить все метки товаров + tags: + - Merch labels + post: + description: Создать новую метку для товара + parameters: + - description: payload + in: body + name: payload + required: true + schema: + $ref: '#/definitions/merch.LabelDTO' + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Создать новую метку для товара + tags: + - Merch labels + /merch/labels/{uuid}: + delete: + description: Пометить метку как удаленную + parameters: + - description: label uuid + in: path + name: uuid + required: true + type: string + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Пометить метку как удаленную + tags: + - Merch labels + put: + description: Изменить метку + parameters: + - description: label uuid + in: path + name: uuid + required: true + type: string + - description: payload + in: body + name: payload + required: true + schema: + $ref: '#/definitions/merch.LabelDTO' + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Изменить метку + tags: + - Merch labels + /merch/labels/attach: + post: + description: Прикрепить метку к товару + parameters: + - description: payload + in: body + name: payload + required: true + schema: + $ref: '#/definitions/merch.LabelLink' + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Прикрепить метку к товару + tags: + - Merch labels + /merch/labels/detach: + post: + description: Удалить привязку метки к товару + parameters: + - description: payload + in: body + name: payload + required: true + schema: + $ref: '#/definitions/merch.LabelLink' + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Удалить привязку метки к товару + tags: + - Merch labels /prices: get: description: Получить цены мерча за период From 8ac753f63293092318f4967cfef813eb9f94f93d Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 21:46:40 +0300 Subject: [PATCH 74/87] labels added to dto --- internal/api/merch/dto.go | 5 +++-- internal/api/merch/repository.go | 11 +++++++++++ internal/api/merch/service.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index 32e72e4..ad63c26 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -27,8 +27,9 @@ type SingleMerchResponse struct { } type ListResponse struct { - MerchUuid string `json:"merch_uuid"` - Name string `json:"name"` + MerchUuid string `json:"merch_uuid"` + Name string `json:"name"` + Labels []string `json:"labels,omitempty" gorm:"-"` } type PriceEntry struct { diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 5b3c308..d1567de 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -47,6 +47,7 @@ type labels interface { deleteLabel(userUuid, labelUuid string) error attachLabel(label CardLabel) error detachLabel(label CardLabel) error + getAttachedLabelsByList(list []string) ([]CardLabel, error) } func (r *Repo) addMerch(bundle merchBundle) error { @@ -289,3 +290,13 @@ func (r *Repo) detachLabel(label CardLabel) error { Where("user_uuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). Delete(&CardLabel{}).Error } + +func (r *Repo) getAttachedLabelsByList(list []string) ([]CardLabel, error) { + var labelsList []CardLabel + + if err := r.db.Model(&CardLabel{}).Where("merch_uuid IN ?", list).Find(&labelsList).Error; err != nil { + return nil, err + } + + return labelsList, nil +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index c6902ff..1632329 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -101,7 +101,34 @@ func (s *service) getSingleMerch(userUuid, merchUuid string) (MerchDTO, error) { } func (s *service) getAllMerch(userUuid string) ([]ListResponse, error) { - return s.repo.getAllMerch(userUuid) + const logMsg = "Merch service | Get all merch" + + allMerch, err := s.repo.getAllMerch(userUuid) + if err != nil { + return nil, err + } + + ids := make([]string, 0, len(allMerch)) + for _, m := range allMerch { + ids = append(ids, m.MerchUuid) + } + + cardLabels, err := s.repo.getAttachedLabelsByList(ids) + if err != nil { + return nil, err + } + log.WithField("content", cardLabels).Debug(logMsg) + + clMap := make(map[string][]string) + for _, cl := range cardLabels { + clMap[cl.MerchUuid] = append(clMap[cl.MerchUuid], cl.LabelUuid) + } + + for item := range allMerch { + allMerch[item].Labels = clMap[allMerch[item].MerchUuid] + } + + return allMerch, nil } func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error { From b6f787571026bdb758c807eb507b207dfc161cbc Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 29 Oct 2025 20:55:51 +0300 Subject: [PATCH 75/87] update --- migrations.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/migrations.sql b/migrations.sql index b5fd81c..1ffe7e5 100644 --- a/migrations.sql +++ b/migrations.sql @@ -70,7 +70,12 @@ CREATE TABLE labels( ); CREATE TABLE card_labels ( + id BIGSERIAL PRIMARY KEY, user_uuid VARCHAR(36) NOT NULL, label_uuid VARCHAR(36) NOT NULL, merch_uuid VARCHAR(36) NOT NULL ); + +ALTER TABLE card_labels + ADD CONSTRAINT card_labels_unique_user_label_merch + UNIQUE (user_uuid, label_uuid, merch_uuid); \ No newline at end of file From 8186d8a46cbfeddef48bb142fcccc6292a2bbc14 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 29 Oct 2025 20:56:26 +0300 Subject: [PATCH 76/87] getMerchLabels + fixes --- internal/api/merch/controller.go | 38 +++++++++++++++++++++++++++++++- internal/api/merch/dto.go | 1 + internal/api/merch/repository.go | 11 +++++++++ internal/api/merch/service.go | 31 +++++++++++++++++++------- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 7f0ae7a..41a6983 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -43,13 +43,14 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref imagesGroup.GET("/:uuid", h.controller.getMerchImage) imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage) - labelsGroup := merchGroup.Group("/labels") + labelsGroup := merchGroup.Group("/labels", authMW) labelsGroup.POST("", h.controller.createLabel) labelsGroup.GET("", h.controller.getLabels) labelsGroup.PUT("/:uuid", h.controller.updateLabel) labelsGroup.DELETE("/:uuid", h.controller.deleteLabel) labelsGroup.POST("/attach", h.controller.attachLabel) labelsGroup.POST("/detach", h.controller.detachLabel) + labelsGroup.GET("/:uuid", h.controller.getMerchLabels) } // @Summary Добавить новый мерч @@ -630,3 +631,38 @@ func (co *controller) detachLabel(c *gin.Context) { } c.Status(http.StatusOK) } + +// @Summary Получить метки товара по его uuid +// @Description Получить метки товара по его uuid +// @Tags Merch labels +// @Security BearerAuth +// @Param uuid path string true "label uuid" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/{uuid} [get] +func (co *controller) getMerchLabels(c *gin.Context) { + const logMsg = "Merch | Get merch labels" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + merchUuid := c.Param("uuid") + if merchUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "label uuid is empty"}) + log.WithError(err).Error(logMsg) + return + } + + response, err := co.service.getMerchLabels(userUuid, merchUuid) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.JSON(http.StatusOK, response) +} diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index ad63c26..2f267b1 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -10,6 +10,7 @@ type MerchDTO struct { Name string `json:"name"` OriginSurugaya SurugayaDTO `json:"origin_surugaya"` OriginMandarake MandarakeDTO `json:"origin_mandarake"` + Labels []string `json:"labels,omitempty" gorm:"-"` } type SurugayaDTO struct { diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index d1567de..b5ecf39 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -48,6 +48,7 @@ type labels interface { attachLabel(label CardLabel) error detachLabel(label CardLabel) error getAttachedLabelsByList(list []string) ([]CardLabel, error) + getAttachedLabelsByUuid(userUuid, merchUuid string) ([]CardLabel, error) } func (r *Repo) addMerch(bundle merchBundle) error { @@ -300,3 +301,13 @@ func (r *Repo) getAttachedLabelsByList(list []string) ([]CardLabel, error) { return labelsList, nil } + +func (r *Repo) getAttachedLabelsByUuid(userUuid, merchUuid string) ([]CardLabel, error) { + var labelsList []CardLabel + + if err := r.db.Model(&CardLabel{}).Where("user_uuid = ? AND merch_uuid = ?", userUuid, merchUuid).Find(&labelsList).Error; err != nil { + return nil, err + } + + return labelsList, nil +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 1632329..4689453 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -411,13 +411,14 @@ func (s *service) deleteMerchImage(ctx context.Context, userUuid, merchUuid stri return fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) } - if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid)); err != nil { - return err - } - - if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid)); err != nil { - return err - } + //uncomment for MinIO + //if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid)); err != nil { + // return err + //} + // + //if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid)); err != nil { + // return err + //} return nil } @@ -561,7 +562,7 @@ func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error } if label.BgColor != "" { - updateMap["bgcolor"] = label.BgColor + updateMap["bg_color"] = label.BgColor } return s.repo.updateLabel(userUuid, labelUuid, updateMap) @@ -597,3 +598,17 @@ func (s *service) detachLabel(userUuid string, label LabelLink) error { } return s.repo.detachLabel(detach) } + +func (s *service) getMerchLabels(userUuid, merchUuid string) ([]string, error) { + getLabels, err := s.repo.getAttachedLabelsByUuid(userUuid, merchUuid) + if err != nil { + return nil, err + } + + response := make([]string, 0, len(getLabels)) + for _, label := range getLabels { + response = append(response, label.LabelUuid) + } + + return response, nil +} From 88fcbfe1a5067f121ff341dceb9ca1d6a04e2dff Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 2 Nov 2025 20:59:25 +0300 Subject: [PATCH 77/87] routes + repo fix --- internal/api/user/controller.go | 8 ++++---- internal/api/user/repository.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/api/user/controller.go b/internal/api/user/controller.go index 212865a..48128f6 100644 --- a/internal/api/user/controller.go +++ b/internal/api/user/controller.go @@ -26,10 +26,10 @@ func newController(service *service, utils interfaces.Utils) *controller { func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, refreshMW gin.HandlerFunc) { userGroup := r.Group("/user") - userGroup.POST("/", h.controller.register) - userGroup.GET("/", authMW, h.controller.get) - userGroup.PUT("/", authMW, h.controller.update) - userGroup.DELETE("/", authMW, h.controller.delete) + userGroup.POST("", h.controller.register) + userGroup.GET("", authMW, h.controller.get) + userGroup.PUT("", authMW, h.controller.update) + userGroup.DELETE("", authMW, h.controller.delete) //auth h.controller.authPath = fmt.Sprintf("%s/user/auth", h.apiPrefix) diff --git a/internal/api/user/repository.go b/internal/api/user/repository.go index 87d2b2d..4b0b754 100644 --- a/internal/api/user/repository.go +++ b/internal/api/user/repository.go @@ -40,7 +40,7 @@ func (r *repo) getByUuid(userUuid string) (user User, err error) { } func (r *repo) update(user map[string]any) error { - return r.db.Where("uuid = ?", user["uuid"]).Updates(&user).Error + return r.db.Model(&User{}).Where("uuid = ?", user["uuid"]).Updates(&user).Error } func (r *repo) delete(userUuid string) error { From 93ce93770d3c37d59e238028eef64560cd69a723 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 2 Nov 2025 21:10:49 +0300 Subject: [PATCH 78/87] zero prices check added --- internal/api/merch/controller.go | 81 +++++++++++++++++++++++++++++++- internal/api/merch/dto.go | 13 +++++ internal/api/merch/model.go | 4 +- internal/api/merch/repository.go | 43 +++++++++++++++++ internal/api/merch/service.go | 8 ++++ 5 files changed, 146 insertions(+), 3 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 41a6983..63b28f2 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -27,7 +27,6 @@ func newController(service *service, utils interfaces.Utils, expires time.Durati 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) @@ -51,6 +50,10 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref labelsGroup.POST("/attach", h.controller.attachLabel) labelsGroup.POST("/detach", h.controller.detachLabel) labelsGroup.GET("/:uuid", h.controller.getMerchLabels) + + zeroPricesGroup := merchGroup.Group("/zeroprices", authMW) + zeroPricesGroup.GET("", h.controller.getZeroPrices) + zeroPricesGroup.DELETE("", h.controller.deleteZeroPrices) } // @Summary Добавить новый мерч @@ -92,6 +95,7 @@ func (co *controller) addMerch(c *gin.Context) { // @Description Получить всю информацию про мерч по его uuid // @Tags Merch // @Security BearerAuth +// @Produce json // @Param uuid path string true "merch_uuid" // @Success 200 {object} MerchDTO // @Failure 400 {object} responses.ErrorResponse400 @@ -125,6 +129,7 @@ func (co *controller) getSingleMerch(c *gin.Context) { // @Description Получить все записи мерча // @Tags Merch // @Security BearerAuth +// @Produce json // @Success 200 {array} ListResponse // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 @@ -151,6 +156,7 @@ func (co *controller) getAllMerch(c *gin.Context) { // @Description Обновить информацию про мерч по его uuid в json-е // @Tags Merch // @Security BearerAuth +// Accept json // @Param body body UpdateMerchDTO true "merch_uuid" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -214,6 +220,7 @@ func (co *controller) deleteMerch(c *gin.Context) { // @Description Получить цены мерча за период // @Tags Merch // @Security BearerAuth +// @Produce json // @Param days query string false "period in days" // @Success 200 {array} PricesResponse // @Failure 400 {object} responses.ErrorResponse400 @@ -243,6 +250,7 @@ func (co *controller) getChartsPrices(c *gin.Context) { // @Description Получить перепады цен мерча за период по его merch_uuid // @Tags Merch // @Security BearerAuth +// @Produce json // @Param uuid path string true "merch_uuid" // @Param days query string false "period in days" // @Success 200 {object} PricesResponse @@ -338,6 +346,7 @@ func (co *controller) uploadMerchImage(c *gin.Context) { // @Description Получить картинки по merch_uuid и query параметрам // @Tags Merch images // @Security BearerAuth +// @Produce json // @Param uuid path string true "merch_uuid" // @Param type query string true "image type" // @Success 200 {object} ImageLink @@ -429,6 +438,7 @@ func (co *controller) deleteMerchImage(c *gin.Context) { // @Description Создать новую метку для товара // @Tags Merch labels // @Security BearerAuth +// Accept json // @Param payload body LabelDTO true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -464,6 +474,7 @@ func (co *controller) createLabel(c *gin.Context) { // @Description Получить все метки товаров // @Tags Merch labels // @Security BearerAuth +// @Produce json // @Success 200 {array} LabelsList // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 @@ -492,6 +503,7 @@ func (co *controller) getLabels(c *gin.Context) { // @Description Изменить метку // @Tags Merch labels // @Security BearerAuth +// Accept json // @Param uuid path string true "label uuid" // @Param payload body LabelDTO true "payload" // @Success 200 @@ -568,6 +580,7 @@ func (co *controller) deleteLabel(c *gin.Context) { // @Description Прикрепить метку к товару // @Tags Merch labels // @Security BearerAuth +// Accept json // @Param payload body LabelLink true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -602,6 +615,7 @@ func (co *controller) attachLabel(c *gin.Context) { // @Description Удалить привязку метки к товару // @Tags Merch labels // @Security BearerAuth +// Accept json // @Param payload body LabelLink true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -636,6 +650,7 @@ func (co *controller) detachLabel(c *gin.Context) { // @Description Получить метки товара по его uuid // @Tags Merch labels // @Security BearerAuth +// @Produce json // @Param uuid path string true "label uuid" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -666,3 +681,67 @@ func (co *controller) getMerchLabels(c *gin.Context) { } c.JSON(http.StatusOK, response) } + +// @Summary Получить нулевые цены +// @Description Получить нулевые цены +// @Tags Merch zero prices +// @Security BearerAuth +// @Produce json +// @Success 200 {array} ZeroPrice +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/zeroprices [get] +func (co *controller) getZeroPrices(c *gin.Context) { + const logMsg = "Merch | Get zero prices" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + response, err := co.service.getZeroPrices(userUuid) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + c.JSON(http.StatusOK, response) +} + +// @Summary Пометить нулевые цены как удаленные +// @Description Пометить нулевые цены как удаленные +// @Tags Merch zero prices +// @Security BearerAuth +// Accept json +// @Param payload body DeleteZeroPrices true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/zeroprices [delete] +func (co *controller) deleteZeroPrices(c *gin.Context) { + const logMsg = "Merch | Delete zero prices" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + var payload DeleteZeroPrices + if err = c.ShouldBindJSON(&payload); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.deleteZeroPrices(userUuid, payload); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.Status(http.StatusOK) +} diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index 2f267b1..b10aa6b 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -1,5 +1,7 @@ package merch +import "time" + type merchBundle struct { Merch *Merch Surugaya *Surugaya @@ -78,3 +80,14 @@ type LabelLink struct { MerchUuid string `json:"merch_uuid"` LabelUuid string `json:"label_uuid"` } + +type ZeroPrice struct { + CreatedAt time.Time `json:"created_at"` + MerchUuid string `json:"merch_uuid"` + Name string `json:"name"` + Origin string `json:"origin"` +} + +type DeleteZeroPrices struct { + MerchUuids []string `json:"merch_uuids"` +} diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 935a194..2dd6e0e 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -44,8 +44,8 @@ func (Mandarake) TableName() string { type Price struct { Id uint `json:"id" gorm:"primary_key"` CreatedAt time.Time `json:"created_at" gorm:"column:created_at"` - UpdatedAt sql.NullTime `json:"updated_at" gorm:"column:updated_at"` - DeletedAt sql.NullTime `json:"deleted_at" gorm:"column:deleted_at"` + UpdatedAt sql.NullTime `json:"updated_at,omitempty" gorm:"column:updated_at"` + DeletedAt sql.NullTime `json:"deleted_at,omitempty" gorm:"column:deleted_at"` MerchUuid string `json:"merch_uuid" gorm:"column:merch_uuid"` Price int `json:"price" gorm:"column:price"` Origin Origin `json:"origin" gorm:"column:origin;type:integer"` diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index b5ecf39..1af8df0 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -38,6 +38,9 @@ type repository interface { type prices interface { getPricesWithDays(userUuid string, period time.Time) ([]Price, error) getDistinctPrices(userUuid, merchUuid string, period time.Time) (prices []Price, err error) + + getZeroPrices(userUuid string) ([]ZeroPrice, error) + deleteZeroPrices(userUuid string, list []string) error } type labels interface { @@ -311,3 +314,43 @@ func (r *Repo) getAttachedLabelsByUuid(userUuid, merchUuid string) ([]CardLabel, return labelsList, nil } + +func (r *Repo) getZeroPrices(userUuid string) ([]ZeroPrice, error) { + var priceList []ZeroPrice + if err := r.db.Raw(` + WITH price_with_neighbors AS ( + SELECT + 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, + LEAD(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS next_price + FROM prices AS p + JOIN merch as m ON m.merch_uuid = p.merch_uuid + WHERE p.deleted_at IS NULL + AND m.user_uuid = ?) + + SELECT + created_at, merch_uuid, origin, name + FROM price_with_neighbors + WHERE + price = 0 + AND prev_price IS NOT NULL + AND prev_price > 0 + AND next_price IS NOT NULL + AND next_price > 0; + `, userUuid).Scan(&priceList).Error; err != nil { + return nil, err + } + return priceList, nil +} + +func (r *Repo) deleteZeroPrices(userUuid string, list []string) error { + subQuery := r.db.Table("merch"). + Select("merch_uuid"). + Where("user_uuid = ?", userUuid) + + return r.db.Model(&Price{}). + Where("merch_uuid IN ?", list). + Where("merch_uuid IN (?)", subQuery). + Update("deleted_at", time.Now().UTC()). + Error +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 4689453..259563d 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -612,3 +612,11 @@ func (s *service) getMerchLabels(userUuid, merchUuid string) ([]string, error) { return response, nil } + +func (s *service) getZeroPrices(userUuid string) ([]ZeroPrice, error) { + return s.repo.getZeroPrices(userUuid) +} + +func (s *service) deleteZeroPrices(userUuid string, list DeleteZeroPrices) error { + return s.repo.deleteZeroPrices(userUuid, list.MerchUuids) +} From a0e21db5a09e7e13d026611c1f068ca27fbe9ed1 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 2 Nov 2025 21:10:59 +0300 Subject: [PATCH 79/87] swagger docs update --- docs/docs.go | 179 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 179 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 114 +++++++++++++++++++++++++++++ 3 files changed, 472 insertions(+) diff --git a/docs/docs.go b/docs/docs.go index 0111bc4..b668308 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -68,6 +68,9 @@ const docTemplate = `{ } ], "description": "Получить все записи мерча", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -145,6 +148,9 @@ const docTemplate = `{ } ], "description": "Получить картинки по merch_uuid и query параметрам", + "produces": [ + "application/json" + ], "tags": [ "Merch images" ], @@ -287,6 +293,9 @@ const docTemplate = `{ } ], "description": "Получить все метки товаров", + "produces": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -441,6 +450,47 @@ const docTemplate = `{ } }, "/merch/labels/{uuid}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить метки товара по его uuid", + "produces": [ + "application/json" + ], + "tags": [ + "Merch labels" + ], + "summary": "Получить метки товара по его uuid", + "parameters": [ + { + "type": "string", + "description": "label uuid", + "name": "uuid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, "put": { "security": [ { @@ -527,6 +577,86 @@ const docTemplate = `{ } } }, + "/merch/zeroprices": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить нулевые цены", + "produces": [ + "application/json" + ], + "tags": [ + "Merch zero prices" + ], + "summary": "Получить нулевые цены", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/merch.ZeroPrice" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Пометить нулевые цены как удаленные", + "tags": [ + "Merch zero prices" + ], + "summary": "Пометить нулевые цены как удаленные", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.DeleteZeroPrices" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, "/merch/{uuid}": { "get": { "security": [ @@ -535,6 +665,9 @@ const docTemplate = `{ } ], "description": "Получить всю информацию про мерч по его uuid", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -619,6 +752,9 @@ const docTemplate = `{ } ], "description": "Получить цены мерча за период", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -664,6 +800,9 @@ const docTemplate = `{ } ], "description": "Получить перепады цен мерча за период по его merch_uuid", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -1018,6 +1157,17 @@ const docTemplate = `{ } } }, + "merch.DeleteZeroPrices": { + "type": "object", + "properties": { + "merch_uuids": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "merch.ImageLink": { "type": "object", "properties": { @@ -1074,6 +1224,12 @@ const docTemplate = `{ "merch.ListResponse": { "type": "object", "properties": { + "labels": { + "type": "array", + "items": { + "type": "string" + } + }, "merch_uuid": { "type": "string" }, @@ -1093,6 +1249,12 @@ const docTemplate = `{ "merch.MerchDTO": { "type": "object", "properties": { + "labels": { + "type": "array", + "items": { + "type": "string" + } + }, "merch_uuid": { "type": "string" }, @@ -1174,6 +1336,23 @@ const docTemplate = `{ } } }, + "merch.ZeroPrice": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "merch_uuid": { + "type": "string" + }, + "name": { + "type": "string" + }, + "origin": { + "type": "string" + } + } + }, "responses.ErrorResponse400": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 7b17bd7..0df1704 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -60,6 +60,9 @@ } ], "description": "Получить все записи мерча", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -137,6 +140,9 @@ } ], "description": "Получить картинки по merch_uuid и query параметрам", + "produces": [ + "application/json" + ], "tags": [ "Merch images" ], @@ -279,6 +285,9 @@ } ], "description": "Получить все метки товаров", + "produces": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -433,6 +442,47 @@ } }, "/merch/labels/{uuid}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить метки товара по его uuid", + "produces": [ + "application/json" + ], + "tags": [ + "Merch labels" + ], + "summary": "Получить метки товара по его uuid", + "parameters": [ + { + "type": "string", + "description": "label uuid", + "name": "uuid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, "put": { "security": [ { @@ -519,6 +569,86 @@ } } }, + "/merch/zeroprices": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Получить нулевые цены", + "produces": [ + "application/json" + ], + "tags": [ + "Merch zero prices" + ], + "summary": "Получить нулевые цены", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/merch.ZeroPrice" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Пометить нулевые цены как удаленные", + "tags": [ + "Merch zero prices" + ], + "summary": "Пометить нулевые цены как удаленные", + "parameters": [ + { + "description": "payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/merch.DeleteZeroPrices" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, "/merch/{uuid}": { "get": { "security": [ @@ -527,6 +657,9 @@ } ], "description": "Получить всю информацию про мерч по его uuid", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -611,6 +744,9 @@ } ], "description": "Получить цены мерча за период", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -656,6 +792,9 @@ } ], "description": "Получить перепады цен мерча за период по его merch_uuid", + "produces": [ + "application/json" + ], "tags": [ "Merch" ], @@ -1010,6 +1149,17 @@ } } }, + "merch.DeleteZeroPrices": { + "type": "object", + "properties": { + "merch_uuids": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "merch.ImageLink": { "type": "object", "properties": { @@ -1066,6 +1216,12 @@ "merch.ListResponse": { "type": "object", "properties": { + "labels": { + "type": "array", + "items": { + "type": "string" + } + }, "merch_uuid": { "type": "string" }, @@ -1085,6 +1241,12 @@ "merch.MerchDTO": { "type": "object", "properties": { + "labels": { + "type": "array", + "items": { + "type": "string" + } + }, "merch_uuid": { "type": "string" }, @@ -1166,6 +1328,23 @@ } } }, + "merch.ZeroPrice": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "merch_uuid": { + "type": "string" + }, + "name": { + "type": "string" + }, + "origin": { + "type": "string" + } + } + }, "responses.ErrorResponse400": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 3dadf18..fd4e41b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7,6 +7,13 @@ definitions: thumbnail: type: string type: object + merch.DeleteZeroPrices: + properties: + merch_uuids: + items: + type: string + type: array + type: object merch.ImageLink: properties: etag: @@ -43,6 +50,10 @@ definitions: type: object merch.ListResponse: properties: + labels: + items: + type: string + type: array merch_uuid: type: string name: @@ -55,6 +66,10 @@ definitions: type: object merch.MerchDTO: properties: + labels: + items: + type: string + type: array merch_uuid: type: string name: @@ -107,6 +122,17 @@ definitions: origin: type: string type: object + merch.ZeroPrice: + properties: + created_at: + type: string + merch_uuid: + type: string + name: + type: string + origin: + type: string + type: object responses.ErrorResponse400: properties: error: @@ -205,6 +231,8 @@ paths: /merch/: get: description: Получить все записи мерча + produces: + - application/json responses: "200": description: OK @@ -285,6 +313,8 @@ paths: name: uuid required: true type: string + produces: + - application/json responses: "200": description: OK @@ -341,6 +371,8 @@ paths: name: type required: true type: string + produces: + - application/json responses: "200": description: OK @@ -398,6 +430,8 @@ paths: /merch/labels: get: description: Получить все метки товаров + produces: + - application/json responses: "200": description: OK @@ -468,6 +502,32 @@ paths: summary: Пометить метку как удаленную tags: - Merch labels + get: + description: Получить метки товара по его uuid + parameters: + - description: label uuid + in: path + name: uuid + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Получить метки товара по его uuid + tags: + - Merch labels put: description: Изменить метку parameters: @@ -550,6 +610,56 @@ paths: summary: Удалить привязку метки к товару tags: - Merch labels + /merch/zeroprices: + delete: + description: Пометить нулевые цены как удаленные + parameters: + - description: payload + in: body + name: payload + required: true + schema: + $ref: '#/definitions/merch.DeleteZeroPrices' + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Пометить нулевые цены как удаленные + tags: + - Merch zero prices + get: + description: Получить нулевые цены + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/merch.ZeroPrice' + type: array + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Получить нулевые цены + tags: + - Merch zero prices /prices: get: description: Получить цены мерча за период @@ -558,6 +668,8 @@ paths: in: query name: days type: string + produces: + - application/json responses: "200": description: OK @@ -591,6 +703,8 @@ paths: in: query name: days type: string + produces: + - application/json responses: "200": description: OK From 2728051fdeed24f957dae3e96a1bc0192be022b6 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 2 Nov 2025 23:27:23 +0300 Subject: [PATCH 80/87] fixes --- internal/api/merch/controller.go | 2 +- internal/api/merch/dto.go | 4 ++- internal/api/merch/repository.go | 44 ++++++++++++++++++++++---------- internal/api/merch/service.go | 25 ++++++++++++++++-- 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 63b28f2..33c194f 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -731,7 +731,7 @@ func (co *controller) deleteZeroPrices(c *gin.Context) { return } - var payload DeleteZeroPrices + var payload []DeleteZeroPrices if err = c.ShouldBindJSON(&payload); err != nil { c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) log.WithError(err).Error(logMsg) diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index b10aa6b..7ee7281 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -82,6 +82,7 @@ type LabelLink struct { } type ZeroPrice struct { + Id int `json:"id"` CreatedAt time.Time `json:"created_at"` MerchUuid string `json:"merch_uuid"` Name string `json:"name"` @@ -89,5 +90,6 @@ type ZeroPrice struct { } type DeleteZeroPrices struct { - MerchUuids []string `json:"merch_uuids"` + Id uint `json:"id"` + MerchUuid string `json:"merch_uuid"` } diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 1af8df0..711b731 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -3,6 +3,7 @@ package merch import ( "database/sql" "errors" + "fmt" "gorm.io/gorm" "gorm.io/gorm/clause" "time" @@ -22,6 +23,7 @@ type repository interface { addMerch(bundle merchBundle) error merchRecordExists(userUuid, merchUuid string) (bool, error) + userOwnsMerchUuids(userUuid string, merchUuids []string) (bool, error) getSingleMerch(userUuid, merchUuid string) (merchBundle, error) getAllMerch(userUuid string) ([]ListResponse, error) @@ -40,7 +42,7 @@ type prices interface { getDistinctPrices(userUuid, merchUuid string, period time.Time) (prices []Price, err error) getZeroPrices(userUuid string) ([]ZeroPrice, error) - deleteZeroPrices(userUuid string, list []string) error + deleteZeroPrices(list []DeleteZeroPrices) error } type labels interface { @@ -84,6 +86,22 @@ 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 + + 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) { var merch Merch if err := r.db. @@ -320,16 +338,17 @@ func (r *Repo) getZeroPrices(userUuid string) ([]ZeroPrice, error) { if err := r.db.Raw(` WITH price_with_neighbors AS ( 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, LEAD(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS next_price FROM prices AS p JOIN merch as m ON m.merch_uuid = p.merch_uuid WHERE p.deleted_at IS NULL + AND m.deleted_at IS NULL AND m.user_uuid = ?) SELECT - created_at, merch_uuid, origin, name + id, created_at, merch_uuid, origin, name FROM price_with_neighbors WHERE price = 0 @@ -343,14 +362,13 @@ func (r *Repo) getZeroPrices(userUuid string) ([]ZeroPrice, error) { return priceList, nil } -func (r *Repo) deleteZeroPrices(userUuid string, list []string) error { - subQuery := r.db.Table("merch"). - Select("merch_uuid"). - Where("user_uuid = ?", userUuid) - - return r.db.Model(&Price{}). - Where("merch_uuid IN ?", list). - Where("merch_uuid IN (?)", subQuery). - Update("deleted_at", time.Now().UTC()). - Error +func (r *Repo) deleteZeroPrices(list []DeleteZeroPrices) error { + for _, item := range list { + if err := r.db.Model(&Price{}). + Where("id = ? AND merch_uuid = ?", item.Id, item.MerchUuid). + Update("deleted_at", time.Now().UTC()).Error; err != nil { + return err + } + } + return nil } diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 259563d..4d06a37 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -16,6 +16,7 @@ import ( is "merch-parser-api/proto/imageStorage" "mime/multipart" "path/filepath" + "slices" "strings" "time" ) @@ -617,6 +618,26 @@ func (s *service) getZeroPrices(userUuid string) ([]ZeroPrice, error) { return s.repo.getZeroPrices(userUuid) } -func (s *service) deleteZeroPrices(userUuid string, list DeleteZeroPrices) error { - return s.repo.deleteZeroPrices(userUuid, list.MerchUuids) +func (s *service) deleteZeroPrices(userUuid string, list []DeleteZeroPrices) error { + 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) } From 7fa79d770a20293799485e76c55fb66f4ca73f6f Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 2 Nov 2025 23:39:25 +0300 Subject: [PATCH 81/87] swagger docs update --- docs/docs.go | 31 ++++++++++-- docs/swagger.json | 31 ++++++++++-- docs/swagger.yaml | 22 +++++++-- internal/api/merch/controller.go | 84 +++++++++++++++++--------------- 4 files changed, 114 insertions(+), 54 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index b668308..b97688b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -106,6 +106,9 @@ const docTemplate = `{ } ], "description": "Обновить информацию про мерч по его uuid в json-е", + "consumes": [ + "application/json" + ], "tags": [ "Merch" ], @@ -331,6 +334,9 @@ const docTemplate = `{ } ], "description": "Создать новую метку для товара", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -373,6 +379,9 @@ const docTemplate = `{ } ], "description": "Прикрепить метку к товару", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -415,6 +424,9 @@ const docTemplate = `{ } ], "description": "Удалить привязку метки к товару", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -498,6 +510,9 @@ const docTemplate = `{ } ], "description": "Изменить метку", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -623,6 +638,9 @@ const docTemplate = `{ } ], "description": "Пометить нулевые цены как удаленные", + "consumes": [ + "application/json" + ], "tags": [ "Merch zero prices" ], @@ -1160,11 +1178,11 @@ const docTemplate = `{ "merch.DeleteZeroPrices": { "type": "object", "properties": { - "merch_uuids": { - "type": "array", - "items": { - "type": "string" - } + "id": { + "type": "integer" + }, + "merch_uuid": { + "type": "string" } } }, @@ -1342,6 +1360,9 @@ const docTemplate = `{ "created_at": { "type": "string" }, + "id": { + "type": "integer" + }, "merch_uuid": { "type": "string" }, diff --git a/docs/swagger.json b/docs/swagger.json index 0df1704..f746266 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -98,6 +98,9 @@ } ], "description": "Обновить информацию про мерч по его uuid в json-е", + "consumes": [ + "application/json" + ], "tags": [ "Merch" ], @@ -323,6 +326,9 @@ } ], "description": "Создать новую метку для товара", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -365,6 +371,9 @@ } ], "description": "Прикрепить метку к товару", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -407,6 +416,9 @@ } ], "description": "Удалить привязку метки к товару", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -490,6 +502,9 @@ } ], "description": "Изменить метку", + "consumes": [ + "application/json" + ], "tags": [ "Merch labels" ], @@ -615,6 +630,9 @@ } ], "description": "Пометить нулевые цены как удаленные", + "consumes": [ + "application/json" + ], "tags": [ "Merch zero prices" ], @@ -1152,11 +1170,11 @@ "merch.DeleteZeroPrices": { "type": "object", "properties": { - "merch_uuids": { - "type": "array", - "items": { - "type": "string" - } + "id": { + "type": "integer" + }, + "merch_uuid": { + "type": "string" } } }, @@ -1334,6 +1352,9 @@ "created_at": { "type": "string" }, + "id": { + "type": "integer" + }, "merch_uuid": { "type": "string" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index fd4e41b..883b177 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -9,10 +9,10 @@ definitions: type: object merch.DeleteZeroPrices: properties: - merch_uuids: - items: - type: string - type: array + id: + type: integer + merch_uuid: + type: string type: object merch.ImageLink: properties: @@ -126,6 +126,8 @@ definitions: properties: created_at: type: string + id: + type: integer merch_uuid: type: string name: @@ -254,6 +256,8 @@ paths: tags: - Merch put: + consumes: + - application/json description: Обновить информацию про мерч по его uuid в json-е parameters: - description: merch_uuid @@ -453,6 +457,8 @@ paths: tags: - Merch labels post: + consumes: + - application/json description: Создать новую метку для товара parameters: - description: payload @@ -529,6 +535,8 @@ paths: tags: - Merch labels put: + consumes: + - application/json description: Изменить метку parameters: - description: label uuid @@ -560,6 +568,8 @@ paths: - Merch labels /merch/labels/attach: post: + consumes: + - application/json description: Прикрепить метку к товару parameters: - description: payload @@ -586,6 +596,8 @@ paths: - Merch labels /merch/labels/detach: post: + consumes: + - application/json description: Удалить привязку метки к товару parameters: - description: payload @@ -612,6 +624,8 @@ paths: - Merch labels /merch/zeroprices: delete: + consumes: + - application/json description: Пометить нулевые цены как удаленные parameters: - description: payload diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 33c194f..7ea9636 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -95,7 +95,7 @@ func (co *controller) addMerch(c *gin.Context) { // @Description Получить всю информацию про мерч по его uuid // @Tags Merch // @Security BearerAuth -// @Produce json +// @Produce json // @Param uuid path string true "merch_uuid" // @Success 200 {object} MerchDTO // @Failure 400 {object} responses.ErrorResponse400 @@ -129,7 +129,7 @@ func (co *controller) getSingleMerch(c *gin.Context) { // @Description Получить все записи мерча // @Tags Merch // @Security BearerAuth -// @Produce json +// @Produce json // @Success 200 {array} ListResponse // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 @@ -156,7 +156,7 @@ func (co *controller) getAllMerch(c *gin.Context) { // @Description Обновить информацию про мерч по его uuid в json-е // @Tags Merch // @Security BearerAuth -// Accept json +// @Accept json // @Param body body UpdateMerchDTO true "merch_uuid" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -182,6 +182,7 @@ func (co *controller) updateMerch(c *gin.Context) { log.WithError(err).Error("Merch | Failed to get single merch") return } + c.Status(http.StatusOK) } // @Summary Пометить мерч как удаленный @@ -192,7 +193,8 @@ func (co *controller) updateMerch(c *gin.Context) { // @Success 200 {object} MerchDTO // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 -// @Router /merch/{uuid} [delete] +// +// @Router /merch/{uuid} [delete] func (co *controller) deleteMerch(c *gin.Context) { merchUuid := c.Param("uuid") if merchUuid == "" { @@ -220,12 +222,16 @@ func (co *controller) deleteMerch(c *gin.Context) { // @Description Получить цены мерча за период // @Tags Merch // @Security BearerAuth -// @Produce json +// @Produce json // @Param days query string false "period in days" // @Success 200 {array} PricesResponse // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 // @Router /prices [get] +// +// @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", "")) @@ -250,7 +256,7 @@ func (co *controller) getChartsPrices(c *gin.Context) { // @Description Получить перепады цен мерча за период по его merch_uuid // @Tags Merch // @Security BearerAuth -// @Produce json +// @Produce json // @Param uuid path string true "merch_uuid" // @Param days query string false "period in days" // @Success 200 {object} PricesResponse @@ -338,7 +344,6 @@ func (co *controller) uploadMerchImage(c *gin.Context) { return } - //c.Status(http.StatusOK) c.JSON(http.StatusOK, response) } @@ -346,7 +351,7 @@ func (co *controller) uploadMerchImage(c *gin.Context) { // @Description Получить картинки по merch_uuid и query параметрам // @Tags Merch images // @Security BearerAuth -// @Produce json +// @Produce json // @Param uuid path string true "merch_uuid" // @Param type query string true "image type" // @Success 200 {object} ImageLink @@ -393,7 +398,6 @@ func (co *controller) getMerchImage(c *gin.Context) { //} // //c.JSON(http.StatusOK, link) - c.JSON(http.StatusNotImplemented, gin.H{"msg": "Method deprecated. Request images from image storage."}) } // @Summary Удалить (безвозвратно) картинки по merch_uuid @@ -438,7 +442,7 @@ func (co *controller) deleteMerchImage(c *gin.Context) { // @Description Создать новую метку для товара // @Tags Merch labels // @Security BearerAuth -// Accept json +// @Accept json // @Param payload body LabelDTO true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -474,7 +478,7 @@ func (co *controller) createLabel(c *gin.Context) { // @Description Получить все метки товаров // @Tags Merch labels // @Security BearerAuth -// @Produce json +// @Produce json // @Success 200 {array} LabelsList // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 @@ -503,7 +507,7 @@ func (co *controller) getLabels(c *gin.Context) { // @Description Изменить метку // @Tags Merch labels // @Security BearerAuth -// Accept json +// @Accept json // @Param uuid path string true "label uuid" // @Param payload body LabelDTO true "payload" // @Success 200 @@ -576,16 +580,16 @@ func (co *controller) deleteLabel(c *gin.Context) { c.Status(http.StatusOK) } -// @Summary Прикрепить метку к товару -// @Description Прикрепить метку к товару -// @Tags Merch labels -// @Security BearerAuth -// Accept json -// @Param payload body LabelLink true "payload" -// @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 -// @Router /merch/labels/attach [post] +// @Summary Прикрепить метку к товару +// @Description Прикрепить метку к товару +// @Tags Merch labels +// @Security BearerAuth +// @Accept json +// @Param payload body LabelLink true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/attach [post] func (co *controller) attachLabel(c *gin.Context) { const logMsg = "Merch | Attach label" @@ -611,16 +615,16 @@ func (co *controller) attachLabel(c *gin.Context) { c.Status(http.StatusOK) } -// @Summary Удалить привязку метки к товару -// @Description Удалить привязку метки к товару -// @Tags Merch labels -// @Security BearerAuth -// Accept json -// @Param payload body LabelLink true "payload" -// @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 -// @Router /merch/labels/detach [post] +// @Summary Удалить привязку метки к товару +// @Description Удалить привязку метки к товару +// @Tags Merch labels +// @Security BearerAuth +// @Accept json +// @Param payload body LabelLink true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/detach [post] func (co *controller) detachLabel(c *gin.Context) { const logMsg = "Merch | Detach label" @@ -650,7 +654,7 @@ func (co *controller) detachLabel(c *gin.Context) { // @Description Получить метки товара по его uuid // @Tags Merch labels // @Security BearerAuth -// @Produce json +// @Produce json // @Param uuid path string true "label uuid" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -686,10 +690,10 @@ func (co *controller) getMerchLabels(c *gin.Context) { // @Description Получить нулевые цены // @Tags Merch zero prices // @Security BearerAuth -// @Produce json -// @Success 200 {array} ZeroPrice -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Produce json +// @Success 200 {array} ZeroPrice +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/zeroprices [get] func (co *controller) getZeroPrices(c *gin.Context) { const logMsg = "Merch | Get zero prices" @@ -715,11 +719,11 @@ func (co *controller) getZeroPrices(c *gin.Context) { // @Description Пометить нулевые цены как удаленные // @Tags Merch zero prices // @Security BearerAuth -// Accept json +// @Accept json // @Param payload body DeleteZeroPrices true "payload" // @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/zeroprices [delete] func (co *controller) deleteZeroPrices(c *gin.Context) { const logMsg = "Merch | Delete zero prices" From aeb5cb819b0d0bfcd192e9c13d3951946017b7dc Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 4 Nov 2025 16:49:04 +0300 Subject: [PATCH 82/87] =?UTF-8?q?=D1=81hanged=20ownership=20validation=20f?= =?UTF-8?q?or=20user'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) } From a338fd03b28378add0d8162217753c5dace9eb56 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 6 Dec 2025 17:32:45 +0300 Subject: [PATCH 83/87] added: time util --- internal/interfaces/utils.go | 6 +++++- pkg/utils/time.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 pkg/utils/time.go diff --git a/internal/interfaces/utils.go b/internal/interfaces/utils.go index dd6b773..31cd891 100644 --- a/internal/interfaces/utils.go +++ b/internal/interfaces/utils.go @@ -1,6 +1,9 @@ package interfaces -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "time" +) type Utils interface { IsEmail(email string) bool @@ -8,4 +11,5 @@ type Utils interface { GetRefreshUuidFromContext(c *gin.Context) (string, error) HashPassword(password string) (string, error) ComparePasswords(hashedPassword string, plainPassword string) error + ParseTime(t string) (time.Time, error) } diff --git a/pkg/utils/time.go b/pkg/utils/time.go new file mode 100644 index 0000000..01bfaf6 --- /dev/null +++ b/pkg/utils/time.go @@ -0,0 +1,11 @@ +package utils + +import "time" + +func (u *Utils) ParseTime(t string) (time.Time, error) { + timeStr, err := time.Parse(time.RFC3339, t) + if err != nil { + return time.Time{}, err + } + return timeStr, nil +} From d997d8bfa4c0a57b68f12a36e66a1b5b3527b7b1 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 6 Dec 2025 17:33:18 +0300 Subject: [PATCH 84/87] added: delete zero prices in period --- internal/api/merch/controller.go | 101 ++++++++++++++++++++++--------- internal/api/merch/repository.go | 17 ++++++ internal/api/merch/service.go | 4 ++ 3 files changed, 94 insertions(+), 28 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 7ea9636..a9f922a 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -54,6 +54,9 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref zeroPricesGroup := merchGroup.Group("/zeroprices", authMW) zeroPricesGroup.GET("", h.controller.getZeroPrices) zeroPricesGroup.DELETE("", h.controller.deleteZeroPrices) + + zeroPricesGroup.DELETE("/period", h.controller.deleteZeroPricesPeriod) + } // @Summary Добавить новый мерч @@ -156,7 +159,7 @@ func (co *controller) getAllMerch(c *gin.Context) { // @Description Обновить информацию про мерч по его uuid в json-е // @Tags Merch // @Security BearerAuth -// @Accept json +// @Accept json // @Param body body UpdateMerchDTO true "merch_uuid" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -194,7 +197,7 @@ func (co *controller) updateMerch(c *gin.Context) { // @Failure 400 {object} responses.ErrorResponse400 // @Failure 500 {object} responses.ErrorResponse500 // -// @Router /merch/{uuid} [delete] +// @Router /merch/{uuid} [delete] func (co *controller) deleteMerch(c *gin.Context) { merchUuid := c.Param("uuid") if merchUuid == "" { @@ -229,9 +232,9 @@ func (co *controller) deleteMerch(c *gin.Context) { // @Failure 500 {object} responses.ErrorResponse500 // @Router /prices [get] // -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 -// @Router /prices [get] +// @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", "")) @@ -442,7 +445,7 @@ func (co *controller) deleteMerchImage(c *gin.Context) { // @Description Создать новую метку для товара // @Tags Merch labels // @Security BearerAuth -// @Accept json +// @Accept json // @Param payload body LabelDTO true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -507,7 +510,7 @@ func (co *controller) getLabels(c *gin.Context) { // @Description Изменить метку // @Tags Merch labels // @Security BearerAuth -// @Accept json +// @Accept json // @Param uuid path string true "label uuid" // @Param payload body LabelDTO true "payload" // @Success 200 @@ -580,16 +583,16 @@ func (co *controller) deleteLabel(c *gin.Context) { c.Status(http.StatusOK) } -// @Summary Прикрепить метку к товару -// @Description Прикрепить метку к товару -// @Tags Merch labels -// @Security BearerAuth -// @Accept json -// @Param payload body LabelLink true "payload" -// @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 -// @Router /merch/labels/attach [post] +// @Summary Прикрепить метку к товару +// @Description Прикрепить метку к товару +// @Tags Merch labels +// @Security BearerAuth +// @Accept json +// @Param payload body LabelLink true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/attach [post] func (co *controller) attachLabel(c *gin.Context) { const logMsg = "Merch | Attach label" @@ -615,16 +618,16 @@ func (co *controller) attachLabel(c *gin.Context) { c.Status(http.StatusOK) } -// @Summary Удалить привязку метки к товару -// @Description Удалить привязку метки к товару -// @Tags Merch labels -// @Security BearerAuth -// @Accept json -// @Param payload body LabelLink true "payload" -// @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 -// @Router /merch/labels/detach [post] +// @Summary Удалить привязку метки к товару +// @Description Удалить привязку метки к товару +// @Tags Merch labels +// @Security BearerAuth +// @Accept json +// @Param payload body LabelLink true "payload" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/detach [post] func (co *controller) detachLabel(c *gin.Context) { const logMsg = "Merch | Detach label" @@ -719,7 +722,7 @@ func (co *controller) getZeroPrices(c *gin.Context) { // @Description Пометить нулевые цены как удаленные // @Tags Merch zero prices // @Security BearerAuth -// @Accept json +// @Accept json // @Param payload body DeleteZeroPrices true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -749,3 +752,45 @@ func (co *controller) deleteZeroPrices(c *gin.Context) { } c.Status(http.StatusOK) } + +// @Summary Пометить нулевые цены как удаленные за указанный период +// @Description Пометить нулевые цены как удаленные за указанный период +// @Tags Merch zero prices +// @Security BearerAuth +// @Param start query string true "start" +// @Param end query string true "end" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/zeroprices/period [delete] +func (co *controller) deleteZeroPricesPeriod(c *gin.Context) { + const logMsg = "Merch | Delete zero prices period" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + start, err := co.utils.ParseTime(c.Query("start")) + if err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + end, err := co.utils.ParseTime(c.Query("end")) + if err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + if err = co.service.deleteZeroPricesPeriod(userUuid, start, end); err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.Status(http.StatusOK) +} diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index e84f551..4a5f3ce 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -42,6 +42,7 @@ type prices interface { getZeroPrices(userUuid string) ([]ZeroPrice, error) deleteZeroPrices(list []DeleteZeroPrices) error + deleteZeroPricesPeriod(userUuid string, start, end time.Time) error } type labels interface { @@ -371,3 +372,19 @@ func (r *Repo) deleteZeroPrices(list []DeleteZeroPrices) error { } return nil } + +func (r *Repo) deleteZeroPricesPeriod(userUuid string, start, end time.Time) error { + if err := r.db.Exec(` + UPDATE prices + SET deleted_at = ? + FROM merch + WHERE prices.merch_uuid = merch.merch_uuid + AND merch.user_uuid = ? + AND prices.price = 0 + AND prices.deleted_at IS NULL + AND prices.created_at BETWEEN ? AND ?; + `, time.Now().UTC(), userUuid, start, end).Error; err != nil { + return err + } + return nil +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index d409b71..ae6c2f5 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -662,3 +662,7 @@ func (s *service) deleteZeroPrices(userUuid string, list []DeleteZeroPrices) err return s.repo.deleteZeroPrices(toDelete) } + +func (s *service) deleteZeroPricesPeriod(userUuid string, start, end time.Time) error { + return s.repo.deleteZeroPricesPeriod(userUuid, start, end) +} From f9eac067be59003f3e0bff1c4012ef732313fe00 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 6 Dec 2025 17:33:29 +0300 Subject: [PATCH 85/87] swagger docs update --- docs/docs.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 47 +++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 30 ++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) diff --git a/docs/docs.go b/docs/docs.go index b97688b..a232b91 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -675,6 +675,53 @@ const docTemplate = `{ } } }, + "/merch/zeroprices/period": { + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Пометить нулевые цены как удаленные за указанный период", + "tags": [ + "Merch zero prices" + ], + "summary": "Пометить нулевые цены как удаленные за указанный период", + "parameters": [ + { + "type": "string", + "description": "start", + "name": "start", + "in": "query", + "required": true + }, + { + "type": "string", + "description": "end", + "name": "end", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, "/merch/{uuid}": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index f746266..74a0031 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -667,6 +667,53 @@ } } }, + "/merch/zeroprices/period": { + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Пометить нулевые цены как удаленные за указанный период", + "tags": [ + "Merch zero prices" + ], + "summary": "Пометить нулевые цены как удаленные за указанный период", + "parameters": [ + { + "type": "string", + "description": "start", + "name": "start", + "in": "query", + "required": true + }, + { + "type": "string", + "description": "end", + "name": "end", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse400" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/responses.ErrorResponse500" + } + } + } + } + }, "/merch/{uuid}": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 883b177..31eaa0d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -674,6 +674,36 @@ paths: summary: Получить нулевые цены tags: - Merch zero prices + /merch/zeroprices/period: + delete: + description: Пометить нулевые цены как удаленные за указанный период + parameters: + - description: start + in: query + name: start + required: true + type: string + - description: end + in: query + name: end + required: true + type: string + responses: + "200": + description: OK + "400": + description: Bad Request + schema: + $ref: '#/definitions/responses.ErrorResponse400' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/responses.ErrorResponse500' + security: + - BearerAuth: [] + summary: Пометить нулевые цены как удаленные за указанный период + tags: + - Merch zero prices /prices: get: description: Получить цены мерча за период From 4c59ab3f58f5f1cbe2eedf3ec5543d93cb6e385e Mon Sep 17 00:00:00 2001 From: nquidox Date: Sat, 6 Dec 2025 19:14:21 +0300 Subject: [PATCH 86/87] return origin name instead of code --- internal/api/merch/dto.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index 7ee7281..5b9180c 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -86,7 +86,7 @@ type ZeroPrice struct { CreatedAt time.Time `json:"created_at"` MerchUuid string `json:"merch_uuid"` Name string `json:"name"` - Origin string `json:"origin"` + Origin Origin `json:"origin"` } type DeleteZeroPrices struct { From 8f2b0470b19e9dd4b4cece4a76c8b8e22f930077 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 7 Dec 2025 13:37:15 +0300 Subject: [PATCH 87/87] false zero price bugfix --- internal/api/merch/repository.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 4a5f3ce..fe5dd7d 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -339,8 +339,8 @@ func (r *Repo) getZeroPrices(userUuid string) ([]ZeroPrice, error) { WITH price_with_neighbors AS ( SELECT 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, - LEAD(price) OVER (PARTITION BY p.merch_uuid ORDER BY p.created_at, p.id) AS next_price + LAG(price) OVER (PARTITION BY p.merch_uuid, p.origin ORDER BY p.created_at, p.id) AS prev_price, + LEAD(price) OVER (PARTITION BY p.merch_uuid, p.origin ORDER BY p.created_at, p.id) AS next_price FROM prices AS p JOIN merch as m ON m.merch_uuid = p.merch_uuid WHERE p.deleted_at IS NULL