diff --git a/alembic/versions/20250428_093516_f9a484a3_update_rent_monitoring.py b/alembic/versions/20250428_093516_f9a484a3_update_rent_monitoring.py new file mode 100644 index 0000000..45b7aeb --- /dev/null +++ b/alembic/versions/20250428_093516_f9a484a3_update_rent_monitoring.py @@ -0,0 +1,38 @@ +"""Initial creation of rent_monitoring table + +Revision ID: 9a76b3f5a8c1 +Revises: 0001 +Create Date: 2023-05-24 11:32:41.754124 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func + +# revision identifiers, used by Alembic. +revision = '9a76b3f5a8c1' +down_revision = '0001' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'rent_monitoring', + sa.Column('id', sa.String(36), primary_key=True, default=func.uuid_generate_v4()), + sa.Column('property_address', sa.String(), nullable=False), + sa.Column('property_type', sa.String(), nullable=False), + sa.Column('bedrooms', sa.Integer(), nullable=False), + sa.Column('bathrooms', sa.Integer(), nullable=False), + sa.Column('square_feet', sa.Float(), nullable=False), + sa.Column('rent_amount', sa.Float(), nullable=False), + sa.Column('listing_url', sa.String(), nullable=False), + sa.Column('listing_source', sa.String(), nullable=False), + sa.Column('is_available', sa.Boolean(), nullable=False, server_default='1'), + sa.Column('created_at', sa.DateTime(), nullable=False, server_default=func.now()), + sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=func.now(), onupdate=func.now()) + ) + + +def downgrade(): + op.drop_table('rent_monitoring') \ No newline at end of file diff --git a/endpoints/rentmonitoring.get.py b/endpoints/rentmonitoring.get.py index e69de29..9e8b4f5 100644 --- a/endpoints/rentmonitoring.get.py +++ b/endpoints/rentmonitoring.get.py @@ -0,0 +1,13 @@ +from fastapi import APIRouter, Depends +from typing import List +from sqlalchemy.orm import Session +from core.database import get_db +from schemas.rent_monitoring import RentMonitoringSchema +from helpers.rent_monitoring_helpers import get_all_rent_monitorings + +router = APIRouter() + +@router.get("/rentmonitoring", status_code=200, response_model=List[RentMonitoringSchema]) +async def get_rent_monitorings(db: Session = Depends(get_db)): + rent_monitorings = get_all_rent_monitorings(db) + return rent_monitorings \ No newline at end of file diff --git a/helpers/rent_monitoring_helpers.py b/helpers/rent_monitoring_helpers.py new file mode 100644 index 0000000..8b792bf --- /dev/null +++ b/helpers/rent_monitoring_helpers.py @@ -0,0 +1,87 @@ +from typing import List, Optional +from sqlalchemy.orm import Session +from models.rent_monitoring import RentMonitoring +from schemas.rent_monitoring import RentMonitoringCreate, RentMonitoringUpdate +import uuid + +def get_all_rent_monitorings(db: Session) -> List[RentMonitoring]: + """ + Retrieves all rent monitoring records from the database. + + Args: + db (Session): The database session. + + Returns: + List[RentMonitoring]: A list of all rent monitoring objects. + """ + return db.query(RentMonitoring).all() + +def get_rent_monitoring_by_id(db: Session, rent_monitoring_id: uuid.UUID) -> Optional[RentMonitoring]: + """ + Retrieves a single rent monitoring record by its ID. + + Args: + db (Session): The database session. + rent_monitoring_id (UUID): The ID of the rent monitoring record to retrieve. + + Returns: + Optional[RentMonitoring]: The rent monitoring object if found, otherwise None. + """ + return db.query(RentMonitoring).filter(RentMonitoring.id == rent_monitoring_id).first() + +def create_rent_monitoring(db: Session, rent_monitoring_data: RentMonitoringCreate) -> RentMonitoring: + """ + Creates a new rent monitoring record in the database. + + Args: + db (Session): The database session. + rent_monitoring_data (RentMonitoringCreate): The data for the rent monitoring record to create. + + Returns: + RentMonitoring: The newly created rent monitoring object. + """ + db_rent_monitoring = RentMonitoring(**rent_monitoring_data.dict()) + db.add(db_rent_monitoring) + db.commit() + db.refresh(db_rent_monitoring) + return db_rent_monitoring + +def update_rent_monitoring(db: Session, rent_monitoring_id: uuid.UUID, rent_monitoring_data: RentMonitoringUpdate) -> RentMonitoring: + """ + Updates an existing rent monitoring record in the database. + + Args: + db (Session): The database session. + rent_monitoring_id (UUID): The ID of the rent monitoring record to update. + rent_monitoring_data (RentMonitoringUpdate): The updated data for the rent monitoring record. + + Returns: + RentMonitoring: The updated rent monitoring object. + """ + db_rent_monitoring = db.query(RentMonitoring).filter(RentMonitoring.id == rent_monitoring_id).first() + if not db_rent_monitoring: + raise ValueError(f"Rent monitoring record with ID {rent_monitoring_id} not found.") + update_data = rent_monitoring_data.dict(exclude_unset=True) + for key, value in update_data.items(): + setattr(db_rent_monitoring, key, value) + db.commit() + db.refresh(db_rent_monitoring) + return db_rent_monitoring + +def delete_rent_monitoring(db: Session, rent_monitoring_id: uuid.UUID) -> bool: + """ + Deletes an existing rent monitoring record from the database. + + Args: + db (Session): The database session. + rent_monitoring_id (UUID): The ID of the rent monitoring record to delete. + + Returns: + bool: True if the rent monitoring record was successfully deleted, False otherwise. + """ + db_rent_monitoring = db.query(RentMonitoring).filter(RentMonitoring.id == rent_monitoring_id).first() + if not db_rent_monitoring: + return False + db.delete(db_rent_monitoring) + db.commit() + return True \ No newline at end of file diff --git a/models/rent_monitoring.py b/models/rent_monitoring.py new file mode 100644 index 0000000..676d229 --- /dev/null +++ b/models/rent_monitoring.py @@ -0,0 +1,21 @@ +from sqlalchemy import Column, String, Integer, Boolean, DateTime, Float +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func +from core.database import Base +import uuid + +class RentMonitoring(Base): + __tablename__ = "rent_monitoring" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + property_address = Column(String, nullable=False) + property_type = Column(String, nullable=False) + bedrooms = Column(Integer, nullable=False) + bathrooms = Column(Integer, nullable=False) + square_feet = Column(Float, nullable=False) + rent_amount = Column(Float, nullable=False) + listing_url = Column(String, nullable=False) + listing_source = Column(String, nullable=False) + is_available = Column(Boolean, default=True) + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 596e6f3..db12c92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,6 @@ sqlalchemy>=1.4.0 python-dotenv>=0.19.0 bcrypt>=3.2.0 alembic>=1.13.1 +jose +passlib +pydantic diff --git a/schemas/rent_monitoring.py b/schemas/rent_monitoring.py new file mode 100644 index 0000000..dd4e295 --- /dev/null +++ b/schemas/rent_monitoring.py @@ -0,0 +1,37 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +import uuid + +class RentMonitoringBase(BaseModel): + property_address: str + property_type: str + bedrooms: int + bathrooms: int + square_feet: float + rent_amount: float + listing_url: str + listing_source: str + is_available: bool = Field(True) + +class RentMonitoringCreate(RentMonitoringBase): + pass + +class RentMonitoringUpdate(RentMonitoringBase): + property_address: Optional[str] = None + property_type: Optional[str] = None + bedrooms: Optional[int] = None + bathrooms: Optional[int] = None + square_feet: Optional[float] = None + rent_amount: Optional[float] = None + listing_url: Optional[str] = None + listing_source: Optional[str] = None + is_available: Optional[bool] = None + +class RentMonitoringSchema(RentMonitoringBase): + id: uuid.UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file