The goal of this project is to create a backend tool that syncs contacts from MockAPI to Mailchimp.
- Retrieve contact data (first name, last name, and email) from MockAPI.
- Add these contacts as members of a new list in Mailchimp.
- Name the Mailchimp list with the developer’s personal name.
- Provide a RESTful endpoint
/contacts/syncto sync and return the total number of synced contacts and their details
- The project is built following Clean Architecture principles:
- Separation of Concerns: Each layer (Presentation, Application, Domain, Infrastructure) has a clear responsibility.
- SOLID Principles: Ensures flexibility, maintainability, and testability.
- Dependency Inversion: Interfaces abstract dependencies, decoupling implementations from higher-level business logic.
- Manages RESTful endpoints.
- Main Endpoint:
GET /contacts/syncto initiate the sync process. - Key Files:
ContactsController.cs.
- Orquestrade the domain business logic for synchronization.
- Service:
ContactSyncService- Fetches data from Domain.
- Process use cases and validates the data.
- Return information to presentation.
- Key Files:
ContactSyncService.cs.
- Defines core entities such as
Contact,SyncResult. - Own infrastrucuture interfaces like
IContactService,IMailService. - Invokes the infrastructure layer to get/send data to external services.
- Key Files:
IContactService.cs,IMailService.cs,Contact.cs
- Implements communication with external APIs (MockAPI and Mailchimp).
- Uses
HttpClientto handle requests. - Specific clients:
MailchimpClient,MockApiClient. - Key Files:
MailchimpClient.cs, andMockApiClient.cs
-
Use of DTOs (Data Transfer Objects):
- DTOs such as
ContactDTOandSyncResponseDTOare used to isolate the data transferred between layers and the API response.
- DTOs such as
-
Dependency Injection (DI):
- Configured in the
DependencyInjection.csfile to manage dependencies likeIMailchimpClientandIContactSyncService.
- Configured in the
-
Modularity and Flexibility:
- The infrastructure layer is designed to handle external dependencies (e.g., Mailchimp or databases). Interfaces ensure that changing or extending these dependencies (e.g., swapping Mailchimp for another service) requires minimal code changes.
-
Testability:
- This architecture allows each layer to be tested independently, ensuring that functionalities are validated in isolation without dependencies on other layers.
- Mocks (
MockApiClient) and fakers (ContactDtoFaker) simulate real scenarios in unit tests, enabling isolated and effective testing of each layer.
-
Error Handling:
- Encapsulated in the
BaseHttpClientto standardize responses and handle failures when communicating with external APIs.
- Encapsulated in the
-
Request to the Endpoint:
- A client sends a
POSTrequest to the/contacts/syncendpoint.
- A client sends a
-
Fetching Data from MockAPI:
- The application calls
MockApiClientto fetch contact data. - The response is mapped into
ContactDTO.
- The application calls
-
Creating Members in Mailchimp:
- For each contact retrieved,
MailchimpClientsends a request to add the contact to a specific list.
- For each contact retrieved,
-
Building the Response:
- The
ContactSyncServicereturns the total number of synced contacts and their details in JSON format.
- The
{
"syncedContacts": 3,
"contacts": [
{
"firstName": "John",
"lastName": "Doe",
"email": "[email protected]"
},
{
"firstName": "Jane",
"lastName": "Smith",
"email": "[email protected]"
}
]
}-
Presentation Layer:
ContactsController.cs: Defines the/contacts/syncendpoint. Responsible for handling requests from clients and initiating the synchronization process through the application layer.
-
Application Layer:
ContactSyncService.cs: Contains the main business logic for synchronizing contacts between MockAPI and Mailchimp. Handles data validation and coordinates operations with the domain and infrastructure layers.
-
Infrastructure Layer:
MailchimpClient.cs: Implements the communication with the Mailchimp API to create and manage mailing lists and contacts.MockApiClient.cs: Fetches contacts from the MockAPI, translating their structure into usable DTOs for the application layer.
- Represents a contact in the domain model.
- Fields:
FirstName(string): The first name of the contact.LastName(string): The last name of the contact.Email(string): The email address of the contact.
- Used to transfer contact data between layers without exposing the domain entity directly.
- Fields:
FirstName(string)LastName(string)Email(string)
- Represents the response structure for the
/contacts/syncendpoint. - Fields:
SyncedContacts(int): The total number of contacts successfully synchronized.Contacts(list ofContactDTO): The details of all synchronized contacts.
- The project DEMO is hosted on a free cloud platform for accessibility.
- The API is documented using
scalar.com, atool with a UI similar to Postman or Swagger, allowing for easy endpoint testing and interaction.
-
Synchronize Contacts
- URL:
/api/contacts/sync - Method:
POST - Description: Synchronizes contacts from MockAPI to Mailchimp and returns the total number of synchronized contacts along with their details.
- Response Codes:
200 OK: Operation succeeded, and data was synchronized.204 No Content: No contacts were found to synchronize.
- URL:
-
Get Contacts
- URL:
/api/contacts/get - Method:
GET - Description: Retrieves all contacts currently synchronized.
- Response Codes:
200 OK: Operation succeeded, and data was retrieved.204 No Content: No contacts were found.
- URL:
-
Clean Contacts
- URL:
/api/contacts/clean - Method:
DELETE - Description: Deletes all synchronized contacts and returns the operation result.
- Response Codes:
200 OK: Operation succeeded, and contacts were deleted.204 No Content: No contacts were found to delete.
- URL:
- A recorded video that provides an overview of how the software works and explains key aspects of the code, including its architecture, functionality, and design decisions: https://www.loom.com/share/0e5c91e7f1df452dab100b6125cb7b47
- Implement authentication and authorization to ensure secure and authorized access to the system.
- Consider adding centralized logging to monitor failures in external integrations.
- Explore additional test coverage to handle edge cases or integration-level testing.