Coverage for src/app/adapters/template_postgres_adapter.py: 40%
35 statements
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-03 00:51 +0200
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-03 00:51 +0200
1from repositories.template_postgres_repository import TemplatePostgresRepository
2from models.template_model import TemplateModel
3from ports.get_template_port import GetTemplatePort
4from ports.get_template_list_port import GetTemplateListPort
5from ports.save_template_port import SaveTemplatePort
6from ports.delete_template_port import DeleteTemplatePort
8from entities.template_entity import TemplateEntity
11class TemplatePostgresAdapter(GetTemplatePort, GetTemplateListPort, DeleteTemplatePort, SaveTemplatePort):
13 def __init__(self, template_postgres_repository: TemplatePostgresRepository):
14 self.template_postgres_repository = template_postgres_repository
16 def get_template(self, template: TemplateModel) -> TemplateModel:
17 """
18 Retrieve a template by its details.
19 Args:
20 template (TemplateModel): The template details to retrieve.
21 Returns:
22 TemplateModel: The retrieved template.
23 """
24 try:
26 template_entity = TemplateEntity(
27 id=template.get_id(),
28 question=template.get_question(),
29 answer=template.get_answer(),
30 author_id=template.get_author_id(),
31 last_modified=template.get_last_modified()
32 )
34 template = self.template_postgres_repository.get_template(template_entity)
36 return TemplateModel(
37 id=template.get_id(),
38 question=template.get_question(),
39 answer=template.get_answer(),
40 author_id=template.get_author_id(),
41 last_modified=template.get_last_modified()
42 )
44 except Exception as e:
45 raise e
48 def get_template_list(self) -> list[TemplateModel]:
49 """
50 Retrieve all templates.
51 Returns:
52 list[TemplateModel]: A list of TemplateModel objects.
53 """
54 try:
56 templates = self.template_postgres_repository.get_template_list()
58 return [
59 TemplateModel(
60 id=template.get_id(),
61 question=template.get_question(),
62 answer=template.get_answer(),
63 author_id=template.get_author_id(),
64 last_modified=template.get_last_modified()
65 )
66 for template in templates
67 ]
69 except Exception as e:
70 raise e
72 def save_template(self, template: TemplateModel) -> int:
73 """
74 Save a template.
75 Args:
76 template (TemplateModel): The template to save.
77 Returns:
78 int: The ID of the saved template.
79 """
80 try:
82 template_entity = TemplateEntity(
83 id=template.get_id(),
84 question=template.get_question(),
85 answer=template.get_answer(),
86 author_id=template.get_author_id(),
87 last_modified=template.get_last_modified()
88 )
90 return self.template_postgres_repository.save_template(template_entity)
92 except Exception as e:
93 raise e
96 def delete_template(self, template: TemplateModel) -> bool:
97 """
98 Delete a template.
99 Args:
100 template (TemplateModel): The template to delete.
101 Returns:
102 bool: True if the template was deleted successfully, otherwise False.
103 """
104 try:
106 template_entity = TemplateEntity(
107 id=template.get_id(),
108 question=template.get_question(),
109 answer=template.get_answer(),
110 author_id=template.get_author_id(),
111 last_modified=template.get_last_modified()
112 )
114 return self.template_postgres_repository.delete_template(template_entity)
116 except Exception as e:
117 raise e