Skip to content

Commit

Permalink
finally finished
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Dec 31, 2024
1 parent 64221e9 commit bbbf241
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions server/routes/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Projects(BaseModel):
"sig_arch",
"sig_graph",
]
tags: Optional[list[str]]
active: bool
founded_at: datetime.datetime

Expand All @@ -61,6 +62,7 @@ class PartialProjects(BaseModel):
"sig_arch",
"sig_graph",
]
tags: Optional[list[str]]
active: bool
founded_at: datetime.datetime

Expand Down Expand Up @@ -231,13 +233,16 @@ class CreateProject(BaseModel):
"sig_arch",
"sig_graph",
]
tags: Optional[list[str]] = None
active: bool
founded_at: datetime.datetime


# todo: add tags along with projects
# Depends on roles, admins can only use this endpoint
@router.post("/projects/create", responses={200: {"model": PartialProjects}})
@router.post(
"/projects/create",
responses={200: {"model": PartialProjects}, 422: {"model": HTTPExceptionMessage}},
)
async def create_project(
request: RouteRequest,
req: CreateProject,
Expand All @@ -249,8 +254,41 @@ async def create_project(
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
"""
rows = await request.app.pool.fetchrow(query, *req.model_dump().values())
return PartialProjects(**dict(rows))

async with request.app.pool.acquire() as connection:
tr = connection.transaction()

project_rows = await connection.fetchrow(
query, *req.model_dump(exclude={"tags"}).values()
)

if req.tags:
subquery = """
INSERT INTO project_tags (project_id, tag_id)
VALUES ($1, (SELECT id FROM tags WHERE title = $2));
"""

await tr.start()

try:
await request.app.pool.fetchmany(
subquery, [(project_rows["id"], tags.lower()) for tags in req.tags]
)
except asyncpg.NotNullViolationError:
await tr.rollback()

# Remove the newly created entry, somewhat like a rollback
await connection.execute(
"DELETE FROM projects WHERE id = $1;", project_rows["id"]
)
raise HTTPException(
detail="The tag(s) specified is invalid. Please check the current tags available.",
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
)
else:
await tr.commit()

return PartialProjects(**dict(project_rows), tags=req.tags)


class JoinResponse(BaseModel):
Expand Down

0 comments on commit bbbf241

Please sign in to comment.