Skip to content

Commit b50d09e

Browse files
committed
Made corrections to backend code
1 parent 6f67502 commit b50d09e

File tree

10 files changed

+214
-177
lines changed

10 files changed

+214
-177
lines changed

.github/workflows/playwright.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,7 @@ jobs:
7575
- run: docker compose down -v --remove-orphans
7676
- name: Run Playwright tests
7777
run: docker compose run --rm playwright npx playwright test --fail-on-flaky-tests --trace=retain-on-failure --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
78-
env:
79-
# Required environment variables for Docker Compose
80-
FIRST_SUPERUSER: [email protected]
81-
FIRST_SUPERUSER_PASSWORD: changethis
82-
SECRET_KEY: changethis
83-
PROJECT_NAME: FastAPI Project
84-
POSTGRES_SERVER: db
85-
POSTGRES_PORT: 5432
86-
POSTGRES_USER: postgres
87-
POSTGRES_PASSWORD: changethis
88-
POSTGRES_DB: app
89-
ENVIRONMENT: test
78+
9079
- run: docker compose down -v --remove-orphans
9180
- name: Upload blob report to GitHub Actions Artifacts
9281
if: ${{ !cancelled() }}

.github/workflows/test-backend.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ jobs:
3232
- name: Run tests
3333
run: uv run bash scripts/tests-start.sh "Coverage for ${{ github.sha }}"
3434
working-directory: backend
35-
env:
36-
# Required environment variables for tests (using dummy values for CI)
37-
FIRST_SUPERUSER: [email protected]
38-
FIRST_SUPERUSER_PASSWORD: changethis
39-
SECRET_KEY: changethis
40-
PROJECT_NAME: FastAPI Project
41-
POSTGRES_SERVER: db
42-
POSTGRES_PORT: 5432
43-
POSTGRES_USER: postgres
44-
POSTGRES_PASSWORD: changethis
45-
POSTGRES_DB: app
46-
ENVIRONMENT: test
4735
- run: docker compose down -v --remove-orphans
4836
- name: Store coverage files
4937
uses: actions/upload-artifact@v4

backend/app/api/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fastapi import APIRouter
22

3-
from app.api.routes import items, login, private, users, utils, projects, galleries
3+
from app.api.routes import galleries, items, login, private, projects, users, utils
44
from app.core.config import settings
55

66
api_router = APIRouter()
@@ -14,4 +14,3 @@
1414

1515
if settings.ENVIRONMENT == "local":
1616
api_router.include_router(private.router)
17-

backend/app/api/routes/galleries.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
from typing import Any
33

44
from fastapi import APIRouter, HTTPException
5-
from sqlmodel import func, select
65

76
from app import crud
87
from app.api.deps import CurrentUser, SessionDep
98
from app.models import (
10-
Message,
11-
Gallery,
9+
GalleriesPublic,
1210
GalleryCreate,
1311
GalleryPublic,
14-
GalleriesPublic,
1512
GalleryUpdate,
16-
Project,
13+
Message,
1714
)
1815

1916
router = APIRouter()
@@ -32,26 +29,31 @@ def read_galleries(
3229
Otherwise, get all galleries for the user's organization.
3330
"""
3431
if not current_user.organization_id:
35-
raise HTTPException(status_code=400, detail="User is not part of an organization")
36-
32+
raise HTTPException(
33+
status_code=400, detail="User is not part of an organization"
34+
)
35+
3736
if project_id:
3837
# Verify project belongs to user's organization
3938
project = crud.get_project(session=session, project_id=project_id)
4039
if not project or project.organization_id != current_user.organization_id:
4140
raise HTTPException(status_code=403, detail="Not enough permissions")
42-
41+
4342
galleries = crud.get_galleries_by_project(
4443
session=session, project_id=project_id, skip=skip, limit=limit
4544
)
4645
count = len(galleries) # Simple count for project galleries
4746
else:
4847
galleries = crud.get_galleries_by_organization(
49-
session=session, organization_id=current_user.organization_id, skip=skip, limit=limit
48+
session=session,
49+
organization_id=current_user.organization_id,
50+
skip=skip,
51+
limit=limit,
5052
)
5153
count = crud.count_galleries_by_organization(
5254
session=session, organization_id=current_user.organization_id
5355
)
54-
56+
5557
return GalleriesPublic(data=galleries, count=count)
5658

5759

@@ -63,13 +65,15 @@ def create_gallery(
6365
Create new gallery.
6466
"""
6567
if not current_user.organization_id:
66-
raise HTTPException(status_code=400, detail="User is not part of an organization")
67-
68+
raise HTTPException(
69+
status_code=400, detail="User is not part of an organization"
70+
)
71+
6872
# Verify project belongs to user's organization
6973
project = crud.get_project(session=session, project_id=gallery_in.project_id)
7074
if not project or project.organization_id != current_user.organization_id:
7175
raise HTTPException(status_code=403, detail="Not enough permissions")
72-
76+
7377
gallery = crud.create_gallery(session=session, gallery_in=gallery_in)
7478
return gallery
7579

@@ -82,12 +86,12 @@ def read_gallery(session: SessionDep, current_user: CurrentUser, id: uuid.UUID)
8286
gallery = crud.get_gallery(session=session, gallery_id=id)
8387
if not gallery:
8488
raise HTTPException(status_code=404, detail="Gallery not found")
85-
89+
8690
# Check if gallery's project belongs to user's organization
8791
project = crud.get_project(session=session, project_id=gallery.project_id)
8892
if not project or project.organization_id != current_user.organization_id:
8993
raise HTTPException(status_code=403, detail="Not enough permissions")
90-
94+
9195
return gallery
9296

9397

@@ -105,13 +109,15 @@ def update_gallery(
105109
gallery = crud.get_gallery(session=session, gallery_id=id)
106110
if not gallery:
107111
raise HTTPException(status_code=404, detail="Gallery not found")
108-
112+
109113
# Check if gallery's project belongs to user's organization
110114
project = crud.get_project(session=session, project_id=gallery.project_id)
111115
if not project or project.organization_id != current_user.organization_id:
112116
raise HTTPException(status_code=403, detail="Not enough permissions")
113-
114-
gallery = crud.update_gallery(session=session, db_gallery=gallery, gallery_in=gallery_in)
117+
118+
gallery = crud.update_gallery(
119+
session=session, db_gallery=gallery, gallery_in=gallery_in
120+
)
115121
return gallery
116122

117123

@@ -125,12 +131,11 @@ def delete_gallery(
125131
gallery = crud.get_gallery(session=session, gallery_id=id)
126132
if not gallery:
127133
raise HTTPException(status_code=404, detail="Gallery not found")
128-
134+
129135
# Check if gallery's project belongs to user's organization
130136
project = crud.get_project(session=session, project_id=gallery.project_id)
131137
if not project or project.organization_id != current_user.organization_id:
132138
raise HTTPException(status_code=403, detail="Not enough permissions")
133-
139+
134140
crud.delete_gallery(session=session, gallery_id=id)
135141
return Message(message="Gallery deleted successfully")
136-

backend/app/api/routes/projects.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
from typing import Any
33

44
from fastapi import APIRouter, HTTPException
5-
from sqlmodel import func, select
65

76
from app import crud
87
from app.api.deps import CurrentUser, SessionDep
98
from app.models import (
9+
DashboardStats,
1010
Message,
11-
Project,
1211
ProjectCreate,
1312
ProjectPublic,
1413
ProjectsPublic,
1514
ProjectUpdate,
16-
DashboardStats,
1715
)
1816

1917
router = APIRouter()
@@ -27,15 +25,20 @@ def read_projects(
2725
Retrieve projects for the current user's organization.
2826
"""
2927
if not current_user.organization_id:
30-
raise HTTPException(status_code=400, detail="User is not part of an organization")
31-
28+
raise HTTPException(
29+
status_code=400, detail="User is not part of an organization"
30+
)
31+
3232
projects = crud.get_projects_by_organization(
33-
session=session, organization_id=current_user.organization_id, skip=skip, limit=limit
33+
session=session,
34+
organization_id=current_user.organization_id,
35+
skip=skip,
36+
limit=limit,
3437
)
3538
count = crud.count_projects_by_organization(
3639
session=session, organization_id=current_user.organization_id
3740
)
38-
41+
3942
return ProjectsPublic(data=projects, count=count)
4043

4144

@@ -45,9 +48,13 @@ def read_dashboard_stats(session: SessionDep, current_user: CurrentUser) -> Any:
4548
Get dashboard statistics for the current user's organization.
4649
"""
4750
if not current_user.organization_id:
48-
raise HTTPException(status_code=400, detail="User is not part of an organization")
49-
50-
return crud.get_dashboard_stats(session=session, organization_id=current_user.organization_id)
51+
raise HTTPException(
52+
status_code=400, detail="User is not part of an organization"
53+
)
54+
55+
return crud.get_dashboard_stats(
56+
session=session, organization_id=current_user.organization_id
57+
)
5158

5259

5360
@router.post("/", response_model=ProjectPublic)
@@ -58,12 +65,14 @@ def create_project(
5865
Create new project.
5966
"""
6067
if not current_user.organization_id:
61-
raise HTTPException(status_code=400, detail="User is not part of an organization")
62-
68+
raise HTTPException(
69+
status_code=400, detail="User is not part of an organization"
70+
)
71+
6372
# Ensure the project is being created for the user's organization
6473
if project_in.organization_id != current_user.organization_id:
6574
raise HTTPException(status_code=403, detail="Not enough permissions")
66-
75+
6776
project = crud.create_project(session=session, project_in=project_in)
6877
return project
6978

@@ -76,11 +85,11 @@ def read_project(session: SessionDep, current_user: CurrentUser, id: uuid.UUID)
7685
project = crud.get_project(session=session, project_id=id)
7786
if not project:
7887
raise HTTPException(status_code=404, detail="Project not found")
79-
88+
8089
# Check if project belongs to user's organization
8190
if project.organization_id != current_user.organization_id:
8291
raise HTTPException(status_code=403, detail="Not enough permissions")
83-
92+
8493
return project
8594

8695

@@ -98,12 +107,14 @@ def update_project(
98107
project = crud.get_project(session=session, project_id=id)
99108
if not project:
100109
raise HTTPException(status_code=404, detail="Project not found")
101-
110+
102111
# Check if project belongs to user's organization
103112
if project.organization_id != current_user.organization_id:
104113
raise HTTPException(status_code=403, detail="Not enough permissions")
105-
106-
project = crud.update_project(session=session, db_project=project, project_in=project_in)
114+
115+
project = crud.update_project(
116+
session=session, db_project=project, project_in=project_in
117+
)
107118
return project
108119

109120

@@ -117,11 +128,10 @@ def delete_project(
117128
project = crud.get_project(session=session, project_id=id)
118129
if not project:
119130
raise HTTPException(status_code=404, detail="Project not found")
120-
131+
121132
# Check if project belongs to user's organization
122133
if project.organization_id != current_user.organization_id:
123134
raise HTTPException(status_code=403, detail="Not enough permissions")
124-
135+
125136
crud.delete_project(session=session, project_id=id)
126137
return Message(message="Project deleted successfully")
127-

backend/app/api/routes/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from datetime import datetime
21
import platform
32
import sys
3+
from datetime import datetime
4+
from typing import Any
5+
46
from fastapi import APIRouter, Depends
57
from pydantic.networks import EmailStr
68

@@ -35,7 +37,7 @@ async def health_check() -> bool:
3537

3638

3739
@router.get("/system-info/")
38-
async def get_system_info() -> dict:
40+
async def get_system_info() -> dict[str, Any]:
3941
"""
4042
Get interesting system information including current time, platform details, and Python version.
4143
"""
@@ -55,7 +57,7 @@ async def get_system_info() -> dict:
5557
"major": sys.version_info.major,
5658
"minor": sys.version_info.minor,
5759
"micro": sys.version_info.micro,
58-
}
60+
},
5961
},
60-
"fun_fact": "This API endpoint was created as part of CS4800 team project exercise!"
62+
"fun_fact": "This API endpoint was created as part of CS4800 team project exercise!",
6163
}

backend/app/core/db.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from app import crud
44
from app.core.config import settings
5-
from app.models import User, UserCreate, Organization, OrganizationCreate
5+
from app.models import Organization, OrganizationCreate, User, UserCreate
66

77
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
88

@@ -27,10 +27,11 @@ def init_db(session: Session) -> None:
2727
).first()
2828
if not organization:
2929
organization_in = OrganizationCreate(
30-
name="Default Organization",
31-
description="Initial organization for Mosaic"
30+
name="Default Organization", description="Initial organization for Mosaic"
31+
)
32+
organization = crud.create_organization(
33+
session=session, organization_in=organization_in
3234
)
33-
organization = crud.create_organization(session=session, organization_in=organization_in)
3435

3536
user = session.exec(
3637
select(User).where(User.email == settings.FIRST_SUPERUSER)
@@ -42,7 +43,7 @@ def init_db(session: Session) -> None:
4243
is_superuser=True,
4344
)
4445
user = crud.create_user(session=session, user_create=user_in)
45-
46+
4647
# Assign user to default organization if not already assigned
4748
if user and not user.organization_id:
4849
user.organization_id = organization.id

0 commit comments

Comments
 (0)