Skip to content

Commit

Permalink
Ran Black formatted on python code in ./src/
Browse files Browse the repository at this point in the history
There appeared to be some python formatting in bf6e156 that did not conform with Black's formatting standards. So Black(https://github.com/psf/black) formatter was used to fix these issues.
  • Loading branch information
CedricCortenraede authored and github-actions[bot] committed Jun 19, 2024
1 parent bf6e156 commit bc1f241
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/api/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

async def init_db(app: Litestar) -> None:
from models.base import Base

# Import models.
import models.country
import models.stream
Expand All @@ -43,7 +43,7 @@ async def init_db(app: Litestar) -> None:
import db.seeders.country_seeder
import db.seeders.stream_tag_seeder
import db.seeders.stream_seeder

async with app.state.db_engine.begin() as connection:
await connection.run_sync(Base.metadata.create_all)

Expand Down Expand Up @@ -119,12 +119,12 @@ def create_app_private() -> Litestar:

app = create_app()
app_private = create_app_private()


if __name__ == "__main__":
# Run the API (for debugging)
# uvicorn.run("main:app", reload=True, reload_dirs="./", port=8002)

subprocess.Popen(
[
"litestar",
Expand Down
2 changes: 1 addition & 1 deletion src/api/api/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class Base(DeclarativeBase):
pass
pass
31 changes: 17 additions & 14 deletions src/api/api/routers/v1/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ class AnimalItem:
class internalController(Controller):
path = "/internal"
tags = ["internal"]

@get("/streams")
async def get_streams(
self, session: AsyncSession
) -> list[Stream]:
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()]
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():
Expand All @@ -43,26 +44,28 @@ async def store_stream_animals(
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()

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,
Expand All @@ -74,10 +77,10 @@ async def store_stream_animals(
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.",
Expand Down
18 changes: 11 additions & 7 deletions src/api/api/routers/v1/internal_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

class StreamRepository(SQLAlchemyAsyncRepository[Stream]):
model_type = Stream


async def provide_streams_repository(session: AsyncSession) -> StreamRepository:
return StreamRepository(session=session)

Expand All @@ -29,13 +29,17 @@ async def provide_streams_repository(session: AsyncSession) -> StreamRepository:
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])
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]
)

0 comments on commit bc1f241

Please sign in to comment.