Skip to content

Commit

Permalink
#22: feat: add API endpoint /reviews/films/{film_id} for retrieving…
Browse files Browse the repository at this point in the history
… film reviews
  • Loading branch information
ReznikovRoman committed Jul 10, 2022
1 parent 0936374 commit a400614
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/ugc/api/v1/handlers/ugc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING
from uuid import UUID

from aiohttp_apispec import docs, request_schema
from aiohttp_apispec import docs, querystring_schema, request_schema
from dependency_injector.wiring import Provide, inject

from aiohttp import web
Expand Down Expand Up @@ -99,6 +99,22 @@ async def create_film_review(
return orjson_response(review, status=HTTPStatus.CREATED)


@docs(**openapi.get_film_reviews)
@querystring_schema(serializers.CursorPaginationQueryParams)
@inject
async def get_film_reviews(
request: web.Request, *,
review_service: ReviewService = Provide[Container.review_service],
) -> web.Response:
"""Получение списка рецензий на фильм с пагинацией."""
film_id: UUID = request.match_info["film_id"]
query_params: dict = request["querystring"]
reviews, cursor = await review_service.get_film_reviews(
film_id, limit=query_params.get("limit"), cursor=query_params.get("cursor"))
response = {"cursor": cursor, "data": reviews}
return orjson_response(response, status=HTTPStatus.OK)


async def _handle_film_bookmark(
request: web.Request,
bookmark_dispatcher: BookmarkDispatcherService, *,
Expand Down
12 changes: 12 additions & 0 deletions src/ugc/api/v1/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@
HTTPStatus.INTERNAL_SERVER_ERROR: {"description": "Ошибка сервера."},
},
}

get_film_reviews = {
"tags": ["review"],
"summary": "Получить список пользовательских рецензий на фильм.",
"responses": {
HTTPStatus.OK: {
"description": "Список рецензий с пагинацией.",
"schema": serializers.FilmReviewList,
},
HTTPStatus.INTERNAL_SERVER_ERROR: {"description": "Ошибка сервера."},
},
}
5 changes: 5 additions & 0 deletions src/ugc/api/v1/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def setup_routes_v1(app: web.Application) -> None:
path="/users/me/reviews/films/{film_id}",
handler=ugc.create_film_review,
),
web.get(
path="/reviews/films/{film_id}",
handler=ugc.get_film_reviews,
allow_head=False,
),

# Miscellaneous
web.get(
Expand Down
23 changes: 21 additions & 2 deletions src/ugc/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
from marshmallow.validate import Range


class CursorPaginationQueryParams(Schema):
"""Параметры для `cursor-based` сортировки."""

limit = fields.Integer()
cursor = fields.String()


class CursorPaginationResultsMixin(Schema):
"""Миксин для результатов `cursor-based` пагинации."""

cursor = fields.String()


class FilmBookmarkList(Schema):
"""Сериалайзер списка закладок фильмов."""

Expand Down Expand Up @@ -30,8 +43,8 @@ class FilmProgressDetail(Schema):
class FilmReviewCreate(Schema):
"""Сериалайзер для создания рецензии на фильм."""

title = fields.Str(strict=True, required=True)
review = fields.Str(strict=True, required=True)
title = fields.Str(required=True)
review = fields.Str(required=True)


class FilmReviewDetail(Schema):
Expand All @@ -43,3 +56,9 @@ class FilmReviewDetail(Schema):
title = fields.Str()
review = fields.Str()
created_at = fields.DateTime()


class FilmReviewList(CursorPaginationResultsMixin):
"""Список рецензий на фильм."""

data = fields.Nested(FilmReviewDetail(many=True))

0 comments on commit a400614

Please sign in to comment.