This commit is contained in:
2026-03-07 19:23:00 +07:00
parent 6ad47cbc38
commit 856f12f2e3
12 changed files with 116 additions and 177 deletions
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"evening_detective/internal/services/story_service"
"evening_detective/internal/services/story_service/models"
"fmt"
"log"
"os"
@@ -19,20 +20,20 @@ func NewFileStoryStorage(filepath string) story_service.IStoryStorage {
}
}
func (s *fileService) Load(ctx context.Context) (*story_service.Story, error) {
func (s *fileService) Load(ctx context.Context) (*models.Story, error) {
data, err := os.ReadFile(s.filepath)
if err != nil {
return nil, fmt.Errorf("story file %s not found", s.filepath)
}
log.Printf("load story from: %s", s.filepath)
story := &story_service.Story{}
story := &models.Story{}
if err := json.Unmarshal(data, story); err != nil {
return nil, err
}
return story, nil
}
func (s *fileService) Save(ctx context.Context, story *story_service.Story) error {
func (s *fileService) Save(ctx context.Context, story *models.Story) error {
data, err := json.Marshal(story)
if err != nil {
return err
+3 -4
View File
@@ -1,12 +1,11 @@
//go:generate mockgen -source=interface.go -destination=mocks/mock.go -package=mocks
package story_storage
import (
"context"
"evening_detective/internal/services/story_service"
"evening_detective/internal/services/story_service/models"
)
type IStoryStorage interface {
Load(ctx context.Context) (*story_service.Story, error)
Save(ctx context.Context, story *story_service.Story) error
Load(ctx context.Context) (*models.Story, error)
Save(ctx context.Context, story *models.Story) error
}
@@ -1,65 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: interface.go
// Package mocks is a generated GoMock package.
package mocks
import (
context "context"
story_service "evening_detective/internal/services/story_service"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockIStoryStorage is a mock of IStoryStorage interface.
type MockIStoryStorage struct {
ctrl *gomock.Controller
recorder *MockIStoryStorageMockRecorder
}
// MockIStoryStorageMockRecorder is the mock recorder for MockIStoryStorage.
type MockIStoryStorageMockRecorder struct {
mock *MockIStoryStorage
}
// NewMockIStoryStorage creates a new mock instance.
func NewMockIStoryStorage(ctrl *gomock.Controller) *MockIStoryStorage {
mock := &MockIStoryStorage{ctrl: ctrl}
mock.recorder = &MockIStoryStorageMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockIStoryStorage) EXPECT() *MockIStoryStorageMockRecorder {
return m.recorder
}
// Load mocks base method.
func (m *MockIStoryStorage) Load(ctx context.Context) (*story_service.Story, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Load", ctx)
ret0, _ := ret[0].(*story_service.Story)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Load indicates an expected call of Load.
func (mr *MockIStoryStorageMockRecorder) Load(ctx interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockIStoryStorage)(nil).Load), ctx)
}
// Save mocks base method.
func (m *MockIStoryStorage) Save(ctx context.Context, story *story_service.Story) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Save", ctx, story)
ret0, _ := ret[0].(error)
return ret0
}
// Save indicates an expected call of Save.
func (mr *MockIStoryStorageMockRecorder) Save(ctx, story interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockIStoryStorage)(nil).Save), ctx, story)
}
@@ -3,23 +3,24 @@ package story_storage
import (
"context"
"evening_detective/internal/services/story_service"
"evening_detective/internal/services/story_service/models"
)
type varService struct {
story *story_service.Story
story *models.Story
}
func NewVarStoryStorage(story *story_service.Story) story_service.IStoryStorage {
func NewVarStoryStorage(story *models.Story) story_service.IStoryStorage {
return &varService{
story: story,
}
}
func (s *varService) Load(ctx context.Context) (*story_service.Story, error) {
func (s *varService) Load(ctx context.Context) (*models.Story, error) {
return s.story, nil
}
func (s *varService) Save(ctx context.Context, story *story_service.Story) error {
func (s *varService) Save(ctx context.Context, story *models.Story) error {
s.story = story
return nil
}