/
george2066
/
Wallet
Обзор
Документация
Войти
/
george2066
/
Wallet
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
api/views.py
38 строк
2 KB
george2066
reset
07 июл 2025, 20:35
07 июл 2025, 20:35
9b5a40e
Код
Авторство
О чём код?
from rest_framework import status from api.models import Wallet from api.serializers import OperationSerializer from rest_framework.response import Response from rest_framework.views import APIView class WalletOperationView(APIView): def post(self, request, wallet_uuid): try: wallet = Wallet.objects.get(uuid=wallet_uuid) except Wallet.DoesNotExist: return Response({"error": "Wallet not found."}, status=status.HTTP_404_NOT_FOUND) serializer = OperationSerializer(data=request.data) if serializer.is_valid(): operation_type = serializer.validated_data['operation_type'] amount = serializer.validated_data['amount'] try: if operation_type == "DEPOSIT": wallet.deposit(amount) elif operation_type == "WITHDRAW": wallet.withdraw(amount) return Response({"balance": str(wallet.balance)}, status=status.HTTP_200_OK) except ValueError as e: return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) class WalletBalanceView(APIView): def get(self, request, wallet_uuid): try: wallet = Wallet.objects.get(uuid=wallet_uuid) return Response({"balance": str(wallet.balance)}, status=status.HTTP_200_OK) except Wallet.DoesNotExist: return Response({"error": "Wallet not found."}, status=status.HTTP_404_NOT_FOUND)