Skip to content

Commit 7b33dfd

Browse files
updated production-run
1 parent fb9d098 commit 7b33dfd

File tree

6 files changed

+95
-62
lines changed

6 files changed

+95
-62
lines changed

src/database/connection.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ def __init__(self, model):
2525
self.model = model
2626

2727
async def save(self, document):
28-
await document.create()
29-
return
28+
doc = await document.create()
29+
if doc:
30+
return doc
31+
return False
3032

3133
async def get(self, id: PydanticObjectId) -> bool:
3234
doc = await self.model.get(id)

src/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
settings = Settings()
1111

12-
api.include_router(production_run_router)
12+
api.include_router(production_run_router, prefix="/productionrun")
1313

1414

1515
@api.on_event("startup")

src/models/production_run.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1+
from typing import Optional
12
from datetime import datetime
3+
24
from beanie import Document
35
from pydantic import BaseModel
46

57

68
class ProductionRun(Document):
7-
id: int
9+
pr_id: str
810
name: str
9-
dt: datetime = None
11+
description: Optional[str]
12+
started: datetime = None
1013

1114
class Config:
1215
schema_extra = {
1316
"example": {
14-
"id": 1,
15-
"name": "Example Schema!",
16-
"dt": '2022-12-23T10:20:30.400+02:00'
17+
"pr_id": "FA-01",
18+
"name": "1st production run",
19+
"description": "Testing the equipment!",
20+
"started": "2022-12-23T10:20:30.400+02:00"
1721
}
1822
}
1923

24+
class Settings:
25+
name = "production_runs"
2026

21-
class ProductionRunItem(BaseModel):
22-
name: str
27+
28+
class ProductionRunUpdate(BaseModel):
29+
name: Optional[str]
30+
description: Optional[str]
2331

2432
class Config:
2533
schema_extra = {
2634
"example": {
27-
"name": "Framing material"
35+
"name": "Framing material",
36+
"description": "Changed gears"
2837
}
2938
}

src/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ passlib==1.7.4
1212
pydantic==1.10.2
1313
pymongo==4.3.3
1414
python-dotenv==0.21.0
15+
pytz==2022.7
1516
sniffio==1.3.0
1617
starlette==0.22.0
1718
typing_extensions==4.4.0

src/routes/production_run.py

+50-51
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
1-
from fastapi import APIRouter, Path
1+
from typing import List
2+
from datetime import datetime
3+
import pytz
24

3-
from models.production_run import ProductionRun, ProductionRunItem
5+
from beanie import PydanticObjectId
6+
from database.connection import Database
7+
from fastapi import APIRouter, Path, HTTPException, status
8+
from models.production_run import ProductionRun, ProductionRunUpdate
49

5-
production_run_router = APIRouter()
10+
production_run_router = APIRouter(
11+
tags=["ProductionRun"]
12+
)
613

714
production_run_list = []
15+
production_run_database = Database(ProductionRun)
816

917

10-
@production_run_router.post("/productionrun")
11-
async def add_item(item: ProductionRun) -> dict:
12-
production_run_list.append(item)
13-
return {
14-
"message": "Production run added successfully."
15-
}
16-
17-
18-
@production_run_router.get("/productionrun")
19-
async def retrieve_item() -> dict:
20-
return {
21-
"productions_runs": production_run_list
22-
}
18+
@production_run_router.get("/", response_model=List[ProductionRun])
19+
async def retrieve_all_production_runs() -> List[ProductionRun]:
20+
production_runs = await production_run_database.get_all()
21+
return production_runs
2322

2423

25-
@production_run_router.get("/productionrun/{item_id}")
26-
async def get_single_item(item_id: int = Path(..., title="The ID of the item to retrieve.")) -> dict:
27-
for item in production_run_list:
28-
if item.id == item_id:
29-
return {
30-
"item": item
31-
}
32-
return {
33-
"message": "Item with supplied ID doesn't exist."
34-
}
24+
@production_run_router.get("/{item_id}", response_model=ProductionRun)
25+
async def retrieve_single_production_run(item_id: PydanticObjectId = Path(..., title="The ID of the item to retrieve.")) -> ProductionRun:
26+
production_run = await production_run_database.get(item_id)
27+
if not production_run:
28+
raise HTTPException(
29+
status_code=status.HTTP_404_NOT_FOUND,
30+
detail="Production run with supplied ID does not exist"
31+
)
32+
return production_run
3533

3634

37-
@production_run_router.put("/productionrun/{item_id}")
38-
async def update_item(item_data: ProductionRunItem, item_id: int = Path(..., title="The ID of the item to be updated.")) -> dict:
39-
for item in production_run_list:
40-
if item.id == item_id:
41-
item.name = item_data.name
42-
return {
43-
"message": "Item updated successfully."
44-
}
35+
@production_run_router.post("/new")
36+
async def create_production_run(body: ProductionRun) -> dict:
37+
body.started = datetime.now(pytz.timezone('Europe/Riga'))
38+
new_production_run = await production_run_database.save(body)
4539
return {
46-
"message": "Item with supplied ID doesn't exist."
40+
"message": "Production run created successfully.",
41+
"pr": new_production_run
4742
}
4843

4944

50-
@production_run_router.delete("/productionrun/{item_id}")
51-
async def delete_single_item(item_id: int) -> dict:
52-
for index in range(len(production_run_list)):
53-
item = production_run_list[index]
54-
if item.id == item_id:
55-
production_run_list.pop(index)
56-
return {
57-
"message": "Item deleted successfully."
58-
}
59-
return {
60-
"message": "Item with supplied ID doesn't exist."
61-
}
45+
@production_run_router.put("/{item_id}", response_model=ProductionRun)
46+
async def update_production_run(item_data: ProductionRunUpdate,
47+
item_id: PydanticObjectId = Path(..., title="The ID of the item to be updated.")) -> ProductionRun:
48+
updated_production_run = await production_run_database.update(item_id, item_data)
49+
if not updated_production_run:
50+
raise HTTPException(
51+
status_code=status.HTTP_404_NOT_FOUND,
52+
detail="Production run with supplied ID does not exist"
53+
)
54+
return updated_production_run
6255

6356

64-
@production_run_router.delete("/productionrun")
65-
async def delete_all_items() -> dict:
66-
production_run_list.clear()
57+
@production_run_router.delete("/{item_id}")
58+
async def delete_single_production_run(item_id: PydanticObjectId = Path(..., title="The ID of the item to be deleted.")) -> dict:
59+
production_run = await production_run_database.get(item_id)
60+
if not production_run:
61+
raise HTTPException(
62+
status_code=status.HTTP_404_NOT_FOUND,
63+
detail="Production run with supplied ID does not exist"
64+
)
65+
await production_run_database.delete(item_id)
6766
return {
68-
"message": "Items deleted successfully."
67+
"message": "Production run deleted successfully."
6968
}

src/test_main.http

+22
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,25 @@ GET http://127.0.0.1:8000/
44
Accept: application/json
55

66
###
7+
8+
GET http://127.0.0.1:8000/productionrun
9+
Accept: application/json
10+
11+
###
12+
13+
POST http://127.0.0.1:8000/productionrun/new
14+
Content-Type: application/json
15+
16+
{
17+
"pr_id": "FA-01",
18+
"name": "1st production run",
19+
"description": "Testing the equipment!"
20+
}
21+
22+
###
23+
24+
GET http://127.0.0.1:8000/productionrun
25+
Accept: application/json
26+
27+
###
28+

0 commit comments

Comments
 (0)