Skip to content

Commit 56b1254

Browse files
Khushi YadavKhushi Yadav
authored andcommitted
changes done
1 parent eb8aa68 commit 56b1254

File tree

13 files changed

+303
-53
lines changed

13 files changed

+303
-53
lines changed

backend/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/.idea/backend.iml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/app/api/deps.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from collections.abc import Generator
22
from typing import Annotated
3+
from jose import jwt, JWTError
34

4-
import jwt
5+
#import jwt
56
from fastapi import Depends, HTTPException, status
67
from fastapi.security import OAuth2PasswordBearer
7-
from jwt.exceptions import InvalidTokenError
8+
#from jwt.exceptions import InvalidTokenError
89
from pydantic import ValidationError
910
from sqlmodel import Session
1011

@@ -36,7 +37,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
3637
token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
3738
)
3839
token_data = TokenPayload(**payload)
39-
except (InvalidTokenError, ValidationError):
40+
except (JWTError, ValidationError):
4041
raise HTTPException(
4142
status_code=status.HTTP_403_FORBIDDEN,
4243
detail="Could not validate credentials",

backend/app/api/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from fastapi import APIRouter
22

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

66
api_router = APIRouter()
77
api_router.include_router(login.router)
88
api_router.include_router(users.router)
99
api_router.include_router(utils.router)
1010
api_router.include_router(items.router)
11+
api_router.include_router(organizations.router)
1112

1213

1314
if settings.ENVIRONMENT == "local":

backend/app/api/routes/items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def read_items(
3636
items = session.exec(statement).all()
3737

3838
return ItemsPublic(data=items, count=count)
39-
39+
4040

4141
@router.get("/{id}", response_model=ItemPublic)
4242
def read_item(session: SessionDep, current_user: CurrentUser, id: str) -> Any:
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from typing import Any
2+
from fastapi import APIRouter, HTTPException
3+
from sqlmodel import select, func
4+
5+
from app.api.deps import SessionDep, CurrentUser
6+
from app.models import (
7+
Organization,
8+
OrganizationCreate,
9+
OrganizationUpdate,
10+
OrganizationPublic,
11+
OrganizationsPublic,
12+
Message,
13+
14+
)
15+
16+
router = APIRouter(prefix="/organizations", tags=["organizations"])
17+
18+
19+
20+
# GET /organizations → List all organizations
21+
22+
@router.get("/", response_model=OrganizationsPublic)
23+
def read_organizations(
24+
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
25+
) -> Any:
26+
"""
27+
Retrieve organizations.
28+
- Superusers see all organizations.
29+
- Normal users see only the ones they own or belong to.
30+
"""
31+
32+
if current_user.is_superuser:
33+
count_statement = select(func.count()).select_from(Organization)
34+
count = session.exec(count_statement).one()
35+
statement = select(Organization).offset(skip).limit(limit)
36+
organizations = session.exec(statement).all()
37+
else:
38+
# assuming each org has an 'owner_id' field similar to items
39+
count_statement = (
40+
select(func.count())
41+
.select_from(Organization)
42+
.where(Organization.owner_id == current_user.id)
43+
)
44+
count = session.exec(count_statement).one()
45+
statement = (
46+
select(Organization)
47+
.where(Organization.owner_id == current_user.id)
48+
.offset(skip)
49+
.limit(limit)
50+
)
51+
organizations = session.exec(statement).all()
52+
53+
return OrganizationsPublic(data=organizations, count=count)
54+
55+
56+
57+
# GET /organizations/{id} → Get a specific organization
58+
59+
@router.get("/{id}", response_model=OrganizationPublic)
60+
def read_organization(
61+
session: SessionDep, current_user: CurrentUser, id: str
62+
) -> Any:
63+
"""
64+
Get organization by ID.
65+
"""
66+
org = session.get(Organization, id)
67+
if not org:
68+
raise HTTPException(status_code=404, detail="Organization not found")
69+
if not current_user.is_superuser and (org.owner_id != current_user.id):
70+
raise HTTPException(status_code=400, detail="Not enough permissions")
71+
return org
72+
73+
74+
75+
# POST /organizations → Create a new organization
76+
77+
@router.post("/", response_model=OrganizationPublic)
78+
def create_organization(
79+
*, session: SessionDep, current_user: CurrentUser, org_in: OrganizationCreate
80+
) -> Any:
81+
"""
82+
Create a new organization.
83+
"""
84+
db_org = Organization.model_validate(org_in, update={"owner_id": current_user.id})
85+
session.add(db_org)
86+
session.commit()
87+
session.refresh(db_org)
88+
return db_org
89+
90+
91+
92+
# PUT /organizations/{id} → Update an organization
93+
94+
@router.put("/{id}", response_model=OrganizationPublic)
95+
def update_organization(
96+
*,
97+
session: SessionDep,
98+
current_user: CurrentUser,
99+
id: str,
100+
org_in: OrganizationUpdate,
101+
) -> Any:
102+
"""
103+
Update an organization.
104+
"""
105+
org = session.get(Organization, id)
106+
if not org:
107+
raise HTTPException(status_code=404, detail="Organization not found")
108+
if not current_user.is_superuser and (org.owner_id != current_user.id):
109+
raise HTTPException(status_code=400, detail="Not enough permissions")
110+
111+
update_data = org_in.model_dump(exclude_unset=True)
112+
org.sqlmodel_update(update_data)
113+
session.add(org)
114+
session.commit()
115+
session.refresh(org)
116+
return org
117+
118+
119+
120+
# DELETE /organizations/{id} → Delete an organization
121+
122+
@router.delete("/{id}", response_model=Message)
123+
def delete_organization(
124+
session: SessionDep, current_user: CurrentUser, id: str
125+
) -> Message:
126+
"""
127+
Delete an organization.
128+
"""
129+
org = session.get(Organization, id)
130+
if not org:
131+
raise HTTPException(status_code=404, detail="Organization not found")
132+
if not current_user.is_superuser and (org.owner_id != current_user.id):
133+
raise HTTPException(status_code=400, detail="Not enough permissions")
134+
135+
session.delete(org)
136+
session.commit()
137+
return Message(message="Organization deleted successfully")

0 commit comments

Comments
 (0)