-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (53 loc) Β· 1.25 KB
/
main.py
File metadata and controls
67 lines (53 loc) Β· 1.25 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
from fastapi import FastAPI
from lstore.db import Database
from lstore.query import Query
from pydantic import BaseModel
from fastapi.responses import JSONResponse
app = FastAPI()
db = Database()
scores_table = db.create_table('Scores', 5, 0)
query = Query(scores_table)
class Scores(BaseModel):
student_id: int
score_1: int
score_2: int
score_3: int
score_4: int
"""
Here is how you can access a score
```sh
curl "http://127.0.0.1:8000/values/1"
```
"""
@app.get("/values/{value_id}")
def read_value(value_id: int):
res = query.select(value_id, 0, [1] * 5)
if len(res) > 0:
val = res[0]
if val is not None:
return JSONResponse(content=val.columns)
return JSONResponse(content={})
"""
Here is how you can add data using curl.
```sh
curl -X PUT "http://127.0.0.1:8000/values"
-H "Content-Type: application/json"
-d '{
"student_id": 100200300,
"score_1": 94,
"score_2": 91,
"score_3": 98,
"score_4": 94
}'
```
"""
@app.put("/values")
def update_item(values: Scores):
ret = query.insert(
values.student_id,
values.score_1,
values.score_2,
values.score_3,
values.score_4,
)
return JSONResponse(content=ret)