Skip to content

Commit

Permalink
Changed association table StreamAnimal
Browse files Browse the repository at this point in the history
  • Loading branch information
CedricCortenraede committed Jun 20, 2024
1 parent 784d839 commit 47d10a3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
39 changes: 18 additions & 21 deletions src/api/api/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
from pathlib import Path
import subprocess
from pathlib import Path

import litestar.cli.commands.core
import uvicorn
from core import settings
from db.connector import postgres_connection, redis_connection
from litestar import Litestar
from litestar.config.cors import CORSConfig
from litestar.contrib.sqlalchemy.base import UUIDAuditBase
from litestar.contrib.sqlalchemy.plugins import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
from litestar.openapi import OpenAPIConfig
from litestar.openapi.spec import Components, SecurityScheme
from litestar.plugins.structlog import (
StructlogPlugin,
StructlogConfig,
StructLoggingConfig,
StructlogPlugin,
)
from litestar.types import Logger
from litestar.contrib.sqlalchemy.plugins import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)

from sqlalchemy import URL
from models.country import Country
from models.animal import Animal
from models.country import Country
from models.stream import Stream
from litestar.contrib.sqlalchemy.base import UUIDAuditBase


from core import settings
from db.connector import redis_connection, postgres_connection
from models.stream_animal import StreamAnimal
from routers import create_router, create_router_private
from sqlalchemy import URL

# Setup basic logging config
config = StructLoggingConfig()
Expand All @@ -38,16 +36,15 @@

async def init_db(app: Litestar) -> None:
# Import models.
import models.country
import models.stream
import models.animal
import models.streams_animals

# Import seeders.
import db.seeders.country_seeder
import db.seeders.stream_tag_seeder
import db.seeders.stream_seeder
import db.seeders.stream_tag_seeder
import db.seeders.users_seeder
import models.animal
import models.country
import models.stream
import models.stream_animal

async with app.state.db_engine.begin() as connection:
await connection.run_sync(UUIDAuditBase.metadata.create_all)
Expand Down
7 changes: 2 additions & 5 deletions src/api/api/models/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
class Animal(UUIDAuditBase):
__tablename__ = "animals"

name: Mapped[str] = mapped_column(String)
name: Mapped[str] = mapped_column(String, unique=True)

streams: Mapped[List["Stream"]] = relationship(
secondary="streams_animals",
back_populates="animals",
)
stream_animals: Mapped[List["StreamAnimal"]] = relationship(back_populates="animal")

def __repr__(self) -> str:
return f"Animal(id={self.id!r}, name={self.name!r})"
4 changes: 2 additions & 2 deletions src/api/api/models/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class Country(UUIDAuditBase):
__tablename__ = "countries"

iso: Mapped[str] = mapped_column(String(3))
name: Mapped[str] = mapped_column(String)
iso: Mapped[str] = mapped_column(String(3), unique=True)
name: Mapped[str] = mapped_column(String, unique=True)

streams: Mapped[List["Stream"]] = relationship(
back_populates="country", cascade="all, delete-orphan"
Expand Down
7 changes: 2 additions & 5 deletions src/api/api/models/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Stream(UUIDAuditBase):
__tablename__ = "streams"

name: Mapped[str] = mapped_column(String)
url: Mapped[str] = mapped_column(String)
url: Mapped[str] = mapped_column(String, unique=True)
tag_id: Mapped[int] = mapped_column(ForeignKey("tags.id"))
country_id: Mapped[str] = mapped_column(ForeignKey("countries.id"))
location: Mapped[str] = mapped_column(String)
Expand All @@ -32,10 +32,7 @@ class Stream(UUIDAuditBase):

tag: Mapped["StreamTag"] = relationship(back_populates="streams")
country: Mapped["Country"] = relationship(back_populates="streams")
animals: Mapped[List["Animal"]] = relationship(
secondary="streams_animals",
back_populates="streams",
)
stream_animals: Mapped[List["StreamAnimal"]] = relationship(back_populates="stream")

def __repr__(self) -> str:
return f"Stream(id={self.id!r}, name={self.name!r}, url={self.url!r}, tag={self.tag_id!r}, country_id={self.country_id!r}, location={self.location!r}, latitude={self.latitude!r}, longitude={self.longitude!r})"
22 changes: 22 additions & 0 deletions src/api/api/models/stream_animal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List
from uuid import UUID

from litestar.contrib.sqlalchemy.base import UUIDAuditBase
from sqlalchemy import Column, ForeignKey, Integer, Table, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.types import Integer


class StreamAnimal(UUIDAuditBase):
__tablename__ = "stream_animal"

stream_id: Mapped[UUID] = mapped_column(ForeignKey("streams.id"), primary_key=True)
animal_id: Mapped[UUID] = mapped_column(ForeignKey("animals.id"), primary_key=True)
count: Mapped[int] = mapped_column(Integer)

stream: Mapped["Stream"] = relationship(back_populates="stream_animals")
animal: Mapped["Animal"] = relationship(back_populates="stream_animals")

__table_args__ = (
UniqueConstraint("stream_id", "animal_id", name="stream_animal_uc"),
)
11 changes: 0 additions & 11 deletions src/api/api/models/streams_animals.py

This file was deleted.

0 comments on commit 47d10a3

Please sign in to comment.