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

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 

7 

8from entities.template_entity import TemplateEntity 

9 

10 

11class TemplatePostgresAdapter(GetTemplatePort, GetTemplateListPort, DeleteTemplatePort, SaveTemplatePort): 

12 

13 def __init__(self, template_postgres_repository: TemplatePostgresRepository): 

14 self.template_postgres_repository = template_postgres_repository 

15 

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: 

25 

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 ) 

33 

34 template = self.template_postgres_repository.get_template(template_entity) 

35 

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 ) 

43 

44 except Exception as e: 

45 raise e 

46 

47 

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: 

55 

56 templates = self.template_postgres_repository.get_template_list() 

57 

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 ] 

68 

69 except Exception as e: 

70 raise e 

71 

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: 

81 

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 ) 

89 

90 return self.template_postgres_repository.save_template(template_entity) 

91 

92 except Exception as e: 

93 raise e 

94 

95 

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: 

105 

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 ) 

113 

114 return self.template_postgres_repository.delete_template(template_entity) 

115 

116 except Exception as e: 

117 raise e 

118