from backend.db.models.houses import Houses
from sqlalchemy.orm import Session

from backend.schemas.house import HouseCreate

class RepositoryHouses:
  def __init__(self):
    self.houses = Houses()

  def get_all(self, skip: int = 0, limit: int = 100):
    return self.houses.query.offset(skip).limit(limit).all()

  def get_by_id(self, house_id: str):
    return self.houses.query.filter_by(id=house_id).one()

  def create(self, db: Session, house: HouseCreate):
    try:
      db_house = Houses(**house.dict())
      db.add(db_house)
      db.commit()
    except Exception:
      db.rollback()
      raise

    db.refresh(db_house)
    return db_house
