Skip to content

Commit

Permalink
fix another merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
saantim committed Jun 18, 2024
2 parents a3ac33c + 257dbd5 commit 022ea16
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 29 deletions.
9 changes: 8 additions & 1 deletion src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_category_by_id(db: Session, id: int):


def delete_category(db: Session, category: models.Category):
db.delete(category)
category.is_archived = True
db.commit()
return category

Expand All @@ -71,6 +71,13 @@ def update_category(
return category


def update_category_status(db: Session, category: models.Category, status: bool):
category.is_archived = status
db.commit()
db.refresh(category)
return category


################################################
# GROUPS
################################################
Expand Down
45 changes: 35 additions & 10 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ def list_group_balances(db: DbDependency, user: UserDependency, group_id: int):
################################################


def check_category_is_unarchived(category: models.Category):
if category.is_archived:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="La categoria esta archivada, no se pueden realizar modificaciones",
)


@app.post("/category", status_code=HTTPStatus.CREATED)
def create_category(
category: schemas.CategoryCreate,
Expand Down Expand Up @@ -318,8 +326,13 @@ def update_category(
return crud.update_category(db, category, category_update)


@app.delete("/category/{category_id}")
def delete_category(db: DbDependency, user: UserDependency, category_id: int):
@app.put("/category/{category_id}/is_archived")
def update_category_status(
new_status: schemas.CategoryStatusUpdate,
db: DbDependency,
user: UserDependency,
category_id: int,
):
category = crud.get_category_by_id(db, category_id)
if category is None:
raise HTTPException(
Expand All @@ -328,7 +341,7 @@ def delete_category(db: DbDependency, user: UserDependency, category_id: int):
group = crud.get_group_by_id(db, category.group_id)
check_group_exists_and_user_is_owner(db, user.id, group)
check_group_is_unarchived(group)
return crud.delete_category(db, category)
return crud.update_category_status(db, category, new_status.is_archived)


@app.get("/group/{group_id}/category")
Expand Down Expand Up @@ -363,7 +376,7 @@ def check_strategy_data(group: models.Group, category: models.Category, strategy


@app.get("/group/{group_id}/spending")
def list_group_unique_spendings(db: DbDependency, user: UserDependency, group_id: int):
def list_group_spendings(db: DbDependency, user: UserDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)

check_group_exists_and_user_is_member(db, user.id, group)
Expand Down Expand Up @@ -391,9 +404,9 @@ def create_unique_spending(
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)
check_strategy_data(group, category, spending.strategy_data, spending.amount, db)
check_category_is_unarchived(category)
return crud.create_unique_spending(db, spending, user.id, category.strategy)


@app.get("/group/{group_id}/unique-spending")
def list_group_unique_spendings(db: DbDependency, user: UserDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)
Expand Down Expand Up @@ -423,6 +436,7 @@ def create_installment_spending(
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)
check_category_is_unarchived(category)

res = []

Expand Down Expand Up @@ -472,6 +486,7 @@ def create_recurring_spending(
status_code=HTTPStatus.NOT_FOUND, detail="Categoria inexistente"
)

check_category_is_unarchived(category)
return crud.create_recurring_spending(db, spending, user.id, category.strategy)


Expand Down Expand Up @@ -593,15 +608,21 @@ def list_group_payments(db: DbDependency, user: UserDependency, group_id: int):


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

check_group_exists_and_user_is_owner(db, user.id, group)
check_group_is_unarchived(group)

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

check_category_is_unarchived(category)

return crud.create_budget(db, budget)


@app.get("/budget/{budget_id}")
Expand Down Expand Up @@ -629,6 +650,10 @@ def put_budget(
check_group_exists_and_user_is_member(db, user.id, group)
check_group_is_unarchived(group)

category = crud.get_category_by_id(db, db_budget.category_id)

check_category_is_unarchived(category)

return crud.put_budget(db, db_budget, put_budget)


Expand Down
1 change: 1 addition & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Category(Base):
name = Column(String)
description = Column(String)
strategy = Column(Enum(Strategy))
is_archived = Column(Boolean, default=False)

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

Expand Down
5 changes: 5 additions & 0 deletions src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CategoryBase(BaseModel):
class Category(CategoryBase):
id: int
group_id: int
is_archived: bool


class CategoryCreate(CategoryBase):
Expand All @@ -59,6 +60,10 @@ class CategoryUpdate(CategoryBase):
pass


class CategoryStatusUpdate(BaseModel):
is_archived: bool


################################################
# GROUPS
################################################
Expand Down
44 changes: 26 additions & 18 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,6 @@ def test_create_budget_on_archived_group(
# CATEGORIES
################################################

################################################
# CATEGORIES
################################################


@pytest.fixture
def some_category(
Expand Down Expand Up @@ -838,20 +834,6 @@ def test_create_new_category(client: TestClient, some_category: schemas.Category
pass


def test_category_delete(
client: TestClient,
some_credentials: schemas.UserCredentials,
some_category: schemas.Category,
):
response = client.delete(
url=f"/category/{some_category.id}",
headers={"x-user": some_credentials.jwt},
)

assert response.status_code == HTTPStatus.OK
assert response.json() == some_category.model_dump()


def test_category_modify_name(
client: TestClient,
some_credentials: schemas.UserCredentials,
Expand All @@ -874,6 +856,32 @@ def test_category_modify_name(
assert response_body["description"] == "otra descripcion"


def test_archive_category(
client: TestClient,
some_credentials: schemas.UserCredentials,
some_category: schemas.Category,
):
response = client.put(
url=f"/category/{some_category.id}/is_archived",
json={"is_archived": True},
headers={"x-user": some_credentials.jwt},
)

assert response.status_code == HTTPStatus.OK, response.json()
response_body = response.json()
assert response_body["is_archived"] == True

response = client.put(
url=f"/category/{some_category.id}/is_archived",
json={"is_archived": False},
headers={"x-user": some_credentials.jwt},
)

assert response.status_code == HTTPStatus.OK, response.json()
response_body = response.json()
assert response_body["is_archived"] == False


################################################
# INVITES
################################################
Expand Down

0 comments on commit 022ea16

Please sign in to comment.