diff --git a/core/apps/shared/serializers/plan.py b/core/apps/shared/serializers/plan.py index e50e893..7c8bd96 100644 --- a/core/apps/shared/serializers/plan.py +++ b/core/apps/shared/serializers/plan.py @@ -22,4 +22,20 @@ class PlanSerializer(serializers.ModelSerializer): description=validated_data.get('description'), date=validated_data.get('date'), user=self.context.get('user'), - ) \ No newline at end of file + ) + + +class PlanUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = Plan + fields = [ + 'title', 'description', 'date', 'is_done', + ] + + def update(self, instance, validated_data): + instance.title = validated_data.get('title', instance.title) + instance.description = validated_data.get('description', instance.description) + instance.date = validated_data.get('date', instance.date) + instance.is_done = validated_data.get('is_done') + instance.save() + return instance \ No newline at end of file diff --git a/core/apps/shared/urls.py b/core/apps/shared/urls.py index f8cdd2b..3dbdeb3 100644 --- a/core/apps/shared/urls.py +++ b/core/apps/shared/urls.py @@ -59,6 +59,8 @@ urlpatterns = [ [ path('', plan_view.PlanApiView.as_view(), name='plan-create-list-api'), path('/complite/', plan_view.ComplitePlanApiView.as_view(), name='complite-plan-api'), + path('/delete/', plan_view.PlanDeleteApiView.as_view(), name='plan-delete-api'), + path('/update/', plan_view.PlanUpdateApiView.as_view(), name='plan-update-api'), ] )), path('location/', include( diff --git a/core/apps/shared/views/plan.py b/core/apps/shared/views/plan.py index acbe906..a9e7744 100644 --- a/core/apps/shared/views/plan.py +++ b/core/apps/shared/views/plan.py @@ -1,15 +1,16 @@ # django from django.shortcuts import get_object_or_404 # rest framework -from rest_framework import generics, permissions +from rest_framework import generics, permissions, views # drf yasg from drf_yasg.utils import swagger_auto_schema +from drf_yasg import openapi # shared from core.apps.shared.models import Plan from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer -from core.apps.shared.serializers.plan import PlanSerializer +from core.apps.shared.serializers.plan import PlanSerializer, PlanUpdateSerializer from core.apps.shared.utils.response_mixin import ResponseMixin @@ -86,4 +87,142 @@ class ComplitePlanApiView(generics.GenericAPIView, ResponseMixin): message='malumot yangilandi' ) except Exception as e: - return self.error_response(data=str(e), message='xatolik') \ No newline at end of file + return self.error_response(data=str(e), message='xatolik') + + +class PlanUpdateApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = PlanUpdateSerializer + queryset = Plan.objects.all() + permission_classes = [permissions.IsAuthenticated] + + @swagger_auto_schema( + responses={ + 200: openapi.Response( + schema=None, + description="Success", + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "malumot tahrirlandi", + "data": { + "id": 1, + "title": "string", + "description": "string", + "date": "string", + "is_done": "true", + "created_at": "string", + } + } + } + ), + 400: openapi.Response( + schema=None, + description="Failure", + examples={ + "application/json": { + "status_code": 400, + "status": "failure", + "message": "malumot tahrirlanmadi", + "data": "string" + } + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + 404: openapi.Response( + schema=None, + description="Not Found", + examples={ + "application/json": { + "status_code": 404, + "status": "failure", + "message": "Plan topilmadi", + "data": {} + } + } + ) + } + ) + def patch(self, request, id): + try: + obj = Plan.objects.filter(id=id, user=request.user).first() + if not obj: + return self.failure_response(message="Plan topilmadi", data={}, status_code=404) + serializer = self.serializer_class(data=request.data, instance=obj) + if serializer.is_valid(): + obj = serializer.save() + created_data = PlanSerializer(obj).data + return self.success_response( + data=created_data, + message='malumot tahrirlandi', + status_code=200 + ) + return self.failure_response(data=serializer.errors, message='malumot tahrirlanmadi') + except Exception as e: + return self.error_response(data=str(e), message='xatolik') + + + +class PlanDeleteApiView(views.APIView, ResponseMixin): + permission_classes = [permissions.IsAuthenticated] + + @swagger_auto_schema( + responses={ + 204: openapi.Response( + schema=None, + description="Success", + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "Plan o'chirildi", + "data": {} + } + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + 404: openapi.Response( + schema=None, + description="Not Found", + examples={ + "application/json": { + "status_code": 404, + "status": "failure", + "message": "Plan topilmadi", + "data": {} + } + } + ) + } + ) + def delete(self, request, id): + try: + obj = Plan.objects.filter(id=id, user=request.user).first() + if not obj: + return self.failure_response(message="Plan topilmadi", status_code=404, data={}) + obj.delete() + return self.success_response(message="Plan o'chirildi", status_code=204, data={}) + except Exception as e: + return self.error_response(data=str(e), message="xatolik") \ No newline at end of file