| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | # api/routes/teams.py | 
					
						
							|  |  |  | from typing import Dict | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from fastapi import APIRouter, Depends, HTTPException | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | from ...datamodel import Team | 
					
						
							| 
									
										
										
										
											2025-03-06 10:52:42 -08:00
										 |  |  | from ...gallery.builder import create_default_gallery | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | from ..deps import get_db | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | router = APIRouter() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.get("/") | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | async def list_teams(user_id: str, db=Depends(get_db)) -> Dict: | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     """List all teams for a user""" | 
					
						
							|  |  |  |     response = db.get(Team, filters={"user_id": user_id}) | 
					
						
							| 
									
										
										
										
											2025-03-06 10:52:42 -08:00
										 |  |  |     if not response.data or len(response.data) == 0: | 
					
						
							|  |  |  |         default_gallery = create_default_gallery() | 
					
						
							|  |  |  |         default_team = Team(user_id=user_id, component=default_gallery.components.teams[0].model_dump()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         db.upsert(default_team) | 
					
						
							|  |  |  |         response = db.get(Team, filters={"user_id": user_id}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |     return {"status": True, "data": response.data} | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.get("/{team_id}") | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | async def get_team(team_id: int, user_id: str, db=Depends(get_db)) -> Dict: | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     """Get a specific team""" | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |     response = db.get(Team, filters={"id": team_id, "user_id": user_id}) | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     if not response.status or not response.data: | 
					
						
							|  |  |  |         raise HTTPException(status_code=404, detail="Team not found") | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |     return {"status": True, "data": response.data[0]} | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.post("/") | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | async def create_team(team: Team, db=Depends(get_db)) -> Dict: | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     """Create a new team""" | 
					
						
							|  |  |  |     response = db.upsert(team) | 
					
						
							|  |  |  |     if not response.status: | 
					
						
							|  |  |  |         raise HTTPException(status_code=400, detail=response.message) | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |     return {"status": True, "data": response.data} | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.delete("/{team_id}") | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  | async def delete_team(team_id: int, user_id: str, db=Depends(get_db)) -> Dict: | 
					
						
							| 
									
										
										
										
											2024-11-09 14:32:24 -08:00
										 |  |  |     """Delete a team""" | 
					
						
							| 
									
										
										
										
											2024-11-26 15:39:36 -08:00
										 |  |  |     db.delete(filters={"id": team_id, "user_id": user_id}, model_class=Team) | 
					
						
							|  |  |  |     return {"status": True, "message": "Team deleted successfully"} |