add scenarios methods

This commit is contained in:
2026-07-13 23:48:20 +07:00
parent 487b1aa436
commit db746f2123
17 changed files with 3620 additions and 118 deletions
@@ -0,0 +1,21 @@
package scenarios_service
import (
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos"
"time"
)
type Scenario struct {
ID int
Name string
Description *string
Image *string
Story *storytelling.Story
Author *repos.User
Status string
UpdatedAt time.Time
CreatedAt time.Time
PublishedAt *time.Time
IsDeleted bool
}
@@ -0,0 +1,39 @@
package scenarios_service
import (
"encoding/json"
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos"
)
func mapScenarios(o []*repos.Scenario) ([]*Scenario, error) {
res := make([]*Scenario, 0, len(o))
for _, item := range o {
scenario, err := mapScenario(item)
if err != nil {
return nil, err
}
res = append(res, scenario)
}
return res, nil
}
func mapScenario(o *repos.Scenario) (*Scenario, error) {
story := &storytelling.Story{}
if err := json.Unmarshal([]byte(o.Scenario), story); err != nil {
return nil, err
}
return &Scenario{
ID: o.ID,
Name: o.Name,
Description: o.Description,
Image: o.Image,
Story: story,
Author: o.Author,
Status: o.Status,
UpdatedAt: o.UpdatedAt,
CreatedAt: o.CreatedAt,
PublishedAt: o.PublishedAt,
IsDeleted: o.IsDeleted,
}, nil
}
@@ -0,0 +1,69 @@
package scenarios_service
import (
"context"
"evening_detective_server/internal/repos/scenarios_repo"
)
type ScenarioService struct {
scenariosRepo *scenarios_repo.ScenariosRepo
}
func NewScenarioService(
scenariosRepo *scenarios_repo.ScenariosRepo,
) *ScenarioService {
return &ScenarioService{
scenariosRepo: scenariosRepo,
}
}
func (s *ScenarioService) AddScenario(
ctx context.Context,
name string,
authorId int,
) (int, error) {
id, err := s.scenariosRepo.AddScenario(ctx, name, authorId)
if err != nil {
return 0, err
}
return id, nil
}
func (s *ScenarioService) GetScenariosByAuthorID(
ctx context.Context,
authorId int,
) ([]*Scenario, error) {
scenarios, err := s.scenariosRepo.GetScenariosByAuthorID(ctx, authorId)
if err != nil {
return nil, err
}
return mapScenarios(scenarios)
}
func (s *ScenarioService) GetScenarioByID(
ctx context.Context,
id int,
) (*Scenario, error) {
scenario, err := s.scenariosRepo.GetScenarioByID(ctx, id)
if err != nil {
return nil, err
}
return mapScenario(scenario)
}
func (s *ScenarioService) UpdateScenarioByID(
ctx context.Context,
id int,
name string,
description string,
image string,
) error {
return s.scenariosRepo.UpdateScenarioByID(ctx, id, name, description, image)
}
func (s *ScenarioService) DeleteScenarioByID(
ctx context.Context,
id int,
) error {
return s.scenariosRepo.DeleteScenarioByID(ctx, id)
}