Skip to content

Commit

Permalink
Merge pull request #276 from hufscheer/feat/#271-timeline
Browse files Browse the repository at this point in the history
[FEAT] 경기 종료 이후 새로운 타임라인 등록 막기
  • Loading branch information
Zena0128 authored Oct 6, 2024
2 parents c5ed37b + 5e0544d commit 1a591d1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/sports/server/command/game/domain/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

import static com.sports.server.command.timeline.exception.TimelineErrorMessage.GAME_ALREADY_FINISHED;

@Entity
@Getter
@Table(name = "games")
Expand Down Expand Up @@ -253,6 +255,12 @@ public Quarter getQuarter() {
.orElseThrow(() -> new IllegalArgumentException("해당 쿼터가 존재하지 않습니다."));
}

public void checkStateForTimeline() {
if (this.getState().equals(GameState.FINISHED)) {
throw new CustomException(HttpStatus.BAD_REQUEST, GAME_ALREADY_FINISHED);
}
}

@Override
public boolean isManagedBy(Member manager) {
return this.manager.equals(manager);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.sports.server.command.timeline.application;

import com.sports.server.command.game.domain.Game;
import com.sports.server.command.game.domain.GameState;
import com.sports.server.command.member.domain.Member;
import com.sports.server.command.timeline.domain.Timeline;
import com.sports.server.command.timeline.domain.TimelineRepository;
import com.sports.server.command.timeline.dto.TimelineRequest;
import com.sports.server.command.timeline.exception.TimelineErrorMessage;
import com.sports.server.command.timeline.mapper.TimelineMapper;
import com.sports.server.common.application.EntityUtils;
import com.sports.server.common.application.PermissionValidator;
Expand All @@ -14,6 +16,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import static com.sports.server.command.timeline.exception.TimelineErrorMessage.GAME_ALREADY_FINISHED;

@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -24,11 +28,11 @@ public class TimelineService {

public void register(Member manager, Long gameId, TimelineRequest request) {
Game game = entityUtils.getEntity(gameId, Game.class);
game.checkStateForTimeline();
PermissionValidator.checkPermission(game, manager);

Timeline timeline = timelineMapper.toEntity(game, request);
timeline.apply();

timelineRepository.save(timeline);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sports.server.command.timeline.exception;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TimelineErrorMessage {

public static final String GAME_ALREADY_FINISHED = "종료된 게임에 새로운 타임라인을 등록할 수 없습니다.";

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.sports.server.command.timeline.domain.ScoreTimeline;
import com.sports.server.command.timeline.domain.Timeline;
import com.sports.server.command.timeline.dto.TimelineRequest;
import com.sports.server.command.timeline.exception.TimelineErrorMessage;
import com.sports.server.common.application.EntityUtils;
import com.sports.server.common.exception.CustomException;
import com.sports.server.common.exception.UnauthorizedException;
Expand Down Expand Up @@ -294,4 +295,24 @@ class DeleteTest {
.isInstanceOf(CustomException.class);
}
}

@Test
void 경기_종료__타임라인을_등록하려고_하면_에러가_발생한다() {
// given
Long team1Id = 1L;
Long team1PlayerId = 1L;
Long finishedGameId = 2L;

TimelineRequest.RegisterScore request = new TimelineRequest.RegisterScore(
team1Id,
quarterId,
team1PlayerId,
3
);

// when & then
assertThatThrownBy(() -> timelineService.register(manager, finishedGameId, request))
.isInstanceOf(CustomException.class)
.hasMessage(TimelineErrorMessage.GAME_ALREADY_FINISHED);
}
}
3 changes: 2 additions & 1 deletion src/test/resources/timeline-fixture.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ VALUES (1, '1쿼터', 1),
-- 경기
INSERT INTO games (id, sport_id, manager_id, league_id, name, start_time, video_id, quarter_changed_at,
game_quarter, state, round)
VALUES (1, 1, 1, 1, '농구 대전', '2023-11-12T10:00:00', 'abc123', '2023-11-12T10:15:00', '3쿼터', 'PLAYING', '4강');
VALUES (1, 1, 1, 1, '농구 대전', '2023-11-12T10:00:00', 'abc123', '2023-11-12T10:15:00', '3쿼터', 'PLAYING', '4강'),
(2, 1, 1, 1, '농구 대전', '2023-11-12T10:00:00', 'abc123', '2023-11-12T10:15:00', '3쿼터', 'FINISHED', '4강');

--
INSERT INTO league_teams (name, logo_image_url, manager_id, organization_id, league_id)
Expand Down

0 comments on commit 1a591d1

Please sign in to comment.