Skip to content

Commit

Permalink
Merge pull request #2 from StreetLamb/frontend/graph-builder-page
Browse files Browse the repository at this point in the history
Add Graph Builder and Chat Capability
  • Loading branch information
StreetLamb authored Apr 22, 2024
2 parents f842887 + a82191a commit 638e454
Show file tree
Hide file tree
Showing 90 changed files with 6,118 additions and 662 deletions.
49 changes: 23 additions & 26 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug FastAPI Project backend: Python Debugger",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": [
"app.main:app",
"--reload"
],
"cwd": "${workspaceFolder}/backend",
"jinja": true,
"envFile": "${workspaceFolder}/.env",
},
{
"type": "chrome",
"request": "launch",
"name": "Debug Frontend: Launch Chrome against http://localhost:5173",
"url": "http://localhost:5173",
"webRoot": "${workspaceFolder}/frontend"
},
]
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug FastAPI Project backend: Python Debugger",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": ["app.main:app", "--reload"],
"cwd": "${workspaceFolder}/backend",
"jinja": true,
"envFile": "${workspaceFolder}/.env"
},
{
"type": "chrome",
"request": "launch",
"name": "Debug Frontend: Launch Chrome against http://localhost:5173",
"url": "http://localhost:5173",
"webRoot": "${workspaceFolder}/frontend"
}
]
}
5 changes: 4 additions & 1 deletion backend/app/api/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from fastapi import APIRouter

from app.api.routes import items, login, users, utils, teams
from app.api.routes import items, login, members, teams, users, utils

api_router = APIRouter()
api_router.include_router(login.router, tags=["login"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
api_router.include_router(items.router, prefix="/items", tags=["items"])
api_router.include_router(teams.router, prefix="/teams", tags=["teams"])
api_router.include_router(
members.router, prefix="/teams/{team_id}/members", tags=["members"]
)
205 changes: 205 additions & 0 deletions backend/app/api/routes/members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
from typing import Any

from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import func, select

from app.api.deps import CurrentUser, SessionDep
from app.models import (
Member,
MemberCreate,
MemberOut,
MembersOut,
MemberUpdate,
Message,
Team,
)

router = APIRouter()


def validate_unique_name_in_team(
session: SessionDep, team_id: int, id: int, member_in: MemberCreate | MemberUpdate
):
"""Check if (name, team_id) is unique"""
if member_in.name is None:
return
statement = select(Member).where(
Member.name == member_in.name,
Member.belongs_to == team_id,
Member.id != id,
)
member_unique = session.exec(statement).first()
if member_unique:
raise HTTPException(
status_code=400, detail="Member with this name already exists"
)


@router.get("/", response_model=MembersOut)
def read_members(
session: SessionDep,
current_user: CurrentUser,
team_id: int,
skip: int = 0,
limit: int = 100,
) -> Any:
"""
Retrieve members from team.
"""
# TODO: Use new way of getting members from teams. Get team first then use team.members
if current_user.is_superuser:
count_statement = select(func.count()).select_from(Member)
count = session.exec(count_statement).one()
statement = (
select(Member).where(Member.belongs_to == team_id).offset(skip).limit(limit)
)
members = session.exec(statement).all()
else:
count_statement = (
select(func.count())
.select_from(Member)
.join(Team)
.where(Team.owner_id == current_user.id, Member.belongs_to == team_id)
)
count = session.exec(count_statement).one()
statement = (
select(Member)
.join(Team)
.where(Team.owner_id == current_user.id, Member.belongs_to == team_id)
.offset(skip)
.limit(limit)
)
members = session.exec(statement).all()

return MembersOut(data=members, count=count)


@router.get("/{id}", response_model=MemberOut)
def read_member(
session: SessionDep, current_user: CurrentUser, team_id: int, id: int
) -> Any:
"""
Get member by ID.
"""
if current_user.is_superuser:
statement = (
select(Member)
.join(Team)
.where(Member.id == id, Member.belongs_to == team_id)
)
member = session.exec(statement).first()
else:
statement = (
select(Member)
.join(Team)
.where(
Member.id == id,
Member.belongs_to == team_id,
Team.owner_id == current_user.id,
)
)
member = session.exec(statement).first()

if not member:
raise HTTPException(status_code=404, detail="Member not found")
return member


@router.post("/", response_model=MemberOut)
def create_member(
*,
session: SessionDep,
current_user: CurrentUser,
team_id: int,
member_in: MemberCreate,
_: bool = Depends(validate_unique_name_in_team),
) -> Any:
"""
Create new member.
"""
if not current_user.is_superuser:
team = session.get(Team, team_id)
if team.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
member = Member.model_validate(member_in, update={"belongs_to": team_id})
session.add(member)
session.commit()
session.refresh(member)
return member


@router.put("/{id}", response_model=MemberOut)
def update_member(
*,
session: SessionDep,
current_user: CurrentUser,
team_id: int,
id: int,
member_in: MemberUpdate,
_: bool = Depends(validate_unique_name_in_team),
) -> Any:
"""
Update a member.
"""
if current_user.is_superuser:
statement = (
select(Member)
.join(Team)
.where(Member.id == id, Member.belongs_to == team_id)
)
member = session.exec(statement).first()
else:
statement = (
select(Member)
.join(Team)
.where(
Member.id == id,
Member.belongs_to == team_id,
Team.owner_id == current_user.id,
)
)
member = session.exec(statement).first()

if not member:
raise HTTPException(status_code=404, detail="Member not found")

update_dict = member_in.model_dump(exclude_unset=True)
member.sqlmodel_update(update_dict)
session.add(member)
session.commit()
session.refresh(member)
return member


@router.delete("/{id}")
def delete_member(
session: SessionDep, current_user: CurrentUser, team_id: int, id: int
) -> Message:
"""
Delete a member.
"""
if current_user.is_superuser:
statement = (
select(Member)
.join(Team)
.where(Member.id == id, Member.belongs_to == team_id)
)
member = session.exec(statement).first()
else:
statement = (
select(Member)
.join(Team)
.where(
Member.id == id,
Member.belongs_to == team_id,
Team.owner_id == current_user.id,
)
)
member = session.exec(statement).first()

if not member:
raise HTTPException(status_code=404, detail="Member not found")

session.delete(member)
session.commit()
return Message(message="Member deleted successfully")
Loading

0 comments on commit 638e454

Please sign in to comment.