generated from VLADIMIR/template
add places operations
This commit is contained in:
+37
-3
@@ -307,7 +307,18 @@ func (s *server) AddScenarioPlace(ctx context.Context, req *proto.AddScenarioPla
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
panic("unimplemented")
|
||||
err := s.scenarioService.AddScenarioPlace(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
convertPlace(req.Place),
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.AddScenarioPlaceRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.AddScenarioPlaceRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) UpdateScenarioPlace(ctx context.Context, req *proto.UpdateScenarioPlaceReq) (*proto.UpdateScenarioPlaceRsp, error) {
|
||||
@@ -316,7 +327,19 @@ func (s *server) UpdateScenarioPlace(ctx context.Context, req *proto.UpdateScena
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
panic("unimplemented")
|
||||
err := s.scenarioService.UpdateScenarioPlace(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.Code,
|
||||
convertPlace(req.Place),
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.UpdateScenarioPlaceRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.UpdateScenarioPlaceRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) DeleteScenarioPlace(ctx context.Context, req *proto.DeleteScenarioPlaceReq) (*proto.DeleteScenarioPlaceRsp, error) {
|
||||
@@ -325,5 +348,16 @@ func (s *server) DeleteScenarioPlace(ctx context.Context, req *proto.DeleteScena
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
panic("unimplemented")
|
||||
err := s.scenarioService.DeleteScenarioPlace(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.Code,
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.DeleteScenarioPlaceRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.DeleteScenarioPlaceRsp{}, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"evening_detective_server/internal/modules/formatter_utils"
|
||||
"evening_detective_server/internal/modules/storytelling"
|
||||
"evening_detective_server/internal/services/scenarios_service"
|
||||
proto "evening_detective_server/proto"
|
||||
@@ -105,3 +106,61 @@ func mapKey(o *storytelling.Key) *proto.Key {
|
||||
Name: o.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func convertPlace(o *proto.Place) *storytelling.Place {
|
||||
return &storytelling.Place{
|
||||
Code: o.Code,
|
||||
Name: formatter_utils.FormatString(o.Name),
|
||||
Text: formatter_utils.FormatText(o.Text),
|
||||
Image: o.Image,
|
||||
Hidden: o.Hidden,
|
||||
Applications: convertApplications(o.Applications),
|
||||
Doors: convertDoors(o.Doors),
|
||||
Keys: convertKeys(o.Keys),
|
||||
}
|
||||
}
|
||||
|
||||
func convertApplications(o []*proto.Application) []*storytelling.Application {
|
||||
res := make([]*storytelling.Application, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, convertApplication(item))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func convertDoors(o []*proto.Door) []*storytelling.Door {
|
||||
res := make([]*storytelling.Door, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, convertDoor(item))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func convertKeys(o []*proto.Key) []*storytelling.Key {
|
||||
res := make([]*storytelling.Key, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, convertKey(item))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func convertApplication(o *proto.Application) *storytelling.Application {
|
||||
return &storytelling.Application{
|
||||
Name: formatter_utils.FormatString(o.Name),
|
||||
Image: o.Image,
|
||||
}
|
||||
}
|
||||
|
||||
func convertDoor(o *proto.Door) *storytelling.Door {
|
||||
return &storytelling.Door{
|
||||
Code: o.Code,
|
||||
Name: formatter_utils.FormatString(o.Name),
|
||||
Keys: convertKeys(o.Keys),
|
||||
}
|
||||
}
|
||||
|
||||
func convertKey(o *proto.Key) *storytelling.Key {
|
||||
return &storytelling.Key{
|
||||
Name: formatter_utils.FormatString(o.Name),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package formatter
|
||||
|
||||
type IFormatter interface {
|
||||
FormatText(text string) string
|
||||
FormatString(text string) string
|
||||
}
|
||||
@@ -1,17 +1,11 @@
|
||||
package formatter
|
||||
package formatter_utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type service struct{}
|
||||
|
||||
func NewFormatter() IFormatter {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) FormatText(text string) string {
|
||||
func FormatText(text string) string {
|
||||
scanner := bufio.NewScanner(strings.NewReader(text))
|
||||
|
||||
scanner.Split(bufio.ScanLines)
|
||||
@@ -41,7 +35,7 @@ func (s *service) FormatText(text string) string {
|
||||
return res.String()
|
||||
}
|
||||
|
||||
func (s *service) FormatString(text string) string {
|
||||
func FormatString(text string) string {
|
||||
l := strings.TrimSpace(text)
|
||||
if strings.HasPrefix(l, "--") {
|
||||
l = strings.Replace(l, "--", "—", 1)
|
||||
+2
-3
@@ -1,4 +1,4 @@
|
||||
package formatter
|
||||
package formatter_utils
|
||||
|
||||
import "testing"
|
||||
|
||||
@@ -31,8 +31,7 @@ func Test_service_FormatText(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var s service
|
||||
got := s.FormatText(tt.text)
|
||||
got := FormatText(tt.text)
|
||||
if got != tt.want {
|
||||
t.Errorf("FormatText() = %v, want %v", got, tt.want)
|
||||
}
|
||||
@@ -204,3 +204,53 @@ func (s *ScenariosRepo) DeleteScenarioByID(
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetStoryByScenarioID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
) (string, error) {
|
||||
story := ""
|
||||
row := s.pool.QueryRow(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenario
|
||||
FROM scenarios
|
||||
WHERE scenarios.id = $1`,
|
||||
id,
|
||||
)
|
||||
err := row.Scan(
|
||||
&story,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", ErrScenarioNotFound
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
return story, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) UpdateStoryByScenarioID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
story string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
scenario = $1,
|
||||
updated_at = NOW()
|
||||
WHERE id = $2`,
|
||||
story,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrScenarioNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ func mapScenarios(o []*repos.Scenario) ([]*Scenario, error) {
|
||||
}
|
||||
|
||||
func mapScenario(o *repos.Scenario) (*Scenario, error) {
|
||||
story := &storytelling.Story{}
|
||||
if err := json.Unmarshal([]byte(o.Scenario), story); err != nil {
|
||||
story, err := mapStory(o.Scenario)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Scenario{
|
||||
@@ -37,3 +37,19 @@ func mapScenario(o *repos.Scenario) (*Scenario, error) {
|
||||
IsDeleted: o.IsDeleted,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func mapStory(o string) (*storytelling.Story, error) {
|
||||
res := &storytelling.Story{}
|
||||
if err := json.Unmarshal([]byte(o), res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func convertStory(o *storytelling.Story) (string, error) {
|
||||
b, err := json.Marshal(o)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package scenarios_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"evening_detective_server/internal/modules/storytelling"
|
||||
"evening_detective_server/internal/repos/scenarios_repo"
|
||||
)
|
||||
|
||||
@@ -67,3 +69,81 @@ func (s *ScenarioService) DeleteScenarioByID(
|
||||
) error {
|
||||
return s.scenariosRepo.DeleteScenarioByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *ScenarioService) AddScenarioPlace(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
place *storytelling.Place,
|
||||
) error {
|
||||
story, err := s.getStory(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
story.Places = append(
|
||||
story.Places,
|
||||
place,
|
||||
)
|
||||
return s.updateStory(ctx, id, story)
|
||||
}
|
||||
|
||||
func (s *ScenarioService) UpdateScenarioPlace(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
code string,
|
||||
place *storytelling.Place,
|
||||
) error {
|
||||
story, err := s.getStory(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range story.Places {
|
||||
if story.Places[i].Code == code {
|
||||
story.Places[i] = place
|
||||
break
|
||||
}
|
||||
}
|
||||
return s.updateStory(ctx, id, story)
|
||||
}
|
||||
|
||||
func (s *ScenarioService) DeleteScenarioPlace(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
code string,
|
||||
) error {
|
||||
story, err := s.getStory(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range story.Places {
|
||||
if story.Places[i].Code == code {
|
||||
story.Places = append(story.Places[:i], story.Places[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return s.updateStory(ctx, id, story)
|
||||
}
|
||||
|
||||
func (s *ScenarioService) getStory(ctx context.Context, id int) (*storytelling.Story, error) {
|
||||
storyString, err := s.scenariosRepo.GetStoryByScenarioID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapStory(storyString)
|
||||
}
|
||||
|
||||
func (s *ScenarioService) updateStory(ctx context.Context, id int, story *storytelling.Story) error {
|
||||
mapCodes := map[string]struct{}{}
|
||||
for _, place := range story.Places {
|
||||
mapCodes[place.Code] = struct{}{}
|
||||
}
|
||||
if len(mapCodes) != len(story.Places) {
|
||||
return errors.New("Такой код точки уже существует")
|
||||
}
|
||||
|
||||
storyString, err := convertStory(story)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.scenariosRepo.UpdateStoryByScenarioID(ctx, id, storyString)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user