Feat: [#32] project 상태 변화 함수(이전 체크, 유저/개수 업데이트)

This commit is contained in:
sm4640
2025-04-28 21:29:51 +09:00
parent 509d51bde6
commit 92f21b5159

40
projects/services.py Normal file
View File

@@ -0,0 +1,40 @@
from .models import Project
ACTION_FIELD_MAP = {
'like': 'likers',
'scrap': 'scrappers',
}
class ProjectBeforeRelCheckService:
@staticmethod
def check_user_project_rel(action_type: str, project: Project, user):
action = ACTION_FIELD_MAP.get(action_type)
if not action:
raise ValueError(f"'{action_type}' is unsupported action type")
if getattr(project, action).filter(id=user.id).exists():
return True
else:
return False
class ProjectStateChangeService:
@staticmethod
def change_user_project_rel(action_type: str, project: Project, user, add=True):
action = ACTION_FIELD_MAP.get(action_type)
if not action:
raise ValueError(f"'{action_type}' is unsupported action type")
user_method = getattr(getattr(project, action), 'add' if add else 'remove')
user_method(user)
@staticmethod
def change_count_project_state(action_type: str, project: Project, add=True):
action = ACTION_FIELD_MAP.get(action_type)
if not action:
raise ValueError(f"'{action_type}' is unsupported action type")
field_name = action_type + '_count'
current_count = getattr(project, field_name)
now_count = current_count+1 if add else max(current_count-1, 0)
setattr(project, field_name, now_count)
project.save(update_fields=[field_name])