-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (46 loc) · 1.95 KB
/
main.py
File metadata and controls
61 lines (46 loc) · 1.95 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
60
61
from fastapi import FastAPI, HTTPException
from schemas import LeagueCreate
app = FastAPI()
leagues = [
{
"id": 1,
"name": "Premier League",
"country": "England",
"number_of_teams": 20
}
]
@app.get("/leagues", tags=["Leagues"], summary="Show all leagues")
async def get_leagues():
return leagues
@app.get("/leagues/{league_id}", tags=["Leagues"], summary="Show specific league")
async def get_league_by_id(league_id: int):
for league in leagues:
if league["id"] == league_id:
return league
raise HTTPException(status_code=404, detail="League not found")
@app.post("/leagues", tags=["Leagues"], summary="Add a new league")
async def add_league(new_league: LeagueCreate):
new_entry = {
"id": len(leagues) + 1,
"name": new_league.name,
"country": new_league.country,
"number_of_teams": new_league.number_of_teams
}
leagues.append(new_entry)
return {"message": "League added successfully", "league": new_entry}
@app.put("/leagues/{league_id}", tags=["Leagues"], summary="Update a league")
async def update_league(league_id: int, updated_league: LeagueCreate):
for league in leagues:
if league["id"] == league_id:
league["name"] = updated_league.name
league["country"] = updated_league.country
league["number_of_teams"] = updated_league.number_of_teams
return {"message": "League updated successfully", "league": league}
raise HTTPException(status_code=404, detail="League not found")
@app.delete("/leagues/{league_id}", tags=["Leagues"], summary="Delete a specific league")
async def delete_league(league_id: int):
for index, league in enumerate(leagues):
if league["id"] == league_id:
del leagues[index]
return {"message": f"League with id {league_id} deleted successfully"}
raise HTTPException(status_code=404, detail="League not found")