Back-End
Introduction
This document describes the backend organization following a diamond architecture. This structure provides clear separation of responsibilities and better code maintainability.
Project structure
The project is organized into several main folders:
1. domain/
This folder contains the application business logic, divided into two parts:
gateways/: Defines interfaces that describe interactions with underlying infrastructure.usecases/: Contains use cases that encapsulate business logic.
Key files:
tags.gateway.ts: Interface defining methods to manipulate tags.createNewTag.usecase.ts: Use case for creating a tag.deleteTag.usecase.ts: Use case for deleting a tag.getListTags.usecase.ts: Use case to get a list of tags.getTags.usecase.ts: Use case to retrieve a specific tag.tag.ts: Defines the tag business entity.
2. infra/
This folder contains concrete implementations of the interfaces defined in domain/ and manages external interactions.
Subfolders:
-
controllers/: Handles incoming requests and invokes appropriate use cases.tags.controller.ts: Controller for tag management.dto/: Contains Data Transfer Objects used in requests and responses.create-tag.dto.ts: DTO for creating a tag.
-
gateways/: Contains implementations of the interfaces defined indomain/gateways/.entities/: Defines data models and their interactions with the database.tag.entity.ts: Data model representing a tag.
tag.gateway.ts: Concrete implementation of the tag gateway.
3. tags.module.ts
This file groups and exports the different tag-related elements as a module to be used in the application.

Example: adding an update use case
To add a new use case to modify a tag, follow these steps:
- Add the use case in
domain/usecases/: createupdateTag.usecase.tsand implement the tag update logic. - Modify the gateway in
domain/gateways/: add anupdateTagmethod totags.gateway.tsto define the update interface. - Implement the update in
infra/gateways/: add theupdateTagimplementation intag.gateway.tsto interact with the database. - Add a DTO in
infra/controllers/dto/: createupdate-tag.dto.tsto define the expected input for the update. - Modify the controller in
infra/controllers/: add a newPUT /tags/:idendpoint intags.controller.tsthat receives the DTO and calls the update use case. - Update the module: register the new use case in
tags.module.tsand make it available to the application.
Conclusion
The diamond architecture enforces a strict separation between business logic (domain) and infrastructure (infra).
- Use cases manipulate entities and abstract gateways only.
- Concrete implementations live in
infra. - Controllers orchestrate interactions between the API and business logic.