Skip to content

Commit 34254fc

Browse files
committed
[BE] ログ削除、境界チェック追加、バリデーション追加
1 parent 781fd7c commit 34254fc

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

backend/dto/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ type StartGameResult struct {
1515
}
1616

1717
type CheckAnswerRequest struct {
18-
Answer string `json:"answer" binding:"required"`
18+
Answer string `json:"answer" binding:"required,max=200"`
1919
}

backend/services/hint_service.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import (
1818
const MinRoundAnswerRevealDuration = 90 * time.Second
1919

2020
var (
21-
ErrRoundNotFound = errors.New("round not found")
22-
ErrRoundNotFinished = errors.New("round is not finished yet")
23-
ErrRoundTooEarly = errors.New("game has not been played long enough")
24-
ErrBookmarkNotFound = errors.New("bookmark not found")
21+
ErrRoundNotFound = errors.New("round not found")
22+
ErrRoundNotFinished = errors.New("round is not finished yet")
23+
ErrRoundTooEarly = errors.New("game has not been played long enough")
24+
ErrBookmarkNotFound = errors.New("bookmark not found")
25+
ErrNoAnswersAvailable = errors.New("no answers available")
2526
)
2627

2728
type IHintService interface {
@@ -186,8 +187,6 @@ func (s *HintService) StartGame() (*dto.StartGameResult, error) {
186187

187188
text := res.Text()
188189

189-
fmt.Println(text)
190-
191190
if err := json.Unmarshal([]byte(text), &geminiData); err != nil {
192191
return nil, fmt.Errorf("JSONパースに失敗しました: %w (raw: %s)", err, text)
193192
}
@@ -221,6 +220,9 @@ func (s *HintService) GetAnswer(id uint) (string, error) {
221220
if err != nil {
222221
return "", err
223222
}
223+
if len(round.Answers) == 0 {
224+
return "", fmt.Errorf("%w: id=%d", ErrNoAnswersAvailable, id)
225+
}
224226
if !round.IsFinished {
225227
if time.Since(round.CreatedAt) < MinRoundAnswerRevealDuration {
226228
return "", fmt.Errorf("%w", ErrRoundTooEarly)
@@ -257,6 +259,9 @@ func (s *HintService) GetFinishedRoundByID(id uint) (*dto.RoundResponse, error)
257259
if !round.IsFinished {
258260
return nil, fmt.Errorf("%w: id=%d", ErrRoundNotFinished, id)
259261
}
262+
if len(round.Answers) == 0 {
263+
return nil, fmt.Errorf("%w: id=%d", ErrNoAnswersAvailable, id)
264+
}
260265
result := &dto.RoundResponse{
261266
ID: round.ID,
262267
Answer: round.Answers[0],
@@ -274,6 +279,9 @@ func (s *HintService) BookmarkRound(id uint) (*dto.RoundResponse, error) {
274279
if !round.IsFinished {
275280
return nil, fmt.Errorf("%w: id=%d", ErrRoundNotFinished, id)
276281
}
282+
if len(round.Answers) == 0 {
283+
return nil, fmt.Errorf("%w: id=%d", ErrNoAnswersAvailable, id)
284+
}
277285
if err := s.repository.BookmarkRound(id); err != nil {
278286
return nil, fmt.Errorf("failed to bookmark round: %w", err)
279287
}
@@ -295,6 +303,9 @@ func (s *HintService) GetRandomBookmark() (*dto.RoundResponse, error) {
295303
return nil, ErrBookmarkNotFound
296304
}
297305

306+
if len(round.Answers) == 0 {
307+
return nil, ErrNoAnswersAvailable
308+
}
298309
return &dto.RoundResponse{
299310
ID: round.ID,
300311
Answer: round.Answers[0],
@@ -312,6 +323,9 @@ func (s *HintService) GetBookmarkedList() ([]dto.RoundResponse, error) {
312323
response := []dto.RoundResponse{}
313324

314325
for _, r := range rounds {
326+
if len(r.Answers) == 0 {
327+
continue
328+
}
315329
limit := 4
316330
if len(r.Hints) < limit {
317331
limit = len(r.Hints)

0 commit comments

Comments
 (0)