diff --git a/internal/api/http/handlers/widget/widget.go b/internal/api/http/handlers/widget/widget.go index c158f5b..b94999b 100644 --- a/internal/api/http/handlers/widget/widget.go +++ b/internal/api/http/handlers/widget/widget.go @@ -93,6 +93,10 @@ func GetStreamersWidgets(widgetService model.WidgetService) echo.HandlerFunc { return request.JSON(http.StatusInternalServerError, err.Error()) } + if widgets == nil { + return request.JSON(http.StatusOK, []string{}) + } + return request.JSON(http.StatusOK, widgets) } } diff --git a/internal/model/interfaces.go b/internal/model/interfaces.go index edde1a9..764a896 100644 --- a/internal/model/interfaces.go +++ b/internal/model/interfaces.go @@ -24,7 +24,7 @@ type WidgetService interface { audio string, name string, ) (WidgetID, error) - GetWidgetsByStreamer(ctx context.Context, streamerID int) ([]*GetWidgetDb, error) + GetWidgetsByStreamer(ctx context.Context, streamerID int) ([]*WidgetWithFileLink, error) //GetWidgetByID(ctx context.Context, widgetID WidgetID) ([]*GetWidgetDb, error) //GetWidgetHTML(ctx context.Context, streamerID StreamerID) (WidgetHTML, error) diff --git a/internal/model/models.go b/internal/model/models.go index 0bed742..d29f265 100644 --- a/internal/model/models.go +++ b/internal/model/models.go @@ -1,26 +1,37 @@ package model import ( + "github.com/google/uuid" "time" ) type GetWidgetDb struct { - ID int `db:"id"` - StreamerID int `db:"streamer_id"` - TemplateID int `db:"template_id"` - Duration int `db:"duration"` - MinAmount int `db:"min_amount"` - MaxAmount int `db:"max_amount"` - CreatedAt time.Time `db:"created_at"` - UpdatedAt time.Time `db:"updated_at"` + ID int `db:"id" json:"id"` + StreamerID int `db:"streamer_id" json:"streamer_id"` + TemplateID int `db:"template_id" json:"template_id"` + Duration int `db:"duration" json:"duration"` + MinAmount int `db:"min_amount" json:"min_amount"` + MaxAmount int `db:"max_amount" json:"max_amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + UpdatedAt time.Time `db:"updated_at" json:"updated_at"` + GroupID int `db:"group_id" json:"group_id"` // Поля для изображения - ImageFileName string `db:"image_file_name"` - ImageType string `db:"image_type"` + ImageFileId uuid.UUID `db:"image_id" json:"image_id"` + ImageFileName string `db:"image_file_name" json:"image_file_name"` + ImageType string `db:"image_type" json:"image_type"` + ImageLink string `json:"image_link"` // Поля для аудио - AudioFileName string `db:"audio_file_name"` - AudioType string `db:"audio_type"` + AudioFileId uuid.UUID `db:"audio_id" json:"audio_id"` + AudioFileName string `db:"audio_file_name" json:"audio_file_name"` + AudioType string `db:"audio_type" json:"audio_type"` + AudioLink string `json:"audio_link"` +} + +type WidgetWithFileLink struct { + GroupID int `json:"group_id"` + Widgets []*GetWidgetDb `json:"widgets"` } type Donat struct { diff --git a/internal/model/sql/model.go b/internal/model/sql/model.go index 5d0c7af..2f23105 100644 --- a/internal/model/sql/model.go +++ b/internal/model/sql/model.go @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS widgets ( id SERIAL PRIMARY KEY, streamer_id INTEGER NOT NULL, template_id INTEGER, + group_id INTEGER NOT NULL DEFAULT 1, name VARCHAR(50) NOT NULL, image UUID REFERENCES files(id) NOT NULL ON DELETE CASCADE, diff --git a/internal/model/sql/query.go b/internal/model/sql/query.go index 148f4b1..157b6eb 100644 --- a/internal/model/sql/query.go +++ b/internal/model/sql/query.go @@ -96,8 +96,10 @@ SELECT w.id, w.streamer_id, w.template_id, + img.id AS image_id, img.file_name AS image_file_name, img.file_type AS image_type, + audio.id AS audio_id, audio.file_name AS audio_file_name, audio.file_type AS audio_type, w.duration, diff --git a/internal/service/widget/widget.go b/internal/service/widget/widget.go index 636a1a5..c9a105c 100644 --- a/internal/service/widget/widget.go +++ b/internal/service/widget/widget.go @@ -70,15 +70,31 @@ func (widgetService *ServiceWidget) CreateWidget( func (widgetService *ServiceWidget) GetWidgetsByStreamer( ctx context.Context, streamerID int, -) ([]*model.GetWidgetDb, error) { - - widget, err := widgetService.widgetRepo.GetWidgetsByStreamerID(ctx, streamerID) +) ([]*model.WidgetWithFileLink, error) { + widgets, err := widgetService.widgetRepo.GetWidgetsByStreamerID(ctx, streamerID) if err != nil { slog.Error(err.Error()) return nil, err } - return widget, nil + groupedWidgets := make(map[int][]*model.GetWidgetDb) + + for _, widget := range widgets { + widget.AudioLink = fmt.Sprintf("http://localhost/%d", widget.AudioFileId) + widget.ImageLink = fmt.Sprintf("http://localhost/%d", widget.ImageLink) + + groupedWidgets[widget.GroupID] = append(groupedWidgets[widget.GroupID], widget) + } + + var result []*model.WidgetWithFileLink + for groupID, widgets := range groupedWidgets { + result = append(result, &model.WidgetWithFileLink{ + GroupID: groupID, + Widgets: widgets, + }) + } + + return result, nil } //