Last active
June 27, 2021 01:06
-
-
Save kauplan/5d1ba11a53e13ddb5f4dfc5c2f395021 to your computer and use it in GitHub Desktop.
ログインユーザ情報をユースケースクラスとビジネスロジッククラスで引き回す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
## | |
## ビジネスロジック | |
## | |
class BusinessLogic: # 「Service」や「Operation」でもよい | |
def __init__(self, login_user=None): | |
self.login_user = login_user | |
class BugReportBL(BusinessLogic): | |
def バグレポートを作成(self, title, text): | |
sql = "insert into bug_reports (title, text, reporter_id) values (?, ?, ?)" | |
reporter = self.login_user | |
args = [title, text, reporter.id] | |
DB.execute(sql, args) | |
Logging.debug(f"login_user={self.login_user.name}") #← ログ出力!!! | |
bugreport = BugReport(title, text, reporter) | |
return bugreport | |
def バグレポートをクローズ(self, ...): | |
... | |
def バグレポートに担当者をアサイン(self, ...): | |
... | |
## | |
## ユースケース | |
## | |
class UseCase: # 「API」でもよい | |
def __init__(self, login_user=None): | |
self.login_user = login_user | |
def perform(self, params, *args, **kwargs): | |
errors = self.validate(params) # バリデーション | |
if errors: | |
return handle_validation_error(errors) | |
context = self.execute(**args, **kwargts) # ビジネスロジックを呼び出す | |
resp_body = self.represent(context) # JSONやHTMLを生成 | |
return resp_body | |
def validate(params): | |
raise NotImplementedError | |
def execute(*args, **kwargs): | |
raise NotImplementedError | |
def represent(context): | |
raise NotImplementedError | |
def handle_validation_error(self, errors): | |
raise ValidationError(errors) | |
class CreateBugReportUC(UseCase): | |
def validate(self, params): # バリデーション | |
errors = {} | |
if not params.get('title'): | |
errors['title'] = "Required." | |
if not params.get('text'): | |
errors['text'] = "Required." | |
return errors | |
def perform(self, title, text): # ビジネスロジックを呼び出す | |
bl = BugReportBL(self.login_user) | |
bugreport = bl.バグレポートを作成(title, text) | |
Logging.debug(f"login_user={self.login_user.name}") #← ログ出力!!! | |
return {'bugreport': bugreport} | |
def represent(self, context): # JSONやHTMLを生成する | |
x = context['bugreport'] | |
json_data = { | |
"bugreport": { | |
"id": x.id, | |
"title": x.title, | |
"text": x.text, | |
"reporter": { | |
"id": x.reporter.id, | |
"name": x.reporter.name, | |
}, | |
}, | |
} | |
return JSON.encode(json_data) | |
## | |
## コントローラ | |
## | |
class BugReportController(Framework.Controller): | |
def on_get(self): | |
... | |
def on_post(self): | |
uc = CreateBugReportUC(self.get_login_user()) # ユースケース作成時にログインユーザを渡す | |
params = self.request.params | |
args = [params.get('title'), params.get('text')] | |
resp_body = uc.perform(params, *args) # ユースケースを実行 | |
return resp_body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment