diff --git a/src/sportslive/game/presentation/game_view.py b/src/sportslive/game/presentation/game_view.py index 801e064..11a7cab 100644 --- a/src/sportslive/game/presentation/game_view.py +++ b/src/sportslive/game/presentation/game_view.py @@ -22,4 +22,4 @@ def post(self, request, league_id: int): 게임 등록 API """ self._game_serivce.create_game(league_id, request.data, request.user) - return Response(status=status.HTTP_200_OK) \ No newline at end of file + return Response(status=status.HTTP_201_CREATED) \ No newline at end of file diff --git a/src/sportslive/record/application/record_create_service.py b/src/sportslive/record/application/record_create_service.py index ac340e7..c50d626 100644 --- a/src/sportslive/record/application/record_create_service.py +++ b/src/sportslive/record/application/record_create_service.py @@ -24,18 +24,18 @@ def create_record(self, game_id: int, record_type: str, request_data): game_team_id: int = record_data.get('game_team_id') game_team: GameTeam = self._game_repository.find_game_team_by_id(game_team_id) game: Game = self._game_repository.find_game_by_id(game_id) - new_record = self._create_and_save_record_object(game_team, record_data, game_id, game) + new_record = self._create_and_save_record_object(game_team, record_data, game_id, game, record_type) if record_type == "score": self._create_and_save_score_record_object(game_team, new_record, record_data) elif record_type == "replacement": self._create_and_save_replacement_record_object(new_record, record_data) - def _create_and_save_record_object(self, game_team: GameTeam, record_data, game_id: int, game: Game) -> Record: + def _create_and_save_record_object(self, game_team: GameTeam, record_data, game_id: int, game: Game, record_type: str) -> Record: datetime_recorded_at = record_data.get('recorded_at') int_recorded_at = self._make_integer_recorded_at(datetime_recorded_at, game) recorded_quarter_id = record_data.get('recorded_quarter_id') - new_record: Record = self._create_new_record_object(game_id, game_team, recorded_quarter_id, 'score', int_recorded_at) + new_record: Record = self._create_new_record_object(game_id, game_team, recorded_quarter_id, record_type, int_recorded_at) self._record_repository.save_record(new_record) return new_record diff --git a/src/sportslive/record/application/record_service.py b/src/sportslive/record/application/record_service.py index 7faf6de..ab30e10 100644 --- a/src/sportslive/record/application/record_service.py +++ b/src/sportslive/record/application/record_service.py @@ -8,7 +8,7 @@ def __init__(self, record_repository: RecordRepository, game_repository: GameRep self._record_repository = record_repository self._game_repository = game_repository - def change_record(self, record_id: int, extra_record_id: int, record_type: str, request_data): + def change_record(self, record_id: int, record_type: str, request_data): record_data = self._get_record_data(record_type, request_data) game_team_id: int = record_data.get('game_team_id') game_team: GameTeam = self._game_repository.find_game_team_by_id(game_team_id) @@ -17,21 +17,21 @@ def change_record(self, record_id: int, extra_record_id: int, record_type: str, self._change_and_save_record_object(record_data, target_record) if record_type == "score": - self._change_and_save_score_record_object(game_team, extra_record_id, record_data) + self._change_and_save_score_record_object(game_team, record_id, record_data) elif record_type == "replacement": - self._change_and_save_replacement_record_object(extra_record_id, record_data) + self._change_and_save_replacement_record_object(record_id, record_data) - def delete_record(self, record_id: int, extra_record_id: int, record_type: str): + def delete_record(self, record_id: int, record_type: str): target_record: Record = self._record_repository.find_record_by_id(record_id) target_extra_record = None if record_type == "score": - target_extra_record = self._record_repository.find_score_record_by_id(extra_record_id) + target_extra_record = self._record_repository.find_score_record_by_record_id(record_id) game_team: GameTeam = self._game_repository.find_game_team_by_id(target_record.game_team_id) self._change_game_team_score_when_delete(game_team, target_extra_record) elif record_type == "replacement": - target_extra_record = self._record_repository.find_replacement_record_by_id(extra_record_id) + target_extra_record = self._record_repository.find_replacement_record_by_record_id(record_id) self._record_repository.delete_record(target_extra_record) self._record_repository.delete_record(target_record) @@ -55,17 +55,16 @@ def _change_and_save_record_object(self, record_data: dict, target_record: Recor target_record.recorded_quarter_id = record_data.get('recorded_quarter_id') self._record_repository.save_record(target_record) - def _change_and_save_score_record_object(self, game_team: GameTeam, extra_record_id: int, record_data: dict): - target_score_record: ScoreRecord = self._record_repository.find_score_record_by_id(extra_record_id) + def _change_and_save_score_record_object(self, game_team: GameTeam, record_id: int, record_data: dict): + target_score_record: ScoreRecord = self._record_repository.find_score_record_by_record_id(record_id) target_score_record.lineup_player_id = record_data.get('score_lineup_player_id') - score = record_data.get('score') self._change_game_team_score(game_team, target_score_record, score) target_score_record.score = score self._record_repository.save_record(target_score_record) - def _change_and_save_replacement_record_object(self, extra_record_id: int, record_data: dict): - target_replacement_record: ReplacementRecord = self._record_repository.find_replacement_record_by_id(extra_record_id) + def _change_and_save_replacement_record_object(self, record_id: int, record_data: dict): + target_replacement_record: ReplacementRecord = self._record_repository.find_replacement_record_by_record_id(record_id) target_replacement_record.origin_lineup_player_id = record_data.get('origin_lineup_player_id') target_replacement_record.replaced_lineup_player_id = record_data.get('replaced_lineup_player_id') self._record_repository.save_record(target_replacement_record) diff --git a/src/sportslive/record/domain/record.py b/src/sportslive/record/domain/record.py index 1699fa4..8ef8391 100644 --- a/src/sportslive/record/domain/record.py +++ b/src/sportslive/record/domain/record.py @@ -19,7 +19,7 @@ class Meta: class ScoreRecord(models.Model): id = models.BigAutoField(primary_key=True) - record = models.ForeignKey(Record, models.CASCADE) + record = models.OneToOneField(Record, models.CASCADE) lineup_player = models.ForeignKey(LineupPlayer, models.CASCADE) score = models.IntegerField() @@ -28,7 +28,7 @@ class Meta: class ReplacementRecord(models.Model): id = models.BigAutoField(primary_key=True) - record = models.ForeignKey(Record, models.CASCADE) + record = models.OneToOneField(Record, models.CASCADE) origin_lineup_player = models.ForeignKey(LineupPlayer, models.CASCADE) replaced_lineup_player = models.ForeignKey(LineupPlayer, models.CASCADE) diff --git a/src/sportslive/record/domain/record_repository.py b/src/sportslive/record/domain/record_repository.py index 5826721..0849800 100644 --- a/src/sportslive/record/domain/record_repository.py +++ b/src/sportslive/record/domain/record_repository.py @@ -12,11 +12,11 @@ def find_record_by_id(self, record_id: int): def find_record_by_id_with_game_team(self, record_id: int): return get_object_or_404(Record.objects.select_related('game_team'), id=record_id) - def find_score_record_by_id(self, score_record_id: int): - return get_object_or_404(ScoreRecord, id=score_record_id) + def find_score_record_by_record_id(self, record_id: int): + return get_object_or_404(ScoreRecord, record_id=record_id) - def find_replacement_record_by_id(self, replaement_record_id: int): - return get_object_or_404(ReplacementRecord, id=replaement_record_id) + def find_replacement_record_by_record_id(self, record_id: int): + return get_object_or_404(ReplacementRecord, record_id=record_id) def delete_record(self, record: Union[Record, ReplacementRecord, ScoreRecord]): return record.delete() \ No newline at end of file diff --git a/src/sportslive/record/presentation/record_change_view.py b/src/sportslive/record/presentation/record_change_view.py index a910b31..f942ef5 100644 --- a/src/sportslive/record/presentation/record_change_view.py +++ b/src/sportslive/record/presentation/record_change_view.py @@ -15,7 +15,7 @@ def __init__(self, *args, **kwargs): self._record_service = RecordContainer.record_service() @swagger_auto_schema(responses={"200": ""}) - def put(self, request, record_id: int, extra_record_id: int, record_type: str): + def put(self, request, record_id: int, record_type: str): """ 타임 라인 수정 API record type이 "score" 일 경우: @@ -37,5 +37,5 @@ def put(self, request, record_id: int, extra_record_id: int, record_type: str): "replacedLineupPlayerId": int } """ - self._record_service.change_record(record_id, extra_record_id, record_type, request.data) + self._record_service.change_record(record_id, record_type, request.data) return Response(status=status.HTTP_200_OK) \ No newline at end of file diff --git a/src/sportslive/record/presentation/record_create_view.py b/src/sportslive/record/presentation/record_create_view.py index 18f9db3..00d69a4 100644 --- a/src/sportslive/record/presentation/record_create_view.py +++ b/src/sportslive/record/presentation/record_create_view.py @@ -25,7 +25,8 @@ def post(self, request, game_id: int, record_type: str): "recordedQuarterId": int, "scoreLineupPlayerId": int, "recordedQuarterId": int, - "score": int + "score": int, + "recordedAt": datetime } record type이 "replacement" 일 경우: { @@ -33,7 +34,8 @@ def post(self, request, game_id: int, record_type: str): "recordedQuarterId": int, "originLineupPlayerId": int, "recordedQuarterId": int, - "replacedLineupPlayerId": int + "replacedLineupPlayerId": int, + "recordedAt": datetime } """ self._record_service.create_record(game_id, record_type, request.data) diff --git a/src/sportslive/record/presentation/record_delete_view.py b/src/sportslive/record/presentation/record_delete_view.py index b17a679..3148691 100644 --- a/src/sportslive/record/presentation/record_delete_view.py +++ b/src/sportslive/record/presentation/record_delete_view.py @@ -15,9 +15,9 @@ def __init__(self, *args, **kwargs): self._record_service = RecordContainer.record_service() @swagger_auto_schema(responses={"204": ""}) - def put(self, request, record_id: int, extra_record_id: int, record_type: str): + def delete(self, request, record_id: int, record_type: str): """ 타임 라인 삭제 API """ - self._record_service.delete_record(record_id, extra_record_id, record_type) + self._record_service.delete_record(record_id, record_type) return Response(status=status.HTTP_204_NO_CONTENT) \ No newline at end of file diff --git a/src/sportslive/record/urls.py b/src/sportslive/record/urls.py index dc774b6..c3174a4 100644 --- a/src/sportslive/record/urls.py +++ b/src/sportslive/record/urls.py @@ -9,6 +9,6 @@ urlpatterns = [ path('create///', RecordCreateView.as_view()), - path('change////', RecordChangeView.as_view()), - path('delete////', RecordDeleteView.as_view()), + path('change///', RecordChangeView.as_view()), + path('delete///', RecordDeleteView.as_view()), ] \ No newline at end of file diff --git a/src/tests/test_record.py b/src/tests/test_record.py index aca85ad..914d51b 100644 --- a/src/tests/test_record.py +++ b/src/tests/test_record.py @@ -63,7 +63,7 @@ def test_change_score_record1(self, load_sql_fixture, dependency_fixture): "scoreLineupPlayerId": 9, "score": 1 } - self._record_service.change_record(1, 1, 'score', request_data) + self._record_service.change_record(1, 'score', request_data) assert Record.objects.get(id=1).recorded_at == 2 assert Record.objects.get(id=1).recorded_quarter_id == 2 @@ -81,11 +81,11 @@ def test_change_score_record2(self, load_sql_fixture, dependency_fixture): "scoreLineupPlayerId": 10, "score": 2 } - self._record_service.change_record(1, 2, 'score', request_data) + self._record_service.change_record(1, 'score', request_data) assert Record.objects.get(id=1).recorded_at == 2 assert Record.objects.get(id=1).recorded_quarter_id == 2 - assert ScoreRecord.objects.get(id=2).lineup_player_id == 10 + assert ScoreRecord.objects.get(id=1).lineup_player_id == 10 assert GameTeam.objects.get(id=5).score == 3 @pytest.mark.django_db @@ -101,7 +101,7 @@ def test_change_replacement_record(self, load_sql_fixture, dependency_fixture): "recordedQuarterId": 2, "replacedLineupPlayerId": 11 } - self._record_service.change_record(3, 1, 'replacement', request_data) + self._record_service.change_record(3, 'replacement', request_data) assert ReplacementRecord.objects.get(id=1).origin_lineup_player_id == 12 assert ReplacementRecord.objects.get(id=1).replaced_lineup_player_id == 11 @@ -111,8 +111,8 @@ def test_delete_record(self, load_sql_fixture, dependency_fixture): """ score 타임라인 하나를 삭제한다 """ - self._record_service.delete_record(1, 1, 'score') - self._record_service.delete_record(2, 2, 'score') + self._record_service.delete_record(1, 'score') + self._record_service.delete_record(2, 'score') assert Record.objects.filter(id=1).exists() == False assert ScoreRecord.objects.filter(id=1).exists() == False