Skip to content

Commit

Permalink
Merge pull request #45 from tigran-saatchyan/features/22-cart-impleme…
Browse files Browse the repository at this point in the history
…ntation

[FEATURE] Cart implementation + refactoring - #22
  • Loading branch information
tigran-saatchyan authored Nov 17, 2023
2 parents 97ded85 + e6a9c90 commit f4167bf
Show file tree
Hide file tree
Showing 33 changed files with 592 additions and 600 deletions.
74 changes: 74 additions & 0 deletions app/api/v1/cart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from typing import Annotated, List, Union

from fastapi import APIRouter, Depends

from app.api.v1.dependencies import UOWDependency, current_user
from app.models import User
from app.schemas.cart import CartCreate, CartUpdate
from app.services.cart import CartService

router = APIRouter(
prefix="/cart",
tags=["Cart"],
)


@router.post("/")
async def add_product_to_cart(
user: Annotated[User, Depends(current_user)],
product: Union[CartCreate, List[CartCreate]],
uow: UOWDependency,
) -> Union[int, List[int]]:
return await CartService().add(uow, product, user)


@router.get("/get_all/")
async def get_all_products_from_cart(
uow: UOWDependency,
user: Annotated[User, Depends(current_user)],
):
return await CartService().get_all(uow, user)


@router.get("/{product_id}")
async def get_one_product_from_cart(
uow: UOWDependency,
product_id: int,
user: Annotated[User, Depends(current_user)],
):
return await CartService().get(uow, product_id, user)


@router.patch("/{product_id}")
async def update_product_quantity(
uow: UOWDependency,
product_id: int,
product: CartUpdate,
user: Annotated[User, Depends(current_user)],
):
return await CartService().update(uow, product_id, product, user)


@router.delete("/{product_id}")
async def remove_product_from_cart(
uow: UOWDependency,
product_id: int,
user: Annotated[User, Depends(current_user)],
):
return await CartService().delete(uow, product_id, user)


@router.delete("/clear_cart/")
async def clear_cart(
uow: UOWDependency,
user: Annotated[User, Depends(current_user)],
):
return await CartService().delete_all(uow, user)


@router.get("/total_price/")
async def get_total_cart_price(
user: Annotated[User, Depends(current_user)],
uow: UOWDependency,
) -> float:
return await CartService().get_total_price(uow, user)
42 changes: 28 additions & 14 deletions app/api/v1/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from app.api.v1.dependencies import UOWDependency, current_user
from app.models import User
from app.schemas.products import ProductsSchemaAdd, ProductsSchemaEdit
from app.schemas.products import ProductsCreate, ProductsUpdate
from app.services.products import ProductsService

router = APIRouter(
Expand All @@ -16,31 +16,45 @@
@router.post("/")
async def add_product(
user: Annotated[User, Depends(current_user)],
product: ProductsSchemaAdd,
product: ProductsCreate,
uow: UOWDependency,
) -> int:
return await ProductsService().add(uow, product, user)


@router.get("/", dependencies=[Depends(current_user)])
async def get_products(uow: UOWDependency):
return await ProductsService().get_all(uow)
@router.get("/")
async def get_products(
uow: UOWDependency,
user: Annotated[User, Depends(current_user)],
):
return await ProductsService().get_all(uow, user)


@router.get("/{product_id}", dependencies=[Depends(current_user)])
async def get_product(product_id: int, uow: UOWDependency):
return await ProductsService().get(uow, product_id)
@router.get("/{product_id}")
async def get_product(
product_id: int,
uow: UOWDependency,
user: Annotated[User, Depends(current_user)],
):
return await ProductsService().get(uow, product_id, user)


@router.patch("/{product_id}", dependencies=[Depends(current_user)])
@router.patch("/{product_id}")
async def update_product(
product_id: int, product: ProductsSchemaEdit, uow: UOWDependency
product_id: int,
product: ProductsUpdate,
uow: UOWDependency,
user: Annotated[User, Depends(current_user)],
):
await ProductsService().update(uow, product_id, product)
await ProductsService().update(uow, product_id, product, user)
return {"message": "Product updated successfully"}


@router.delete("/{product_id}", dependencies=[Depends(current_user)])
async def delete_product(product_id: int, uow: UOWDependency):
await ProductsService().delete(uow, product_id)
@router.delete("/{product_id}")
async def delete_product(
product_id: int,
uow: UOWDependency,
user: Annotated[User, Depends(current_user)],
):
await ProductsService().delete(uow, product_id, user)
return {"message": "Product deleted successfully"}
2 changes: 2 additions & 0 deletions app/api/v1/routers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from app.api.v1.cart import router as cart_router
from app.api.v1.products import router as product_router

all_routers = [
product_router,
cart_router,
]

This file was deleted.

46 changes: 0 additions & 46 deletions app/migrations/versions/49af15ac2ace_updated_last_login.py

This file was deleted.

40 changes: 0 additions & 40 deletions app/migrations/versions/541102dacd00_updated_users_table.py

This file was deleted.

This file was deleted.

47 changes: 47 additions & 0 deletions app/migrations/versions/6a1f68c6ba5c_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""initial
Revision ID: 6a1f68c6ba5c
Revises:
Create Date: 2023-11-16 20:49:30.626229+04:00
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '6a1f68c6ba5c'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('user',
sa.Column('telephone', sa.String(length=50), nullable=False),
sa.Column('first_name', sa.String(length=150), nullable=True),
sa.Column('last_name', sa.String(length=150), nullable=True),
sa.Column('last_login', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('email', sa.String(length=320), nullable=False),
sa.Column('hashed_password', sa.String(length=1024), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.Column('is_superuser', sa.Boolean(), nullable=False),
sa.Column('is_verified', sa.Boolean(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('telephone')
)
op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_user_email'), table_name='user')
op.drop_table('user')
# ### end Alembic commands ###
Loading

0 comments on commit f4167bf

Please sign in to comment.