Skip to content

Commit

Permalink
add service for my orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Nvnastya committed Dec 1, 2024
1 parent 7274595 commit d74b32a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 0 additions & 3 deletions django/tamprog/garden/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ def rent(self, request):

log.debug(f"Attempting to rent {beds_count} beds for user with ID={user.id} in field with ID={field_id}")

# Получаем поле по ID
try:
field = Field.objects.get(id=field_id)
except Field.DoesNotExist:
Expand Down Expand Up @@ -372,15 +371,13 @@ def release(self, request):

log.debug(f"Attempting to release {beds_count} beds in field with ID={field_id}")

# Получаем поле по ID
try:
field = Field.objects.get(id=field_id)
except Field.DoesNotExist:
return Response({'error': 'Field not found'}, status=status.HTTP_404_NOT_FOUND)

BedService.release_beds(field, beds_count)

# Возвращаем успешный ответ
return Response({
'message': f'{beds_count} beds released successfully.'
}, status=status.HTTP_200_OK)
Expand Down
5 changes: 5 additions & 0 deletions django/tamprog/orders/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def complete_order(order):
log.debug(f"Order with ID={order.id} completed")
return order

@staticmethod
def get_orders(user):
log.debug(f"Getting orders by user with ID={user.id}")
return Order.objects.filter(user=user)

@staticmethod
def filter_orders(is_completed=None):
if is_completed is not None:
Expand Down
18 changes: 18 additions & 0 deletions django/tamprog/orders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError
from rest_framework.decorators import action
from .serializer import *
from .models import Order
from .services import OrderService
Expand Down Expand Up @@ -176,6 +177,23 @@ def perform_update(self, serializer):
log.debug(f"Completing order with ID={order.id}")
OrderService.complete_order(order)

@extend_schema(
summary='List all orders for current user',
description='List all orders for current user',
responses={
status.HTTP_200_OK: OpenApiResponse(
description='Successful response with list of orders',
response=OrderSerializer(many=True),
)
},
)
@action(detail=False, methods=['get'])
def my_orders(self, request):
orders = OrderService.get_orders(request.user)
serializer = self.get_serializer(orders, many=True)
log.debug(f"Returning list of orders by user: {serializer.data}")
return Response(serializer.data)

def get_queryset(self):
is_completed = self.request.query_params.get('is_completed', None)
if is_completed is not None:
Expand Down
6 changes: 4 additions & 2 deletions django/tamprog/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ class LoginView(generics.GenericAPIView):
OpenApiExample(
name="Successful login",
value={
'username': "example_user",
"id": 1,
"username": "example_user",
"refresh": "string",
"access": "string",
"wallet_balance": 0.00,
'is_staff': False,
"is_staff": False,
},
)
],
Expand Down Expand Up @@ -86,6 +87,7 @@ def post(self, request, *args, **kwargs):
refresh = RefreshToken.for_user(user)
log.info(f"User {user.username} logged in successfully")
return Response({
'id': user.id,
'username': user.username,
'refresh': str(refresh),
'access': str(refresh.access_token),
Expand Down

0 comments on commit d74b32a

Please sign in to comment.