-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
42 lines (29 loc) · 1.35 KB
/
Copy pathproject.py
File metadata and controls
42 lines (29 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""CRUD operations for Projects."""
from uuid import UUID
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.project import Project
from app.schemas.project import ProjectCreate, ProjectUpdate
async def create_project(db: AsyncSession, payload: ProjectCreate) -> Project:
project = Project(**payload.model_dump())
db.add(project)
await db.commit()
await db.refresh(project)
return project
async def get_project(db: AsyncSession, project_id: UUID) -> Project | None:
result = await db.execute(select(Project).where(Project.id == project_id))
return result.scalar_one_or_none()
async def list_projects(db: AsyncSession, skip: int = 0, limit: int = 100) -> tuple[list[Project], int]:
count_result = await db.execute(select(func.count()).select_from(Project))
total = count_result.scalar_one()
result = await db.execute(select(Project).offset(skip).limit(limit))
return list(result.scalars().all()), total
async def update_project(db: AsyncSession, project_id: UUID, payload: ProjectUpdate) -> Project | None:
project = await get_project(db, project_id)
if project is None:
return None
for field, value in payload.model_dump(exclude_unset=True).items():
setattr(project, field, value)
await db.commit()
await db.refresh(project)
return project