|
2 | 2 | from fastapi_sqlalchemy import db |
3 | 3 |
|
4 | 4 | from .models.button import ButtonCreate, ButtonUpdate, ButtonGet |
| 5 | +from .models.category import CategoryGet |
5 | 6 | from ..models.database import Button, Category |
6 | 7 |
|
7 | 8 | button = APIRouter() |
8 | 9 |
|
9 | 10 |
|
10 | 11 | @button.post("/", response_model=ButtonGet) |
11 | | -def create_button(button_inp: ButtonCreate): |
12 | | - category = db.session.query(Category).filter(Category.id == button_inp.category_id).one_or_none() |
| 12 | +def create_button(button_inp: ButtonCreate, category_id: int): |
| 13 | + category = db.session.query(Category).filter(Category.id == category_id).one_or_none() |
13 | 14 | if not category: |
14 | 15 | raise HTTPException(status_code=404, detail="Category does not exist") |
15 | | - button = Button(**button_inp.dict()) |
| 16 | + last_button = db.session.query(Button).order_by(Button.order.desc()).first() |
| 17 | + button = Button(**button_inp.dict(exclude_none=True)) |
| 18 | + button.category_id = category_id |
| 19 | + if last_button: |
| 20 | + button.order = last_button.order + 1 |
16 | 21 | db.session.add(button) |
17 | | - db.session.flush() |
| 22 | + db.session.commit() |
18 | 23 | return button |
19 | 24 |
|
20 | 25 |
|
21 | | -@button.get("/", response_model=list[ButtonGet]) |
22 | | -def get_buttons(offset: int = 0, limit: int = 100): |
23 | | - return db.session.query(Button).offset(offset).limit(limit).all() |
| 26 | +@button.get("/", response_model=CategoryGet) |
| 27 | +def get_buttons(category_id: int): |
| 28 | + category = db.session.query(Category).filter(Category.id == category_id).one_or_none() |
| 29 | + if not category: |
| 30 | + raise HTTPException(status_code=404, detail="Category does not exist") |
| 31 | + return category |
24 | 32 |
|
25 | 33 |
|
26 | 34 | @button.get("/{button_id}", response_model=ButtonGet) |
27 | | -def get_button(button_id: int): |
| 35 | +def get_button(button_id: int, category_id: int): |
| 36 | + category = db.session.query(Category).filter(Category.id == category_id).one_or_none() |
| 37 | + if not category: |
| 38 | + raise HTTPException(status_code=404, detail="Category does not exist") |
28 | 39 | button = db.session.query(Button).filter(Button.id == button_id).one_or_none() |
29 | 40 | if not button: |
30 | 41 | raise HTTPException(status_code=404, detail="Button does not exist") |
| 42 | + if button.category_id != category_id: |
| 43 | + raise HTTPException(status_code=404, detail="Button is not this category") |
31 | 44 | return button |
32 | 45 |
|
33 | 46 |
|
34 | 47 | @button.delete("/{button_id}", response_model=None) |
35 | | -def remove_button(button_id: int): |
| 48 | +def remove_button(button_id: int, category_id: int): |
| 49 | + category = db.session.query(Category).filter(Category.id == category_id).one_or_none() |
| 50 | + if not category: |
| 51 | + raise HTTPException(status_code=404, detail="Category does not exist") |
36 | 52 | button = db.session.query(Button).filter(Button.id == button_id).one_or_none() |
37 | 53 | if not button: |
38 | 54 | raise HTTPException(status_code=404, detail="Button does not exist") |
| 55 | + if button.category_id != category_id: |
| 56 | + raise HTTPException(status_code=404, detail="Button is not in this category") |
39 | 57 | db.session.delete(button) |
40 | | - db.session.flush() |
| 58 | + db.session.query(Button).filter(Button.order > button.order).update({"order": Button.order - 1}) |
| 59 | + db.session.commit() |
41 | 60 |
|
42 | 61 |
|
43 | | -@button.patch("/{button_id}", response_model=ButtonGet) |
44 | | -def update_button(button_inp: ButtonUpdate, button_id: int): |
45 | | - button = db.session.query(Button).filter(Button.id == button_id) |
46 | | - if not button.one_or_none(): |
| 62 | +@button.patch("/{button_id}", response_model=ButtonUpdate) |
| 63 | +def update_button(button_inp: ButtonUpdate, button_id: int, category_id: int): |
| 64 | + query = db.session.query(Button).filter(Button.id == button_id) |
| 65 | + button = query.one_or_none() |
| 66 | + last_button = ( |
| 67 | + db.session.query(Button).filter(Button.category_id == category_id).order_by(Button.order.desc()).first() |
| 68 | + ) |
| 69 | + category = db.session.query(Category).filter(Category.id == category_id).one_or_none() |
| 70 | + |
| 71 | + if not category: |
| 72 | + raise HTTPException(status_code=404, detail="Category does not exist") |
| 73 | + if not button: |
47 | 74 | raise HTTPException(status_code=404, detail="Button does not exist") |
48 | 75 | if not any(button_inp.dict().values()): |
49 | 76 | raise HTTPException(status_code=400, detail="Empty schema") |
50 | | - button.update( |
51 | | - button_inp.dict(exclude_unset=True) |
52 | | - ) |
53 | | - db.session.flush() |
54 | | - patched = button.one() |
55 | | - return patched |
| 77 | + if button.category_id != category_id: |
| 78 | + raise HTTPException(status_code=404, detail="Button is not this category") |
| 79 | + |
| 80 | + if button_inp.order: |
| 81 | + if last_button and (button_inp.order > last_button.order + 1): |
| 82 | + raise HTTPException( |
| 83 | + status_code=400, |
| 84 | + detail=f"Can`t create button with order {button_inp.order}. " f"Last category is {last_button.order}", |
| 85 | + ) |
| 86 | + if button_inp.order < 1: |
| 87 | + raise HTTPException(status_code=400, detail="Order can`t be less than 1") |
| 88 | + if button.order > button_inp.order: |
| 89 | + db.session.query(Button).filter(Button.order < button.order).update({"order": Button.order + 1}) |
| 90 | + elif button.order < button_inp.order: |
| 91 | + db.session.query(Button).filter(Button.order > button.order).update({"order": Button.order - 1}) |
| 92 | + |
| 93 | + query.update(button_inp.dict(exclude_unset=True, exclude_none=True)) |
| 94 | + db.session.commit() |
| 95 | + return button |
0 commit comments