diff --git a/commerce/controllers.py b/commerce/controllers.py index 389aff6..0968d49 100644 --- a/commerce/controllers.py +++ b/commerce/controllers.py @@ -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]) @@ -111,9 +112,9 @@ 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]) @@ -121,7 +122,7 @@ def list_addresses(request): # return Category.objects.all() -@address_controller.get('cities', response={ +@city_controller.get('cities', response={ 200: List[CitiesOut], 404: MessageOut }) @@ -134,7 +135,7 @@ 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 }) @@ -142,7 +143,7 @@ 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 }) @@ -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 }) @@ -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): @@ -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'} diff --git a/commerce/schemas.py b/commerce/schemas.py index 0944297..38378d1 100644 --- a/commerce/schemas.py +++ b/commerce/schemas.py @@ -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): @@ -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 \ No newline at end of file diff --git a/config/urls.py b/config/urls.py index fea5e70..c717323 100644 --- a/config/urls.py +++ b/config/urls.py @@ -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)