File tree 1 file changed +12
-1
lines changed
1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 1
1
from fastapi import APIRouter , BackgroundTasks , Depends , HTTPException
2
2
from sqlmodel import Session , select
3
3
from typing import List
4
- from src .models import MedicineRequest , User
4
+ from src .models import Medicine , MedicineRequest , User
5
5
from src .schemas import (
6
6
RequestCreate ,
7
7
RequestRead ,
@@ -21,6 +21,17 @@ def create_request(
21
21
session : Session = Depends (get_session ),
22
22
current_user : User = Depends (get_current_user ),
23
23
):
24
+ # Query the medicine using SQLModel's select
25
+ statement = select (Medicine ).where (Medicine .name == request .name )
26
+ result = session .exec (statement )
27
+ medicine = result .first ()
28
+
29
+ if not medicine :
30
+ raise HTTPException (status_code = 404 , detail = "Medicine not found." )
31
+
32
+ if request .quantity > medicine .quantity :
33
+ raise HTTPException (status_code = 400 , detail = "Not enough quantity available." )
34
+
24
35
db_request = MedicineRequest (** request .dict ())
25
36
session .add (db_request )
26
37
session .commit ()
You can’t perform that action at this time.
0 commit comments