UPDATE
This commit is contained in:
+87
-16
@@ -9,11 +9,32 @@ from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from config.openapi import build_envelope_serializer, build_response
|
||||
from .models import Plant
|
||||
from .serializers import PlantSerializer
|
||||
from .services import fetch_plant_info_from_api
|
||||
|
||||
|
||||
PlantListResponseSerializer = build_envelope_serializer(
|
||||
"PlantListResponseSerializer",
|
||||
PlantSerializer,
|
||||
many=True,
|
||||
)
|
||||
PlantDetailResponseSerializer = build_envelope_serializer(
|
||||
"PlantDetailResponseSerializer",
|
||||
PlantSerializer,
|
||||
)
|
||||
PlantValidationErrorSerializer = build_envelope_serializer(
|
||||
"PlantValidationErrorSerializer",
|
||||
data_required=False,
|
||||
allow_null=True,
|
||||
)
|
||||
PlantFetchInfoResponseSerializer = build_envelope_serializer(
|
||||
"PlantFetchInfoResponseSerializer",
|
||||
PlantSerializer,
|
||||
)
|
||||
|
||||
|
||||
class PlantListCreateView(APIView):
|
||||
"""لیست تمام گیاهان و ایجاد گیاه جدید."""
|
||||
|
||||
@@ -21,7 +42,12 @@ class PlantListCreateView(APIView):
|
||||
tags=["Plant"],
|
||||
summary="لیست گیاهان",
|
||||
description="لیست تمام گیاهان ذخیرهشده را برمیگرداند.",
|
||||
responses={200: PlantSerializer(many=True)},
|
||||
responses={
|
||||
200: build_response(
|
||||
PlantListResponseSerializer,
|
||||
"لیست گیاهان ذخیرهشده.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def get(self, request):
|
||||
plants = Plant.objects.all()
|
||||
@@ -37,8 +63,14 @@ class PlantListCreateView(APIView):
|
||||
description="یک گیاه جدید با مشخصات دادهشده ایجاد میکند.",
|
||||
request=PlantSerializer,
|
||||
responses={
|
||||
201: PlantSerializer,
|
||||
400: OpenApiResponse(description="داده نامعتبر"),
|
||||
201: build_response(
|
||||
PlantDetailResponseSerializer,
|
||||
"گیاه جدید با موفقیت ایجاد شد.",
|
||||
),
|
||||
400: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"داده ورودی نامعتبر است.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
@@ -83,8 +115,14 @@ class PlantDetailView(APIView):
|
||||
summary="جزئیات گیاه",
|
||||
description="مشخصات یک گیاه را بر اساس شناسه برمیگرداند.",
|
||||
responses={
|
||||
200: PlantSerializer,
|
||||
404: OpenApiResponse(description="گیاه یافت نشد"),
|
||||
200: build_response(
|
||||
PlantDetailResponseSerializer,
|
||||
"جزئیات گیاه.",
|
||||
),
|
||||
404: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"گیاه یافت نشد.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def get(self, request, pk):
|
||||
@@ -106,9 +144,18 @@ class PlantDetailView(APIView):
|
||||
description="تمام فیلدهای یک گیاه را آپدیت میکند.",
|
||||
request=PlantSerializer,
|
||||
responses={
|
||||
200: PlantSerializer,
|
||||
400: OpenApiResponse(description="داده نامعتبر"),
|
||||
404: OpenApiResponse(description="گیاه یافت نشد"),
|
||||
200: build_response(
|
||||
PlantDetailResponseSerializer,
|
||||
"گیاه با موفقیت بهروزرسانی شد.",
|
||||
),
|
||||
400: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"داده ورودی نامعتبر است.",
|
||||
),
|
||||
404: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"گیاه یافت نشد.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def put(self, request, pk):
|
||||
@@ -136,9 +183,18 @@ class PlantDetailView(APIView):
|
||||
description="فقط فیلدهای ارسالشده آپدیت میشوند.",
|
||||
request=PlantSerializer,
|
||||
responses={
|
||||
200: PlantSerializer,
|
||||
400: OpenApiResponse(description="داده نامعتبر"),
|
||||
404: OpenApiResponse(description="گیاه یافت نشد"),
|
||||
200: build_response(
|
||||
PlantDetailResponseSerializer,
|
||||
"گیاه با موفقیت بهروزرسانی شد.",
|
||||
),
|
||||
400: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"داده ورودی نامعتبر است.",
|
||||
),
|
||||
404: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"گیاه یافت نشد.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def patch(self, request, pk):
|
||||
@@ -165,8 +221,14 @@ class PlantDetailView(APIView):
|
||||
summary="حذف گیاه",
|
||||
description="یک گیاه را حذف میکند.",
|
||||
responses={
|
||||
200: OpenApiResponse(description="حذف موفق"),
|
||||
404: OpenApiResponse(description="گیاه یافت نشد"),
|
||||
200: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"گیاه با موفقیت حذف شد.",
|
||||
),
|
||||
404: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"گیاه یافت نشد.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def delete(self, request, pk):
|
||||
@@ -197,9 +259,18 @@ class PlantFetchInfoView(APIView):
|
||||
},
|
||||
),
|
||||
responses={
|
||||
200: PlantSerializer,
|
||||
400: OpenApiResponse(description="نام گیاه ارسال نشده"),
|
||||
503: OpenApiResponse(description="سرویس در دسترس نیست"),
|
||||
200: build_response(
|
||||
PlantFetchInfoResponseSerializer,
|
||||
"اطلاعات گیاه از سرویس خارجی دریافت شد.",
|
||||
),
|
||||
400: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"نام گیاه ارسال نشده است.",
|
||||
),
|
||||
503: build_response(
|
||||
PlantValidationErrorSerializer,
|
||||
"سرویس خارجی در دسترس نیست.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
|
||||
Reference in New Issue
Block a user