-
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
1 parent
bc894ae
commit bf6e156
Showing
11 changed files
with
152 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
|
||
|
||
from core.config import settings | ||
from db.postgres import create_tables | ||
|
||
|
||
# @asynccontextmanager | ||
|
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
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 sqlalchemy.orm import DeclarativeBase | ||
|
||
|
||
class Base(DeclarativeBase): | ||
pass |
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 |
---|---|---|
@@ -1,12 +1,85 @@ | ||
from litestar import Controller, get, Request | ||
from dataclasses import dataclass | ||
from typing import Annotated | ||
from litestar import Controller, get, Request, post, Response, MediaType | ||
from litestar.exceptions import * | ||
from litestar.enums import RequestEncodingType | ||
from litestar.params import Body | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from modules.weather_information import get_weather_information | ||
from sqlalchemy.dialects.postgresql import insert | ||
from sqlalchemy import select | ||
from models.animal import Animal | ||
from models.stream import Stream | ||
from litestar.datastructures import State | ||
from models.streams_animals import streams_animals | ||
|
||
|
||
@dataclass | ||
class AnimalItem: | ||
animal: str | ||
count: int | ||
|
||
|
||
# TODO: exclude from schemas | ||
# Controller for internal endpoints | ||
class internalController(Controller): | ||
path = "/internal" | ||
tags = ["internal"] | ||
|
||
@get("/streams") | ||
async def get_streams( | ||
self, session: AsyncSession | ||
) -> list[Stream]: | ||
pass | ||
|
||
@post("/stream_animals") | ||
async def store_stream_animals( | ||
self, session: AsyncSession, stream_id: int, data: Annotated[list[AnimalItem], Body()] | ||
) -> Response: | ||
# Check if provided stream_id is valid. | ||
if not await session.scalars(select(Stream.id).filter_by(id=stream_id)).first(): | ||
return Response( | ||
media_type=MediaType.TEXT, | ||
content="Provided stream id is not valid.", | ||
status_code=422, | ||
) | ||
|
||
# Save animals to provided stream_id. | ||
for animal in data: | ||
animal_name, animal_count = animal.animal, animal.count | ||
|
||
# Check if animal already exists in database, if not create animal. | ||
animal_id = await session.scalars(select(Animal.id).filter_by(name=animal_name)).first() | ||
|
||
# If no animal exists, create one. | ||
if not animal_id: | ||
# Create animal object, get extra information from external API. | ||
animal_db = Animal( | ||
name=animal_name, | ||
) | ||
session.add(animal_db) | ||
|
||
# Get id of newly created animal. | ||
session.flush() | ||
animal_id = animal_db.id | ||
|
||
# Link animal to stream_id. | ||
stmt = insert(streams_animals).values( | ||
stream_id=stream_id, | ||
animal_id=animal_id, | ||
count=animal_count, | ||
) | ||
stmt = stmt.on_conflict_do_update( | ||
index_elements=["stream_id", "animal_id"], | ||
set_={"count": streams_animals.c.count + animal_count}, | ||
) | ||
await session.execute(stmt) | ||
|
||
# Save all changes to database. | ||
await session.commit() | ||
|
||
return Response( | ||
media_type=MediaType.TEXT, | ||
content="Successfully saved provided animals to stream.", | ||
status_code=201, | ||
) |
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,41 @@ | ||
from dataclasses import dataclass | ||
from typing import Annotated | ||
from litestar import Controller, get, Request, post, Response, MediaType | ||
from litestar.exceptions import * | ||
from litestar.enums import RequestEncodingType | ||
from litestar.params import Body | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from sqlalchemy.dialects.postgresql import insert | ||
from sqlalchemy import select | ||
from models.animal import Animal | ||
from models.stream import Stream | ||
from litestar.datastructures import State | ||
from models.streams_animals import streams_animals | ||
from litestar.contrib.sqlalchemy.repository import SQLAlchemyAsyncRepository | ||
from litestar.di import Provide | ||
|
||
|
||
class StreamRepository(SQLAlchemyAsyncRepository[Stream]): | ||
model_type = Stream | ||
|
||
|
||
async def provide_streams_repository(session: AsyncSession) -> StreamRepository: | ||
return StreamRepository(session=session) | ||
|
||
|
||
# TODO: exclude from schemas | ||
# Controller for internal endpoints | ||
class internalController(Controller): | ||
path = "/internal" | ||
tags = ["internal-streams"] | ||
|
||
dependencies = {"streams_repository": Provide(provide_streams_repository)} | ||
|
||
@get("/streams") | ||
async def get_streams(self, stream_repository: StreamRepository) -> list[Stream]: | ||
return await stream_repository.list() | ||
|
||
@get("/streams/{stream_id}") | ||
async def get_stream(self, stream_repository: StreamRepository, stream_id: int) -> Stream: | ||
return await stream_repository.get(item_id=stream_id, load=[Stream.tag, Stream.country, Stream.animals]) |