Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e-commerce #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 87 additions & 10 deletions commerce/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from ninja import Router
from pydantic import UUID4

from commerce.models import Product, Category, City, Vendor, Item, Order, OrderStatus
from commerce.schemas import MessageOut, ProductOut, CitiesOut, CitySchema, VendorOut, ItemOut, ItemSchema, ItemCreate
from commerce.models import Address, Product, Category, City, Vendor, Item, Order, OrderStatus
from commerce.schemas import AddressSchema, AddressesOut, CheckoutSchema, MessageOut, ProductOut, CitiesOut, CitySchema, VendorOut, ItemOut, ItemSchema, ItemCreate

products_controller = Router(tags=['products'])
address_controller = Router(tags=['addresses'])
vendor_controller = Router(tags=['vendors'])
order_controller = Router(tags=['orders'])
city_controller = Router(tags=['cities'])


@vendor_controller.get('', response=List[VendorOut])
Expand Down Expand Up @@ -111,17 +112,17 @@ def list_products(
"""


@address_controller.get('')
def list_addresses(request):
pass
# @address_controller.get('')
# def list_addresses(request):
# pass


# @products_controller.get('categories', response=List[CategoryOut])
# def list_categories(request):
# return Category.objects.all()


@address_controller.get('cities', response={
@city_controller.get('cities', response={
200: List[CitiesOut],
404: MessageOut
})
Expand All @@ -134,15 +135,15 @@ def list_cities(request):
return 404, {'detail': 'No cities found'}


@address_controller.get('cities/{id}', response={
@city_controller.get('cities/{id}', response={
200: CitiesOut,
404: MessageOut
})
def retrieve_city(request, id: UUID4):
return get_object_or_404(City, id=id)


@address_controller.post('cities', response={
@city_controller.post('cities', response={
201: CitiesOut,
400: MessageOut
})
Expand All @@ -152,7 +153,7 @@ def create_city(request, city_in: CitySchema):
return 201, city


@address_controller.put('cities/{id}', response={
@city_controller.put('cities/{id}', response={
200: CitiesOut,
400: MessageOut
})
Expand All @@ -163,7 +164,7 @@ def update_city(request, id: UUID4, city_in: CitySchema):
return 200, city


@address_controller.delete('cities/{id}', response={
@city_controller.delete('cities/{id}', response={
204: MessageOut
})
def delete_city(request, id: UUID4):
Expand Down Expand Up @@ -252,3 +253,79 @@ def create_order(request):
order_qs.save()

return {'detail': 'order created successfully'}






############ Adresses CRUD
@order_controller.post('/orders/item/{id}/increase-quantity', response={
200: MessageOut,
})
def increase_item_quantity(request, id: UUID4):
item = get_object_or_404(Item, id=id, user=User.objects.first())
item.item_qty += 1
item.save()

return 200, {'detail': 'Item quantity increased successfully!'}



@address_controller.get('addresses', response={
200: List[AddressesOut],
404: MessageOut
})
def list_addresses(request):
addresses_qs = Address.objects.all()

if addresses_qs:
return addresses_qs

return 404, {'detail': 'No addresses found'}


@address_controller.get('addresses/{id}', response={
200: AddressesOut,
404: MessageOut
})
def retrieve_address(request, id: UUID4):
return get_object_or_404(Address, id=id)


@address_controller.post('addresses', response={
200: MessageOut,
400: MessageOut
})
def create_address(request, address_in: AddressSchema):
Address.objects.create(**address_in.dict() , user=User.objects.first())
return 200, {'detail': 'Added successfully'}


@address_controller.put('addresses/{id}', response={
200: AddressSchema,
400: MessageOut
})
def update_address(request, id: UUID4, address_in: AddressSchema):
address = get_object_or_404(Address, id=id)
for key, value in address_in.items():
setattr(address, key, value)
return 200, address


@address_controller.delete('addresses/{id}', response={
204: MessageOut
})
def delete_address(request, id: UUID4):
address = get_object_or_404(Address, id=id)
address.delete()
return 204, {'detail': ''}


@order_controller.put('/api/orders/checkout/{id}', response=MessageOut)

def checkout(request, id: UUID4, checkout_in: CheckoutSchema):
order = get_object_or_404(Order, id=id)
for key, value in checkout_in.items():
setattr(order, key, value)
return {'detail': 'order checkout successfully'}
24 changes: 23 additions & 1 deletion commerce/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ninja.orm import create_schema
from pydantic import UUID4

from commerce.models import Product, Merchant
from commerce.models import Address, Product, Merchant


class MessageOut(Schema):
Expand Down Expand Up @@ -91,4 +91,26 @@ class ItemCreate(Schema):
class ItemOut(UUIDSchema, ItemSchema):
pass

class UserOut(UUIDSchema):
username:str
last_name:str
email:str
is_staff:str
is_active:str


class AddressSchema(Schema):
city_id:UUID4
work_address:bool
address1:str
address2:str
phone:str


class AddressesOut(UUIDSchema , AddressSchema):
pass


class CheckoutSchema(Schema):
note: str = None
address: UUID4
3 changes: 2 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
from django.urls import path
from ninja import NinjaAPI

from commerce.controllers import products_controller, address_controller, vendor_controller, order_controller
from commerce.controllers import products_controller, address_controller, vendor_controller, order_controller,city_controller
from config import settings

api = NinjaAPI()

api.add_router('products', products_controller)
api.add_router('addresses', address_controller)
api.add_router('cities', city_controller)
api.add_router('vendors', vendor_controller)
api.add_router('orders', order_controller)

Expand Down