This repository contains the Homeward API service, built using Django and Django Rest Framework. Follow the steps below to clone the repository, set up the environment, and run the tests.
Ensure you have the following installed on your system:
- Python 3.8+
- pip (Python package manager)
- Virtualenv
- Git
- Open your terminal or command prompt.
- Clone the repository:
git clone git@github.com:marcellhenrique/homeward.git
- Navigate to the project directory:
cd homeward
- Create a virtual environment:
python -m venv env
- Activate the virtual environment:
- On macOS/Linux:
source env/bin/activate - On Windows:
.\env\Scripts\activate
- On macOS/Linux:
- Install the dependencies:
pip install -r requirements.txt
- Ensure the virtual environment is activated.
- Run the tests using the Django test runner:
python manage.py test - All tests should pass if the setup was successful.
In this project, the Service Pattern is utilized to manage the application's business logic. The pattern is designed to decouple complex business logic from views and models, making the code more modular, testable, and maintainable.
The Service Pattern is appropriate for this project due to the following reasons:
- Separation of Concerns: It isolates business logic from Django views and models, making it easier to manage, test, and modify.
- Single Responsibility: Each service has a single responsibility and does not need to manage how the data is presented or stored.
- Reusability: Services can be reused across different views and projects, promoting code reuse and reducing duplication.
- Testability: By isolating the business logic into services, the code is easier to unit test without being tightly coupled to other layers like the database or web framework.
In the Service Pattern, the logic of the application is split between views, models, and services. The service layer handles the core business logic, including interactions with external APIs and other services.
The service pattern is implemented with the following structure:
The AgentService is responsible for managing the business logic related to real estate agents. It provides methods to fetch agent details associated with a specific customer and to retrieve a list of agents based on their location.
This method retrieves the details of an agent associated with a given customer ID.
customer_id(int): The ID of the customer whose associated agent information needs to be fetched.
- A dictionary containing the customer and agent details if found, or
Noneif the customer or agent is not found.
{
"customer": {
"id": 1,
"name": "Cynthia Hart",
"email": "c.hart@sample.com",
"phone": "111-123-1234",
"current_address": "908 Test St. Austin, TX 78749"
},
"agent": {
"name": "Dana Agent",
"contact_info": "dana.agent@example.com",
"brokerage_name": "Super Realty",
"location": "Austin, TX"
}
}This method retrieves a list of agents based on the provided location.
location(str): A string containing the location (city/state) to filter the agents by.
- A list of dictionaries containing details of agents located in the specified location.
{
"name": "Dana Agent",
"contact_info": "dana.agent@example.com",
"brokerage_name": "Super Realty",
"location": "Austin, TX"
},
{
"name": "Lawrence Agent",
"contact_info": "lawrence.agent@example.com",
"brokerage_name": "Dream Homes Realty",
"location": "Austin, TX"
}The Agent model represents a real estate agent in the system. It stores essential details about the agent, such as their name, contact information, brokerage name, and location.
- Type:
CharField - Max Length: 255 characters
- Description: The full name of the real estate agent.
- Example:
"Dana Agent"
- Type:
CharField - Max Length: 255 characters
- Nullable: Yes
- Blank: Yes
- Description: The contact information of the real estate agent (e.g., phone number, email). This field is optional.
- Example:
"dana.agent@example.com"
- Type:
CharField - Max Length: 255 characters
- Nullable: Yes
- Blank: Yes
- Description: The name of the brokerage that the agent is associated with. This field is optional.
- Example:
"Super Realty"
- Type:
CharField - Max Length: 255 characters
- Nullable: Yes
- Blank: Yes
- Description: The location (city, state) where the agent operates. This field is optional.
- Example:
"Austin, TX"
This method defines how the Agent object will be represented as a string. It returns a string combining the agent's name and brokerage name.
This section provides the documentation for the views in the project. These views interact with the AgentService to provide data related to customers and agents.
- URL:
/customer-agent/<customer_id>/ - Method:
GET - Description: Fetches the details of the real estate agent associated with a specific customer, based on the
customer_id. - Parameters:
customer_id: The ID of the customer. This is a required URL parameter.
- 200 OK: If the customer and agent are found, returns a JSON object with the customer and agent details.
- 404 Not Found: If either the customer or agent is not found, returns a "Customer or agent not found." error message.
GET /customer-agent/1/- URL:
/agents/ - Method:
GET - Description: Fetches a list of real estate agents filtered by location (city or state). This view expects a query parameter to specify the location.
- Parameters:
- queryparams:
location: The location (city or state) to filter the agents by. This is a required query parameter.
- queryparams:
- 200 OK: Returns a list of agents matching the provided location.
- 400 Bad Request: If the location query parameter is not provided, returns an error message indicating that the location parameter is required.
GET /agents-by-location/?location=Austin