Skip to content

Commit 52dc5c4

Browse files
committed
Created REST routers: AnotherKind, MyKind
1 parent 2d5b610 commit 52dc5c4

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed

api/routers/AnotherKind.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from fastapi import APIRouter, HTTPException, Depends
2+
from typing import *
3+
from uuid import uuid4
4+
from sqlalchemy import insert, select, update, delete
5+
from sqlalchemy.orm import Session
6+
from sqlalchemy.exc import SQLAlchemyError
7+
from api.models.AnotherKind import *
8+
from api.database import Base, engine, get_session
9+
from api.db_models import Apps, Status
10+
11+
router = APIRouter(
12+
prefix="/AnotherKind",
13+
tags=["AnotherKind"]
14+
)
15+
16+
@router.post("")
17+
def create(item: AnotherKind, db: Session = Depends(get_session)):
18+
try:
19+
json = item.dict()
20+
if json['kind'] != "AnotherKind":
21+
raise HTTPException(status_code=400, detail="Неподходящий тип документа (kind)")
22+
values = {
23+
'UUID': str(uuid4()),
24+
'kind': json['kind'],
25+
'name': json['name'],
26+
'version': json['version'],
27+
'description': json['description'],
28+
'state': Status.new,
29+
'json': json
30+
}
31+
32+
stmt = insert(Apps).values(values)
33+
db.execute(stmt)
34+
db.commit()
35+
except SQLAlchemyError as e:
36+
db.rollback()
37+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
38+
return {"status": "success"}
39+
40+
41+
@router.put("/{uuid}/specification")
42+
def update_specification(uuid: str, specification: Dict[str, Any], db: Session = Depends(get_session)):
43+
try:
44+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "AnotherKind").first()
45+
if not obj:
46+
raise HTTPException(status_code=404, detail="Объект не найден")
47+
48+
updated_json = obj.json.copy()
49+
updated_json['configuration']['specification'] = specification
50+
51+
stmt = update(Apps).where(Apps.UUID == uuid).values(json=updated_json)
52+
db.execute(stmt)
53+
db.commit()
54+
except SQLAlchemyError as e:
55+
db.rollback()
56+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
57+
58+
return {"status": "success"}
59+
60+
@router.put("/{uuid}/settings")
61+
def update_settings(uuid: str, settings: Dict[str, Any], db: Session = Depends(get_session)):
62+
try:
63+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "AnotherKind").first()
64+
if not obj:
65+
raise HTTPException(status_code=404, detail="Объект не найден")
66+
67+
updated_json = obj.json.copy()
68+
updated_json['configuration']['settings'] = settings
69+
70+
stmt = update(Apps).where(Apps.UUID == uuid).values(json=updated_json)
71+
db.execute(stmt)
72+
db.commit()
73+
except SQLAlchemyError as e:
74+
db.rollback()
75+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
76+
77+
return {"status": "success"}
78+
79+
80+
@router.put("/{uuid}/state")
81+
def update_state(uuid: str, state: Status, db: Session = Depends(get_session)):
82+
try:
83+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "AnotherKind").first()
84+
if not obj:
85+
raise HTTPException(status_code=404, detail="Объект не найден")
86+
87+
stmt = update(Apps).where(Apps.UUID == uuid).values(state=state)
88+
db.execute(stmt)
89+
db.commit()
90+
except SQLAlchemyError as e:
91+
db.rollback()
92+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
93+
94+
return {"status": "success"}
95+
96+
97+
@router.delete("/{uuid}")
98+
def delete(uuid: str, db: Session = Depends(get_session)):
99+
try:
100+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "AnotherKind").first()
101+
if not obj:
102+
raise HTTPException(status_code=404, detail="Объект не найден")
103+
104+
db.delete(obj)
105+
db.commit()
106+
except SQLAlchemyError as e:
107+
raise HTTPException(status_code=500, detail="Ошибка базы данных")
108+
return {"status": "success"}
109+
110+
111+
@router.get("/{uuid}")
112+
def read(uuid: str, db: Session = Depends(get_session)):
113+
try:
114+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "AnotherKind").first()
115+
if not obj:
116+
raise HTTPException(status_code=404, detail="Объект не найден")
117+
except SQLAlchemyError as e:
118+
raise HTTPException(status_code=500, detail="Ошибка базы данных")
119+
120+
return {"status": "success",
121+
"data": obj}
122+
123+
124+
@router.get("/{uuid}/state")
125+
def read_state(uuid: str, db: Session = Depends(get_session)):
126+
try:
127+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "AnotherKind").first()
128+
if not obj:
129+
raise HTTPException(status_code=404, detail="Объект не найден")
130+
except SQLAlchemyError as e:
131+
raise HTTPException(status_code=500, detail="Ошибка базы данных")
132+
133+
return {
134+
"status": "success",
135+
"data": {
136+
"state": obj.state
137+
}
138+
}

api/routers/MyKind.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from fastapi import APIRouter, HTTPException, Depends
2+
from typing import *
3+
from uuid import uuid4
4+
from sqlalchemy import insert, select, update, delete
5+
from sqlalchemy.orm import Session
6+
from sqlalchemy.exc import SQLAlchemyError
7+
from api.models.MyKind import *
8+
from api.database import Base, engine, get_session
9+
from api.db_models import Apps, Status
10+
11+
router = APIRouter(
12+
prefix="/MyKind",
13+
tags=["MyKind"]
14+
)
15+
16+
@router.post("")
17+
def create(item: MyKind, db: Session = Depends(get_session)):
18+
try:
19+
json = item.dict()
20+
if json['kind'] != "MyKind":
21+
raise HTTPException(status_code=400, detail="Неподходящий тип документа (kind)")
22+
values = {
23+
'UUID': str(uuid4()),
24+
'kind': json['kind'],
25+
'name': json['name'],
26+
'version': json['version'],
27+
'description': json['description'],
28+
'state': Status.new,
29+
'json': json
30+
}
31+
32+
stmt = insert(Apps).values(values)
33+
db.execute(stmt)
34+
db.commit()
35+
except SQLAlchemyError as e:
36+
db.rollback()
37+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
38+
return {"status": "success"}
39+
40+
41+
@router.put("/{uuid}/specification")
42+
def update_specification(uuid: str, specification: Dict[str, Any], db: Session = Depends(get_session)):
43+
try:
44+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "MyKind").first()
45+
if not obj:
46+
raise HTTPException(status_code=404, detail="Объект не найден")
47+
48+
updated_json = obj.json.copy()
49+
updated_json['configuration']['specification'] = specification
50+
51+
stmt = update(Apps).where(Apps.UUID == uuid).values(json=updated_json)
52+
db.execute(stmt)
53+
db.commit()
54+
except SQLAlchemyError as e:
55+
db.rollback()
56+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
57+
58+
return {"status": "success"}
59+
60+
@router.put("/{uuid}/settings")
61+
def update_settings(uuid: str, settings: Dict[str, Any], db: Session = Depends(get_session)):
62+
try:
63+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "MyKind").first()
64+
if not obj:
65+
raise HTTPException(status_code=404, detail="Объект не найден")
66+
67+
updated_json = obj.json.copy()
68+
updated_json['configuration']['settings'] = settings
69+
70+
stmt = update(Apps).where(Apps.UUID == uuid).values(json=updated_json)
71+
db.execute(stmt)
72+
db.commit()
73+
except SQLAlchemyError as e:
74+
db.rollback()
75+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
76+
77+
return {"status": "success"}
78+
79+
80+
@router.put("/{uuid}/state")
81+
def update_state(uuid: str, state: Status, db: Session = Depends(get_session)):
82+
try:
83+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "MyKind").first()
84+
if not obj:
85+
raise HTTPException(status_code=404, detail="Объект не найден")
86+
87+
stmt = update(Apps).where(Apps.UUID == uuid).values(state=state)
88+
db.execute(stmt)
89+
db.commit()
90+
except SQLAlchemyError as e:
91+
db.rollback()
92+
raise HTTPException(status_code=500, detail="Ошибка базы данных: " + str(e))
93+
94+
return {"status": "success"}
95+
96+
97+
@router.delete("/{uuid}")
98+
def delete(uuid: str, db: Session = Depends(get_session)):
99+
try:
100+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "MyKind").first()
101+
if not obj:
102+
raise HTTPException(status_code=404, detail="Объект не найден")
103+
104+
db.delete(obj)
105+
db.commit()
106+
except SQLAlchemyError as e:
107+
raise HTTPException(status_code=500, detail="Ошибка базы данных")
108+
return {"status": "success"}
109+
110+
111+
@router.get("/{uuid}")
112+
def read(uuid: str, db: Session = Depends(get_session)):
113+
try:
114+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "MyKind").first()
115+
if not obj:
116+
raise HTTPException(status_code=404, detail="Объект не найден")
117+
except SQLAlchemyError as e:
118+
raise HTTPException(status_code=500, detail="Ошибка базы данных")
119+
120+
return {"status": "success",
121+
"data": obj}
122+
123+
124+
@router.get("/{uuid}/state")
125+
def read_state(uuid: str, db: Session = Depends(get_session)):
126+
try:
127+
obj = db.query(Apps).filter(Apps.UUID == uuid, Apps.kind == "MyKind").first()
128+
if not obj:
129+
raise HTTPException(status_code=404, detail="Объект не найден")
130+
except SQLAlchemyError as e:
131+
raise HTTPException(status_code=500, detail="Ошибка базы данных")
132+
133+
return {
134+
"status": "success",
135+
"data": {
136+
"state": obj.state
137+
}
138+
}

0 commit comments

Comments
 (0)