-
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.
- Loading branch information
Showing
11 changed files
with
296 additions
and
126 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,7 @@ class Person(UUIDBase): | |
|
||
full_name: str | ||
films: list[PersonFilm] | ||
|
||
|
||
class PersonName(UUIDBase): | ||
name: str |
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,51 +1,45 @@ | ||
from functools import lru_cache | ||
from typing import Annotated, NewType | ||
|
||
from dataclasses import dataclass | ||
from uuid import UUID | ||
|
||
from core.settings import settings | ||
from db.elastic import get_elastic | ||
from db.redis import get_redis | ||
from elasticsearch import AsyncElasticsearch, NotFoundError | ||
from fastapi import Depends | ||
from models.film import Film | ||
from redis.asyncio import Redis | ||
|
||
FILM_CACHE_EXPIRE_IN_SECONDS = 60 * 5 | ||
FilmID = NewType("FilmID", UUID) | ||
|
||
|
||
@dataclass | ||
class FilmService: | ||
def __init__(self, redis: Redis, elastic: AsyncElasticsearch): | ||
self.redis = redis | ||
self.elastic = elastic | ||
|
||
async def get_by_id(self, film_id: str) -> Film | None: | ||
film = await self._film_from_cache(film_id) | ||
if not film: | ||
film = await self._get_film_from_elastic(film_id) | ||
if not film: | ||
return None | ||
await self._put_film_to_cache(film) | ||
|
||
return film | ||
|
||
async def _get_film_from_elastic(self, film_id: str) -> Film | None: | ||
elastic: Annotated[AsyncElasticsearch, Depends(get_elastic)] | ||
|
||
async def search( | ||
self, | ||
*, | ||
query: str | None = None, | ||
page: int = 1, | ||
size: int = settings.default_page_size, | ||
sort_order: str = "asc", | ||
sort_by: str = "id", | ||
genre: str | None = "Action", | ||
) -> list[Film]: | ||
result = await self.elastic.search( | ||
index=settings.es_films_index, | ||
from_=(page - 1) * size, | ||
size=size, | ||
query={"match": {"title": query, "genre": genre}} if query else {"match_all": {}}, | ||
sort={sort_by: {"order": sort_order}}, | ||
) | ||
|
||
return [Film.model_validate(hit["_source"]) for hit in result["hits"]["hits"]] | ||
|
||
async def get_by_id(self, film_id: FilmID) -> Film | None: | ||
try: | ||
doc = await self.elastic.get(index="movies", id=film_id) | ||
doc = await self.elastic.get(index=settings.es_films_index, id=str(film_id)) | ||
except NotFoundError: | ||
return None | ||
return Film(**doc["_source"]) | ||
|
||
async def _film_from_cache(self, film_id: str) -> Film | None: | ||
data = await self.redis.get(film_id) | ||
if not data: | ||
return None | ||
|
||
return Film.parse_raw(data) | ||
|
||
async def _put_film_to_cache(self, film: Film) -> None: | ||
await self.redis.set(str(film.id), film.json(), FILM_CACHE_EXPIRE_IN_SECONDS) | ||
|
||
|
||
@lru_cache | ||
def get_film_service( | ||
redis: Redis = Depends(get_redis), | ||
elastic: AsyncElasticsearch = Depends(get_elastic), | ||
) -> FilmService: | ||
return FilmService(redis, elastic) | ||
return Film.model_validate(doc["_source"]) |
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,5 @@ | ||
from typing import Any | ||
|
||
|
||
def get_key_by_args(*args: Any, **kwargs: Any) -> str: | ||
return f"{args}:{kwargs}" |
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,47 @@ | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
||
from models.film import Film | ||
from services.film import FilmID, FilmService | ||
|
||
|
||
@pytest.fixture() | ||
async def film_service(elastic): | ||
return FilmService(elastic) | ||
|
||
|
||
async def test_search_film_by_id(film_service): | ||
# Arrange | ||
star_wars_id = FilmID(UUID("c35dc09c-8ace-46be-8941-7e50b768ec33")) | ||
|
||
# Act | ||
film = await film_service.get_by_id(star_wars_id) | ||
# Assert | ||
assert film is not None | ||
assert film.id == star_wars_id | ||
assert film.imdb_rating == 6.6 | ||
assert film.title == "Star Wars" | ||
assert ( | ||
film.description | ||
== "Luke Skywalker, a young farmer from the desert planet of Tattooine, must save Princess Leia from the evil Darth Vader." | ||
) | ||
assert film.director == [] | ||
assert film.actors_names == [] | ||
assert film.writers_names == ["George Lucas"] | ||
assert film.actors == [] | ||
assert film.writers[0].id == UUID("a5a8f573-3cee-4ccc-8a2b-91cb9f55250a") | ||
assert film.writers[0].name == "George Lucas" | ||
|
||
|
||
async def test_search_persons(film_service: FilmService): | ||
# Arrange | ||
page = 2 | ||
size = 5 | ||
|
||
# Act | ||
films = await film_service.search(page=page, size=size) | ||
|
||
# Assert | ||
assert len(films) == size | ||
assert all(isinstance(film, Film) for film in films) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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