-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (127 loc) · 4.42 KB
/
main.py
File metadata and controls
173 lines (127 loc) · 4.42 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from fastapi import FastAPI, HTTPException, Depends, Request, Response
from pydantic import BaseModel
from typing import Optional, List, Any, Dict, Tuple, cast
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from fastapi.responses import JSONResponse
from db import models
import requests
from db.crud import upsert_user, get_user
from db.crud import get_team, create_team, delete_team
from db.crud import get_all_tms, get_tm_list_by_station
from db.crud import (
create_comment,
get_comment,
get_all_comments,
update_comment,
delete_comment,
)
from db.crud import (
create_temperature,
get_temperature,
get_all_temperatures,
update_temperature,
delete_temperature,
)
from db.database import SessionLocal, get_db
app = FastAPI()
origins = ["http://localhost:3000", "https://kauth.kakao.com", "https://kapi.kakao.com"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class TMList(BaseModel):
start_station: str
end_station: str
desired_departure: str
current_members: int
class TeamInfo(BaseModel):
start_station: str
end_station: str
start_time: str
current_population: int
member_info: List[int]
comments: Optional[str]
class Rating(BaseModel):
team_no: int
rating: float
# kakao login
class User(BaseModel):
access_token: str
#incldue
# 1 Landing Page
from typing import List
@app.get(
"/tm_list/{station}",
response_model=List[TMList],
description="Retrieve all TM lists related to the specified station",
)
async def get_tm_list(station: str, db: Session = Depends(get_db)):
if not station:
raise HTTPException(status_code=400, detail="Station must not be empty")
tm_list = get_tm_list_by_station(db, station=station)
if not tm_list:
raise HTTPException(status_code=404, detail="No TMList found for this station")
# Convert datetime objects to ISO format strings for dictionaries
for tm in tm_list:
tm["desired_departure"] = tm["desired_departure"].isoformat() if tm["desired_departure"] else None
return tm_list
# 2 Authentication
def get_kakao_user_info(access_token: str):
url = "https://kapi.kakao.com/v2/user/me"
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise HTTPException(status_code=400, detail="Could not retrieve user information")
return response.json() # returns user information as a dictionary
@app.get("/kakao")
def get_kakao_token(code):
url = "https://kauth.kakao.com/oauth/token"
payload = {
"grant_type": "authorization_code",
"client_id": "d679f25e59dbc97619baf1256489b449",
"redirect_uri": "http://localhost:3000",
"code": code,
}
response = requests.post(url, data=payload)
access_token = response.json().get('access_token', None)
if access_token is None:
raise HTTPException(status_code=400, detail="Could not retrieve access token")
return get_kakao_user_info(access_token)
# 3 CRUD Team
@app.get(
"/team/{team_no}",
response_model=TeamInfo,
description="Get specific team info by team id",
)
async def get_team_info(team_no: int, db: Session = Depends(get_db)):
team = get_team(db, team_id=team_no)
if team is None:
raise HTTPException(status_code=404, detail="Team not found")
return team
@app.delete("/team/{team_no}", description="Delete a specific team by team id")
async def delete_team(team_no: int, db: Session = Depends(get_db)):
team = get_team(db, team_id=team_no)
if team is None:
raise HTTPException(status_code=404, detail="Team not found")
delete_team(db, team_id=team_no)
return {"message": f"Team {team_no} deleted successfully"}
@app.put("/team/", response_model=TeamInfo, description="Create a new team")
async def put_team_info(team: TeamInfo, db: Session = Depends(get_db)):
new_team = create_team(db, team=team)
return new_team
# 4 Rating
@app.post("/rating/")
async def post_rating(rating: Rating):
# db: Session = Depends(get_db)
# 평점을 저장하는 코드를 여기에 작성합니다.
# create_rating(db, rating=rating)
return {"message": "Post request received", "rating": rating}
@app.get("/")
async def home():
return {"This is": "home"}