Skip to content

Commit 342c7d9

Browse files
added calculations and postgres
1 parent 20cac6a commit 342c7d9

14 files changed

Lines changed: 155 additions & 19 deletions

mortgage_calculator/app/crud/mortgage_crud.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ def create_mortgage_crud(
1010
payload: schemas.MortgageCreateModel, db: Session = Depends(get_db)
1111
):
1212
try:
13+
# Debug print the payload
14+
print(f"Payload: {payload}")
15+
16+
# Create a new mortgage object from the payload
1317
new_mortgage = models.MortgageOrm(**payload.model_dump())
1418

19+
# Debug print the new mortgage object
20+
print(f"New Mortgage: {new_mortgage}")
21+
1522
# Check whether the property exists in the DB
1623
property_data = (
1724
db.query(models.PropertyOrm)
@@ -24,12 +31,25 @@ def create_mortgage_crud(
2431
detail="Property not found.",
2532
)
2633

34+
# Get Purchase Price of the property and calculate mortgage amount (LTV * Purchase Price)
35+
purchase_price = float(property_data.purchase_price) # Convert to float
36+
loan_to_value = float(new_mortgage.loan_to_value) # Ensure this is float
37+
mortgage_amount = (purchase_price * loan_to_value) / 100
38+
new_mortgage.mortgage_amount = mortgage_amount
39+
40+
# Debug print the mortgage amount
41+
print(f"Calculated Mortgage Amount: {mortgage_amount}")
42+
2743
db.add(new_mortgage)
2844
db.commit()
2945
db.refresh(new_mortgage)
3046

47+
# Validate the new mortgage object
3148
mortgage_data = schemas.MortgageModel.model_validate(new_mortgage)
3249

50+
# Debug print the validated mortgage data
51+
print(f"Validated Mortgage Data: {mortgage_data}")
52+
3353
return schemas.MortgageResponseModel(
3454
status=schemas.Status.Success,
3555
message="Mortgage created successfully.",

mortgage_calculator/app/crud/property_crud.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def create_property_crud(
1111
):
1212
try:
1313
new_property = models.PropertyOrm(**payload.model_dump())
14+
print(new_property.admin_costs)
1415
db.add(new_property)
1516
db.commit()
1617
db.refresh(new_property)

mortgage_calculator/app/custom/calculations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def calculate_interest_only_payment(
99
:return: Monthly interest payment.
1010
"""
1111
monthly_interest_rate = annual_interest_rate / 100 / 12
12-
return loan_amount * monthly_interest_rate
12+
return round(loan_amount * monthly_interest_rate, 2)
1313

1414

1515
def calculate_repayment_mortgage_payment(
@@ -32,4 +32,4 @@ def calculate_repayment_mortgage_payment(
3232
* (monthly_interest_rate * (1 + monthly_interest_rate) ** total_payments)
3333
/ ((1 + monthly_interest_rate) ** total_payments - 1)
3434
)
35-
return monthly_payment
35+
return round(monthly_payment, 2)

mortgage_calculator/app/custom/db_queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ def get_mortgage_payment(mortgage_id: str, db: Session):
2626

2727
if mortgage.mortgage_type == MortgageType.interest_only.value:
2828
monthly_payment = calculate_interest_only_payment(
29-
property.purchase_price, mortgage.interest_rate
29+
mortgage.mortgage_amount, mortgage.interest_rate
3030
)
3131
elif mortgage.mortgage_type == MortgageType.repayment.value:
3232
# Assuming a fixed loan term, e.g., 30 years. This could also be dynamically fetched or adjusted.
3333
monthly_payment = calculate_repayment_mortgage_payment(
34-
property.purchase_price, mortgage.interest_rate, 30
34+
mortgage.mortgage_amount, mortgage.interest_rate, mortgage.loan_term
3535
)
3636
else:
3737
raise HTTPException(

mortgage_calculator/app/database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from sqlalchemy import create_engine
22
from sqlalchemy.orm import sessionmaker, declarative_base
33

4-
SQLITE_DATABASE_URL = "sqlite:///./mortgage.db"
4+
# SQLITE_DATABASE_URL = "sqlite:///./mortgage.db"
5+
SQLALCHEMY_DATABASE_URL = "postgresql://myuser:mypassword@localhost/mydatabase"
56

67
engine = create_engine(
7-
SQLITE_DATABASE_URL, echo=True, connect_args={"check_same_thread": False}
8+
SQLALCHEMY_DATABASE_URL,
89
)
910
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
1011

mortgage_calculator/app/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MortgageOrm(Base):
4040
interest_rate = Column(Numeric(5, 2), nullable=False)
4141
mortgage_type = Column(SQLAlchemyEnum(MortgageType), nullable=False)
4242
loan_term = Column(Numeric(5, 2), nullable=True)
43+
mortgage_amount = Column(Numeric(10, 2), nullable=False)
4344

4445
property = relationship("PropertyOrm", back_populates="mortgages")
4546

mortgage_calculator/app/routes.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import app.schemas as schemas
1818
from sqlalchemy.orm import Session
1919
from app.database import get_db
20+
from typing import Dict
2021

2122
router = APIRouter()
2223

@@ -28,7 +29,7 @@
2829
)
2930
def create_property(
3031
property: schemas.PropertyCreateModel, db: Session = Depends(get_db)
31-
):
32+
) -> schemas.PropertyResponseModel:
3233
"""
3334
Create a new property.
3435
"""
@@ -40,7 +41,9 @@ def create_property(
4041
status_code=status.HTTP_200_OK,
4142
response_model=schemas.PropertyResponseModel,
4243
)
43-
def get_property(property_id: str, db: Session = Depends(get_db)):
44+
def get_property(
45+
property_id: str, db: Session = Depends(get_db)
46+
) -> schemas.PropertyResponseModel:
4447
"""
4548
Get a property by ID.
4649
"""
@@ -56,7 +59,7 @@ def update_property(
5659
property_id: str,
5760
property: schemas.PropertyUpdateModel,
5861
db: Session = Depends(get_db),
59-
):
62+
) -> schemas.PropertyResponseModel:
6063
"""
6164
Update a property by ID.
6265
"""
@@ -68,7 +71,9 @@ def update_property(
6871
status_code=status.HTTP_202_ACCEPTED,
6972
response_model=schemas.PropertyDeleteModel,
7073
)
71-
def delete_property(property_id: str, db: Session = Depends(get_db)):
74+
def delete_property(
75+
property_id: str, db: Session = Depends(get_db)
76+
) -> schemas.PropertyDeleteModel:
7277
"""
7378
Delete a property by ID.
7479
"""
@@ -80,7 +85,7 @@ def delete_property(property_id: str, db: Session = Depends(get_db)):
8085
status_code=status.HTTP_200_OK,
8186
response_model=schemas.PropertyListResponseModel,
8287
)
83-
def get_properties(db: Session = Depends(get_db)):
88+
def get_properties(db: Session = Depends(get_db)) -> schemas.PropertyListResponseModel:
8489
"""
8590
Get all properties.
8691
"""
@@ -94,7 +99,7 @@ def get_properties(db: Session = Depends(get_db)):
9499
)
95100
def create_mortgage(
96101
mortgage: schemas.MortgageCreateModel, db: Session = Depends(get_db)
97-
):
102+
) -> schemas.MortgageResponseModel:
98103
"""
99104
Create a new mortgage.
100105
"""
@@ -106,7 +111,9 @@ def create_mortgage(
106111
status_code=status.HTTP_200_OK,
107112
response_model=schemas.MortgageResponseModel,
108113
)
109-
def get_mortgage(mortgage_id: str, db: Session = Depends(get_db)):
114+
def get_mortgage(
115+
mortgage_id: str, db: Session = Depends(get_db)
116+
) -> schemas.MortgageResponseModel:
110117
"""
111118
Get a mortgage by ID.
112119
"""
@@ -122,7 +129,7 @@ def update_mortgage(
122129
mortgage_id: str,
123130
mortgage: schemas.MortgageUpdateModel,
124131
db: Session = Depends(get_db),
125-
):
132+
) -> schemas.MortgageResponseModel:
126133
"""
127134
Update a mortgage by ID.
128135
"""
@@ -134,7 +141,9 @@ def update_mortgage(
134141
status_code=status.HTTP_202_ACCEPTED,
135142
response_model=schemas.MortgageDeleteModel,
136143
)
137-
def delete_mortgage(mortgage_id: str, db: Session = Depends(get_db)):
144+
def delete_mortgage(
145+
mortgage_id: str, db: Session = Depends(get_db)
146+
) -> schemas.MortgageDeleteModel:
138147
"""
139148
Delete a mortgage by ID.
140149
"""
@@ -146,15 +155,15 @@ def delete_mortgage(mortgage_id: str, db: Session = Depends(get_db)):
146155
status_code=status.HTTP_200_OK,
147156
response_model=schemas.MortgageListResponseModel,
148157
)
149-
def get_mortgages(db: Session = Depends(get_db)):
158+
def get_mortgages(db: Session = Depends(get_db)) -> schemas.MortgageListResponseModel:
150159
"""
151160
Get all mortgages.
152161
"""
153162
return get_mortgages_crud(db=db)
154163

155164

156-
@router.post("/mortgage/{mortgage_id}/payment", response_model=dict)
157-
def get_mortgage_interest_payment(mortgage_id: str, db: Session = Depends(get_db)):
165+
@router.post("/mortgage/{mortgage_id}/payment", response_model=Dict)
166+
def calculate_mortgage_payment(mortgage_id: str, db: Session = Depends(get_db)) -> Dict:
158167
"""
159168
Retrieve the monthly payment for a given mortgage.
160169
"""

mortgage_calculator/app/schemas.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class PropertyCreateModel(PropertyBaseModel):
6363
rental_income: float
6464
renovation_cost: float
6565
property_name: str
66+
admin_costs: float
67+
management_fees: float
6668

6769

6870
class PropertyUpdateModel(PropertyBaseModel):
@@ -125,13 +127,21 @@ class MortgageBaseModel(BaseModel):
125127
"description": "Loan term in years",
126128
},
127129
)
130+
mortgage_amount: Optional[float] = Field(
131+
default=None,
132+
json_schema_extra={
133+
"example": 225000.00,
134+
"description": "Amount of the mortgage. Calculated based on loan to value and purchase price.",
135+
},
136+
)
128137

129138
model_config = ConfigDict(from_attributes=True)
130139

131140

132141
class MortgageCreateModel(MortgageBaseModel):
133142
loan_to_value: float
134143
interest_rate: float
144+
loan_term: int
135145
mortgage_type: MortgageType
136146
property_id: UUID
137147

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3.8'
2+
3+
services:
4+
db:
5+
image: postgres:latest
6+
container_name: postgres_local
7+
environment:
8+
POSTGRES_USER: myuser
9+
POSTGRES_PASSWORD: mypassword
10+
POSTGRES_DB: mydatabase
11+
ports:
12+
- "5432:5432"
13+
volumes:
14+
- postgres_data:/var/lib/postgresql/data
15+
16+
volumes:
17+
postgres_data:

mortgage_calculator/mortgage.db

-20 KB
Binary file not shown.

0 commit comments

Comments
 (0)