From 92f21b5159996c403ff6c3b62f5fdaa9dfd837b9 Mon Sep 17 00:00:00 2001 From: sm4640 Date: Mon, 28 Apr 2025 21:29:51 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20[#32]=20project=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20=EB=B3=80=ED=99=94=20=ED=95=A8=EC=88=98(=EC=9D=B4?= =?UTF-8?q?=EC=A0=84=20=EC=B2=B4=ED=81=AC,=20=EC=9C=A0=EC=A0=80/=EA=B0=9C?= =?UTF-8?q?=EC=88=98=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projects/services.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 projects/services.py diff --git a/projects/services.py b/projects/services.py new file mode 100644 index 0000000..50b9a64 --- /dev/null +++ b/projects/services.py @@ -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])