Skip to content

Commit

Permalink
Merge pull request #123 from hufscheer/refactor/all
Browse files Browse the repository at this point in the history
[REFACTOR] 타임라인 생성/수정 전체적인 리팩토링
  • Loading branch information
leehjhjhj authored Mar 20, 2024
2 parents 8a9278f + 6807be5 commit 0587812
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/sportslive/game/presentation/game_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
return Response(status=status.HTTP_201_CREATED)
6 changes: 3 additions & 3 deletions src/sportslive/record/application/record_create_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 10 additions & 11 deletions src/sportslive/record/application/record_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/sportslive/record/domain/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions src/sportslive/record/domain/record_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 2 additions & 2 deletions src/sportslive/record/presentation/record_change_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" 일 경우:
Expand All @@ -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)
6 changes: 4 additions & 2 deletions src/sportslive/record/presentation/record_create_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ 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" 일 경우:
{
"gameTeamId": int,
"recordedQuarterId": int,
"originLineupPlayerId": int,
"recordedQuarterId": int,
"replacedLineupPlayerId": int
"replacedLineupPlayerId": int,
"recordedAt": datetime
}
"""
self._record_service.create_record(game_id, record_type, request.data)
Expand Down
4 changes: 2 additions & 2 deletions src/sportslive/record/presentation/record_delete_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/sportslive/record/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

urlpatterns = [
path('create/<str:record_type>/<int:game_id>/', RecordCreateView.as_view()),
path('change/<int:record_id>/<int:extra_record_id>/<str:record_type>/', RecordChangeView.as_view()),
path('delete/<str:record_type>/<int:extra_record_id>/<int:game_id>/', RecordDeleteView.as_view()),
path('change/<int:record_id>/<str:record_type>/', RecordChangeView.as_view()),
path('delete/<int:record_id>/<str:record_type>/', RecordDeleteView.as_view()),
]
12 changes: 6 additions & 6 deletions src/tests/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0587812

Please sign in to comment.