This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
153 lines (120 loc) · 3.45 KB
/
api.py
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import json
from knowledge import *
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
"http://localhost:3000",
]
with open("data.json") as read_file:
data = json.load(read_file)
class Account(BaseModel):
id: int
name: str
exp: int
balance: int
class Project(BaseModel):
id: int
name: str
exp: int
minlvl: int
payment: int
description: str
jenis: str
waktu: int
tools: str
prototipe: str
requirement: int
class ProjectCreate(BaseModel):
name: str
description: str
jenis: str
waktu: int
tools: str
prototipe: str
requirement: int
class Course(BaseModel):
id: int
name: str
exp: int
difficulty: int
price: int
description: str
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Tes"}
@app.get("/account")
async def read_account():
if data['account']:
return data['account']
@app.get("/account/{account_id}")
async def read_account_by_id(account_id: int):
for account_data in data['account']:
if account_data['id'] == account_id:
return account_data
raise HTTPException(status_code=404, detail=f'Not Found')
@app.get("/projects")
async def read_projects():
if data['project']:
return data['project']
@app.get("/projects/{project_id}")
async def read_project_by_id(project_id: int):
for project_data in data['project']:
if project_data['id'] == project_id:
return project_data
raise HTTPException(status_code=404, detail=f'Not Found')
@app.post("/projects")
async def create_project(project: ProjectCreate):
id = 1
if len(data['project']) > 0:
id += data['project'][len(data['project'])-1]['id']
project_dict = project.dict()
# KBSHIT
engine = ProjectCost()
engine.reset(tipe=project_dict['jenis'], time=project_dict['waktu'], tech=project_dict['tools'],
prototipe=project_dict['prototipe'], n_req=project_dict['requirement'])
engine.run()
# print(EXP, MIN_LEVEL, PAYMENT)
res = {
"id": id,
"name": project_dict['name'],
"exp": engine.EXP, # KBS
"minlevel": engine.MIN_LEVEL, # KBS
"payment": engine.PAYMENT, # KBS
"description": project_dict['description'],
"jenis": project_dict['jenis'],
"waktu": project_dict['waktu'],
"tools": project_dict['tools'],
"prototipe": project_dict['prototipe'],
"requirement": project_dict['requirement']
}
data['project'].append(dict(res))
if res:
read_file.close()
with open("data.json", "w") as write_file:
json.dump(data, write_file)
write_file.close()
return res
raise HTTPException(status_code=400, detail=f'Bad request')
@app.get("/courses")
async def read_courses():
if data['course']:
return data['course']
@app.get("/courses/{course_id}")
async def read_course_by_id(course_id: int):
for course_data in data['course']:
if course_data['id'] == course_id:
return course_data
raise HTTPException(status_code=404, detail=f'Not Found')