|
1 |
| -from fastapi import APIRouter, Path |
| 1 | +from typing import List |
| 2 | +from datetime import datetime |
| 3 | +import pytz |
2 | 4 |
|
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 |
4 | 9 |
|
5 |
| -production_run_router = APIRouter() |
| 10 | +production_run_router = APIRouter( |
| 11 | + tags=["ProductionRun"] |
| 12 | +) |
6 | 13 |
|
7 | 14 | production_run_list = []
|
| 15 | +production_run_database = Database(ProductionRun) |
8 | 16 |
|
9 | 17 |
|
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 |
23 | 22 |
|
24 | 23 |
|
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 |
35 | 33 |
|
36 | 34 |
|
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) |
45 | 39 | return {
|
46 |
| - "message": "Item with supplied ID doesn't exist." |
| 40 | + "message": "Production run created successfully.", |
| 41 | + "pr": new_production_run |
47 | 42 | }
|
48 | 43 |
|
49 | 44 |
|
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 |
62 | 55 |
|
63 | 56 |
|
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) |
67 | 66 | return {
|
68 |
| - "message": "Items deleted successfully." |
| 67 | + "message": "Production run deleted successfully." |
69 | 68 | }
|
0 commit comments