-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_repository.py
More file actions
59 lines (50 loc) · 1.83 KB
/
Copy pathproject_repository.py
File metadata and controls
59 lines (50 loc) · 1.83 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from uuid import UUID
from sqlalchemy.orm import Session
from app.models.project import Project, ProjectStatus
class ProjectRepository:
def __init__(self, db: Session) -> None:
self._db = db
def create(self, tenant_id: UUID, title: str, created_by: UUID, description: str | None = None) -> Project:
project = Project(
tenant_id=tenant_id,
title=title,
description=description,
status=ProjectStatus.DRAFT,
created_by=created_by,
)
self._db.add(project)
self._db.commit()
self._db.refresh(project)
return project
def get_by_id(self, project_id: UUID, tenant_id: UUID) -> Project | None:
return (
self._db.query(Project)
.filter(Project.id == project_id, Project.tenant_id == tenant_id)
.first()
)
def list(
self,
tenant_id: UUID,
status: ProjectStatus | None = None,
search: str | None = None,
limit: int = 20,
offset: int = 0,
) -> tuple[list[Project], int]:
query = self._db.query(Project).filter(Project.tenant_id == tenant_id)
if status:
query = query.filter(Project.status == status)
if search:
query = query.filter(Project.title.ilike(f"%{search}%"))
total = query.count()
items = query.order_by(Project.created_at.desc()).offset(offset).limit(limit).all()
return items, total
def update(self, project: Project, **kwargs) -> Project:
for key, value in kwargs.items():
if value is not None:
setattr(project, key, value)
self._db.commit()
self._db.refresh(project)
return project
def delete(self, project: Project) -> None:
self._db.delete(project)
self._db.commit()