Skip to content

Commit

Permalink
Merge pull request #24 from cs3216-a3-group-4/seeleng/edit-event-routes
Browse files Browse the repository at this point in the history
feat: update event routes
  • Loading branch information
seelengxd authored Sep 22, 2024
2 parents 693695c + abb4692 commit 9dd4db3
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 22 deletions.
61 changes: 61 additions & 0 deletions backend/alembic/versions/37cf2356fe08_move_table_back.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""move table back
Revision ID: 37cf2356fe08
Revises: c3b7f848f6f3
Create Date: 2024-09-23 01:20:37.025966
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "37cf2356fe08"
down_revision: Union[str, None] = "c3b7f848f6f3"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"analysis",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("event_id", sa.Integer(), nullable=False),
sa.Column("category_id", sa.Integer(), nullable=False),
sa.Column("content", sa.String(), nullable=False),
sa.ForeignKeyConstraint(
["category_id"],
["category.id"],
),
sa.ForeignKeyConstraint(
["event_id"],
["event.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.drop_table("analysis1")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"analysis1",
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column("event_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column("category_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column("content", sa.VARCHAR(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(
["category_id"], ["category.id"], name="analysis1_category_id_fkey"
),
sa.ForeignKeyConstraint(
["event_id"], ["event.id"], name="analysis1_event_id_fkey"
),
sa.PrimaryKeyConstraint("id", name="analysis1_pkey"),
)
op.drop_table("analysis")
# ### end Alembic commands ###
31 changes: 31 additions & 0 deletions backend/alembic/versions/90baf2b50ff3_add_id_to_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Add id to analysis
Revision ID: 90baf2b50ff3
Revises: bc5fcbe47713
Create Date: 2024-09-23 00:55:33.251027
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "90baf2b50ff3"
down_revision: Union[str, None] = "bc5fcbe47713"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
(op.add_column("analysis", sa.Column("id", sa.Integer(), nullable=False)),)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("analysis", "id")
# ### end Alembic commands ###
61 changes: 61 additions & 0 deletions backend/alembic/versions/c3b7f848f6f3_move_table_temporarily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""move table temporarily
Revision ID: c3b7f848f6f3
Revises: ddf34f03e551
Create Date: 2024-09-23 01:20:21.380233
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "c3b7f848f6f3"
down_revision: Union[str, None] = "ddf34f03e551"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"analysis1",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("event_id", sa.Integer(), nullable=False),
sa.Column("category_id", sa.Integer(), nullable=False),
sa.Column("content", sa.String(), nullable=False),
sa.ForeignKeyConstraint(
["category_id"],
["category.id"],
),
sa.ForeignKeyConstraint(
["event_id"],
["event.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.drop_table("analysis")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"analysis",
sa.Column("event_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column("category_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column("content", sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column("id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(
["category_id"], ["category.id"], name="analysis_category_id_fkey"
),
sa.ForeignKeyConstraint(
["event_id"], ["event.id"], name="analysis_event_id_fkey"
),
sa.PrimaryKeyConstraint("id", name="analysis_pkey"),
)
op.drop_table("analysis1")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""make id in analysis actually pk
Revision ID: ddf34f03e551
Revises: 90baf2b50ff3
Create Date: 2024-09-23 01:08:01.468695
"""

from typing import Sequence, Union

from alembic import op


# revision identifiers, used by Alembic.
revision: str = "ddf34f03e551"
down_revision: Union[str, None] = "90baf2b50ff3"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.execute("ALTER TABLE analysis DROP CONSTRAINT analysis_pkey CASCADE")
op.create_primary_key("analysis_pkey", "analysis", ["id"])
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
16 changes: 11 additions & 5 deletions backend/src/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ class Event(Base):
back_populates="events", secondary="analysis"
)

analysises: Mapped[list["Analysis"]] = relationship(back_populates="event")

original_article: Mapped[Article] = relationship(back_populates="original_events")
articles: Mapped[list[Article]] = relationship(
back_populates="events", secondary=article_event_table
)
gp_questions: Mapped["GPQuestion"] = relationship(back_populates="event")
gp_questions: Mapped[list["GPQuestion"]] = relationship(back_populates="event")

notes = relationship(
"Note",
Expand All @@ -84,17 +86,21 @@ class Category(Base):
events: Mapped[list[Event]] = relationship(
secondary="analysis", back_populates="categories"
)
analysises: Mapped[list["Analysis"]] = relationship(back_populates="category")


class Analysis(Base):
__tablename__ = "analysis"

event_id: Mapped[int] = mapped_column(ForeignKey("event.id"), primary_key=True)
category_id: Mapped[int] = mapped_column(
ForeignKey("category.id"), primary_key=True
)
id: Mapped[int] = mapped_column(primary_key=True)

event_id: Mapped[int] = mapped_column(ForeignKey("event.id"))
category_id: Mapped[int] = mapped_column(ForeignKey("category.id"))
content: Mapped[str]

event: Mapped[Event] = relationship(back_populates="analysises")
category: Mapped[Category] = relationship(back_populates="analysises")


class GPQuestion(Base):
__tablename__ = "gp_question"
Expand Down
34 changes: 30 additions & 4 deletions backend/src/events/router.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from datetime import datetime
from http import HTTPStatus
from typing import Annotated
from fastapi import APIRouter, Depends, Query
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from src.auth.dependencies import get_current_user
from src.auth.models import User
from src.events.models import Category, Event
from src.events.models import Analysis, Article, Category, Event, GPQuestion
from src.common.dependencies import get_session
from src.events.schemas import EventDTO, EventIndexResponse

Expand All @@ -15,6 +17,8 @@
@router.get("/")
def get_events(
_: Annotated[User, Depends(get_current_user)],
start_date: Annotated[datetime | None, Query()] = None,
end_date: Annotated[datetime | None, Query()] = None,
session=Depends(get_session),
category_ids: Annotated[list[int] | None, Query()] = None,
limit: int | None = None,
Expand All @@ -35,6 +39,15 @@ def get_events(
event_query = event_query.limit(limit)
if offset is not None:
event_query = event_query.offset(offset)
if start_date is not None:
event_query = event_query.where(
Event.original_article.has(Article.date >= start_date)
)
if end_date is not None:
event_query = event_query.where(
Event.original_article.has(Article.date <= end_date)
)
event_query = event_query.order_by(Event.rating.desc(), Event.date.desc())

events = list(session.scalars(event_query))
return EventIndexResponse(total_count=total_count, count=len(events), data=events)
Expand All @@ -47,7 +60,20 @@ def get_event(
session=Depends(get_session),
) -> EventDTO:
event = session.scalar(
select(Event).where(Event.id == id).options(selectinload(Event.categories))
select(Event)
.where(Event.id == id)
.options(
selectinload(
Event.gp_questions,
GPQuestion.categories,
),
selectinload(
Event.categories,
),
selectinload(Event.analysises, Analysis.category),
)
)
# TODO: link to more models, give more data
if not event:
raise HTTPException(HTTPStatus.NOT_FOUND)

return event
26 changes: 23 additions & 3 deletions backend/src/events/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,39 @@
from src.categories.schemas import CategoryDTO


class EventDTO(BaseModel):
class MiniEventDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
title: str
description: str
analysis: str
is_singapore: bool
date: datetime

categories: list[CategoryDTO]


class AnalysisDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
category: CategoryDTO
content: str


class GPQuestionDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
question: str
is_llm_generated: bool
categories: list[CategoryDTO]


class EventDTO(MiniEventDTO):
model_config = ConfigDict(from_attributes=True)
analysises: list[AnalysisDTO]
gp_questions: list[GPQuestionDTO]


class EventIndexResponse(BaseModel):
total_count: int
count: int
data: list[EventDTO]
data: list[MiniEventDTO]
Loading

0 comments on commit 9dd4db3

Please sign in to comment.