Skip to content

Commit

Permalink
Merge pull request #48 from VaquitApp/category-id
Browse files Browse the repository at this point in the history
Merge main with develop, fix tests, and add `id` to Category
  • Loading branch information
MegaRedHand authored May 31, 2024
2 parents 5e64342 + ad62f8f commit 9b28109
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 55 deletions.
34 changes: 25 additions & 9 deletions src/crud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import select
from sqlalchemy import delete, select
from sqlalchemy.orm import Session
from uuid import UUID

Expand Down Expand Up @@ -34,7 +34,10 @@ def create_user(db: Session, user: schemas.UserCreate) -> models.User:

def create_category(db: Session, category: schemas.CategoryCreate):
new_category = models.Category(
name=category.name, description=category.description, group_id=category.group_id
group_id=category.group_id,
name=category.name,
description=category.description,
strategy=category.strategy,
)
db.add(new_category)
db.commit()
Expand All @@ -45,16 +48,26 @@ def create_category(db: Session, category: schemas.CategoryCreate):
def get_categories_by_group_id(db: Session, group_id: int):
return db.query(models.Category).filter(models.Category.group_id == group_id).all()

def get_category(db: Session, group_id: int, category_name= str):
return db.query(models.Category).filter(models.Category.group_id == group_id, models.Category.name == category_name).first()

def get_category_by_id(db: Session, id: int):
return db.query(models.Category).filter(models.Category.id == id).first()


def delete_category(db: Session, category: models.Category):
db.delete(category)
db.commit()
return {"message": "Categoria eliminada exitosamente!"}
return category

def get_category_by_name_and_group_id(db: Session, category_name: str, group_id: int):
return db.query(models.Category).filter(models.Category.name == category_name, models.Category.group_id == group_id).first()

def update_category(
db: Session, category: models.Category, category_update: schemas.CategoryUpdate
):
category.name = category_update.name
category.description = category_update.description
category.strategy = category_update.strategy
db.commit()
db.refresh(category)
return category


################################################
Expand Down Expand Up @@ -132,6 +145,7 @@ def create_spending(db: Session, spending: schemas.SpendingCreate, user_id: int)
db.refresh(db_spending)
return db_spending


def get_spendings_by_group_id(db: Session, group_id: int):
return (
db.query(models.Spending)
Expand All @@ -140,10 +154,11 @@ def get_spendings_by_group_id(db: Session, group_id: int):
.all()
)

def get_spendings_by_category(db: Session, category_name:str):

def get_spendings_by_category(db: Session, category_id: int):
return (
db.query(models.Spending)
.filter(models.Spending.category_name == category_name)
.filter(models.Spending.category_id == category_id)
.limit(100)
.all()
)
Expand Down Expand Up @@ -190,6 +205,7 @@ def get_budgets_by_group_id(db: Session, group_id: int):
# INVITES
################################################


def get_invite_by_token(db: Session, token: str):
return db.query(models.Invite).filter(models.Invite.token == UUID(token)).first()

Expand Down
60 changes: 43 additions & 17 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,55 @@ def create_category(category: schemas.CategoryCreate, db: DbDependency):
return crud.create_category(db, category)


@app.get("/category/{group_id}")
def list_group_categories(db: DbDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)

if group is None:
@app.get("/category/{category_id}")
def update_category(db: DbDependency, category_id: int):
category = crud.get_category_by_id(db, category_id)
if category is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Grupo inexistente"
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)

categories = crud.get_categories_by_group_id(db, group_id)
return category

return categories

@app.put("/category/{category_id}")
def update_category(
category_update: schemas.CategoryUpdate, db: DbDependency, category_id: int
):
category = crud.get_category_by_id(db, category_id)
if category is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)

@app.delete("/category", status_code=HTTPStatus.OK)
def delete_category(category: schemas.CategoryBase , db: DbDependency):
category_to_delete = crud.get_category(db, category.group_id, category.name)
return crud.update_category(db, category, category_update)


@app.delete("/category/{category_id}")
def delete_category(db: DbDependency, category_id: int):
category_to_delete = crud.get_category_by_id(db, category_id)
if category_to_delete is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)

return crud.delete_category(db, category_to_delete)


@app.get("/group/{group_id}/category")
def list_group_categories(db: DbDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)

if group is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Grupo inexistente"
)

categories = crud.get_categories_by_group_id(db, group_id)

return categories


################################################
# GROUPS
################################################
Expand Down Expand Up @@ -238,7 +263,8 @@ def unarchive_group(db: DbDependency, user: UserDependency, group_id: int):

@app.post("/spending", status_code=HTTPStatus.CREATED)
def create_spending(
spending: schemas.SpendingCreate, db: DbDependency, user: UserDependency):
spending: schemas.SpendingCreate, db: DbDependency, user: UserDependency
):
group = crud.get_group_by_id(db, spending.group_id)

check_group_exists_and_user_is_member(user.id, group)
Expand All @@ -248,14 +274,13 @@ def create_spending(
status_code=HTTPStatus.NOT_ACCEPTABLE,
detail="El grupo esta archivado, no se pueden seguir agregando gastos.",
)

categories = crud.get_categories_by_group_id(db, group.id)
category = crud.get_category_by_name_and_group_id(db, spending.category_name,group.id)
if category is None or (category not in categories):

category = crud.get_category_by_id(db, spending.category_id)
if category is None or category.group_id != spending.group_id:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)

return crud.create_spending(db, spending, user.id)


Expand Down Expand Up @@ -343,6 +368,7 @@ def list_group_budgets(db: DbDependency, user: UserDependency, group_id: int):
# INVITES
################################################


@app.get("/invite/{token}")
def get_invite(db: DbDependency, token: str):
invite = crud.get_invite_by_token(db, token)
Expand Down
15 changes: 10 additions & 5 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
String,
Boolean,
Table,
UniqueConstraint,
func,
Enum,
UUID,
Expand Down Expand Up @@ -44,7 +45,7 @@ class Group(Base):
owner_id = Column(ForeignKey("users.id"))
name = Column(String)
description = Column(String)
is_archived = Column(Boolean)
is_archived = Column(Boolean)
members: Mapped[Set[User]] = relationship(
secondary=user_to_group_table, back_populates="groups"
)
Expand All @@ -53,19 +54,23 @@ class Group(Base):
class Category(Base):
__tablename__ = "categories"

name = Column(String, primary_key=True)
id = Column(Integer, primary_key=True)
group_id = Column(ForeignKey("groups.id"))
name = Column(String)
description = Column(String)
group_id = Column(ForeignKey("groups.id"), primary_key=True)
# TODO: move strategy to enums
strategy = Column(String)

__table_args__ = (UniqueConstraint("group_id", "name"),)


class Spending(Base):
__tablename__ = "spendings"

id = Column(Integer, primary_key=True)
owner_id = Column(ForeignKey("users.id"))
group_id = Column(ForeignKey("groups.id"))
category_name = Column(String)
category_id = Column(ForeignKey("categories.id"))
amount = Column(Integer)
description = Column(String)
date: Mapped[datetime] = mapped_column(DateTime, default=func.now())
Expand All @@ -76,7 +81,7 @@ class Budget(Base):

id = Column(Integer, primary_key=True)
group_id = Column(ForeignKey("groups.id"))
category_id = Column(Integer) # TODO: Column(ForeignKey("categories.id"))
category_id = Column(ForeignKey("categories.id"))
start_date: Mapped[datetime] = mapped_column(DateTime)
end_date: Mapped[datetime] = mapped_column(DateTime)
amount = Column(Integer)
Expand Down
18 changes: 14 additions & 4 deletions src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ class AddUserToGroupRequest(BaseModel):

class CategoryBase(BaseModel):
name: str
group_id: int

class CategoryCreate(CategoryBase):
description: str
strategy: str


class Category(CategoryBase):
id: int
group_id: int


class CategoryCreate(CategoryBase):
group_id: int


class CategoryUpdate(CategoryBase):
pass


Expand Down Expand Up @@ -85,14 +92,17 @@ class SpendingBase(BaseModel):
description: str
date: Optional[datetime] = Field(None)
group_id: int
category_name: str
category_id: int


class SpendingCreate(SpendingBase):
pass


class SpendingPut(SpendingBase):
pass


class Spending(SpendingBase):
id: int
owner_id: int
Expand Down
Loading

0 comments on commit 9b28109

Please sign in to comment.