Skip to content

Commit

Permalink
#22: WIP: feat: add API endpoint for creating new film reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
ReznikovRoman committed Jul 9, 2022
1 parent c50ba11 commit fe5065d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/ugc/api/v1/handlers/ugc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

from ugc.api.security import get_user_id_from_jwt
from ugc.api.utils import orjson_response
from ugc.api.v1 import openapi
from ugc.api.v1.serializers import FilmProgressCreate
from ugc.api.v1 import openapi, serializers
from ugc.containers import Container

if TYPE_CHECKING:
from ugc.domain.bookmarks import BookmarkDispatcherService, BookmarkService
from ugc.domain.progress import ProgressDispatcherService, ProgressService
from ugc.domain.reviews import ReviewService


@docs(**openapi.add_film_bookmark)
Expand Down Expand Up @@ -55,7 +55,7 @@ async def get_user_films_bookmarks(


@docs(**openapi.track_film_progress)
@request_schema(FilmProgressCreate)
@request_schema(serializers.FilmProgressCreate)
@inject
async def track_film_progress(
request: web.Request, *,
Expand Down Expand Up @@ -83,6 +83,20 @@ async def get_film_progress(
return orjson_response(progress, status=HTTPStatus.OK)


@docs(**openapi.create_film_review)
@request_schema(serializers.FilmReviewCreate)
@inject
async def create_film_review(
request: web.Request, *,
review_service: ReviewService = Provide[Container.review_service],
) -> web.Response:
"""Создание новой рецензии на фильм от авторизированного пользователя."""
film_id: UUID = request.match_info["film_id"] # noqa: F841
user_id = get_user_id_from_jwt(request.headers) # noqa: F841
review = await review_service.create_review()
return orjson_response(review, status=HTTPStatus.CREATED)


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

create_film_review = {
"tags": ["review"],
"summary": "Создать пользовательскую рецензию на фильм.",
"security": [{"JWT": []}],
"responses": {
HTTPStatus.CREATED: {
"description": "Рецензия создана.",
"schema": serializers.FilmReviewDetail,
},
HTTPStatus.UNAUTHORIZED: {"description": "Пользователь не авторизован."},
HTTPStatus.BAD_REQUEST: {"description": "Ошибка в теле запроса."},
HTTPStatus.INTERNAL_SERVER_ERROR: {"description": "Ошибка сервера."},
},
}
10 changes: 9 additions & 1 deletion src/ugc/api/v1/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def setup_routes_v1(app: web.Application) -> None:
app.add_routes([
# UGC
# Bookmarks
web.post(
path="/users/me/bookmarks/films/{film_id}",
handler=ugc.add_film_bookmark,
Expand All @@ -19,6 +19,8 @@ def setup_routes_v1(app: web.Application) -> None:
handler=ugc.get_user_films_bookmarks,
allow_head=False,
),

# Progress
web.post(
path="/users/me/progress/films/{film_id}",
handler=ugc.track_film_progress,
Expand All @@ -29,6 +31,12 @@ def setup_routes_v1(app: web.Application) -> None:
allow_head=False,
),

# Reviews
web.post(
path="/users/me/reviews/films/{film_id}",
handler=ugc.create_film_review,
),

# Miscellaneous
web.get(
path="/health",
Expand Down
17 changes: 17 additions & 0 deletions src/ugc/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ class FilmProgressDetail(Schema):
user_id = fields.UUID()
film_id = fields.UUID()
viewed_frame = fields.Integer()


class FilmReviewCreate(Schema):
"""Сериалайзер для создания рецензии на фильм."""

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


class FilmReviewDetail(Schema):
"""Сериалайзер рецензии на фильм."""

id = fields.Str() # noqa: VNE003
user_id = fields.UUID()
film_id = fields.UUID()
title = fields.Str()
review = fields.Str()

0 comments on commit fe5065d

Please sign in to comment.