-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Docs: Merchandise에 대해 swagger 데이터 추가 * Feat: 일관된 응답을 리턴하도록 로직 수정
- Loading branch information
Showing
8 changed files
with
90 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from drf_spectacular.utils import OpenApiExample, OpenApiResponse | ||
|
||
from core.responses.serializer import ( | ||
ErrorResponseSerializer, | ||
ListSuccessResponseSerializer, | ||
) | ||
from core.swagger import SwaggerSchema | ||
|
||
|
||
class MerchandiseAPIDocs(SwaggerSchema): | ||
@classmethod | ||
def list(cls): | ||
responses = { | ||
"성공": OpenApiResponse( | ||
response=ListSuccessResponseSerializer, | ||
description="다중 응답 성공", | ||
examples=[ | ||
OpenApiExample( | ||
name="상품 목록 조회 (페이지네이션 있음)", | ||
value={ | ||
"status": "SUCCESS", | ||
"data": [ | ||
{ | ||
"id": 1, | ||
"product_name": "상품1", | ||
"product_info": "상품 설명입니다.", | ||
"image": "http://localhost:8000/media/merchandise/1.jpg", | ||
}, | ||
], | ||
"pagination": { | ||
"count": 20, | ||
"next": "http://localhost:8000/api/v1/merchandise/?page=2", | ||
"previous": "http://localhost:8000/api/v1/merchandise/?page=1", | ||
}, | ||
}, | ||
), | ||
OpenApiExample( | ||
name="상품 목록 조회 (데이터 있음)", | ||
value={ | ||
"status": "SUCCESS", | ||
"data": [ | ||
{ | ||
"id": 1, | ||
"product_name": "상품1", | ||
"product_info": "상품 설명입니다.", | ||
"image": "http://localhost:8000/media/merchandise/1.jpg", | ||
}, | ||
], | ||
"pagination": {"count": 1, "next": None, "previous": None}, | ||
}, | ||
), | ||
OpenApiExample( | ||
name="Merchandise 목록 조회 (데이터 없음)", | ||
value={ | ||
"status": "SUCCESS", | ||
"data": [], | ||
"pagination": {"count": 0, "next": None, "previous": None}, | ||
}, | ||
), | ||
], | ||
), | ||
"에러": OpenApiResponse(response=ErrorResponseSerializer, description="응답 에러"), | ||
} | ||
return cls.generate_schema( | ||
operation_id="merchandise_list", description="모든 상품 목록 조회", responses=responses | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,20 @@ | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
from drf_spectacular.utils import extend_schema_view | ||
from rest_framework.viewsets import GenericViewSet | ||
|
||
from core.responses.base import BaseResponse | ||
|
||
from .models import Merchandise | ||
from .serializers import MerchandiseSerializer | ||
from .swagger import MerchandiseAPIDocs | ||
|
||
|
||
class MerchandiseViewSet(ReadOnlyModelViewSet): | ||
""" | ||
MD 페이지는 상세보기 페이지가 존재하지 않으므로 retrieve는 생략되었습니다. | ||
""" | ||
|
||
@extend_schema_view(list=MerchandiseAPIDocs.list()) | ||
class MerchandiseViewSet(GenericViewSet): | ||
queryset = Merchandise.objects.all() | ||
serializer_class = MerchandiseSerializer | ||
|
||
def list(self, request, *args, **kwargs): | ||
queryset = self.filter_queryset(self.get_queryset()) | ||
if not queryset.exists(): | ||
return Response( | ||
{ | ||
"status_code": "200", | ||
"body": { | ||
"code": "success", | ||
"message": "MD 데이터가 존재하지 않습니다", | ||
"data": [], | ||
"pagination": {}, | ||
}, | ||
}, | ||
status=status.HTTP_200_OK, | ||
) | ||
serializer = self.get_serializer(queryset, many=True) | ||
return Response( | ||
{ | ||
"status_code": "200", | ||
"body": { | ||
"code": "success", | ||
"message": "성공", | ||
"data": serializer.data, | ||
"pagination": {}, | ||
}, | ||
}, | ||
status=status.HTTP_200_OK, | ||
) | ||
def list(self, request, *args, **kwargs) -> BaseResponse: | ||
queryset = self.get_queryset() | ||
page = self.paginate_queryset(queryset) | ||
serializer = self.get_serializer(page or queryset, many=True) | ||
return self.get_paginated_response(serializer.data) |