-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd5a3ef
commit 95e70d4
Showing
6 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import uuid | ||
from datetime import datetime, timedelta | ||
|
||
from pydantic import UUID4 | ||
from sqlalchemy import literal | ||
from sqlalchemy.dialects.postgresql import insert | ||
from sqlalchemy.orm import Session | ||
|
||
from app.models import Inventory as InventoryModel | ||
|
||
from .schemas import Inventory | ||
|
||
|
||
def get_user_inventory(db: Session, user_id: str): | ||
return ( | ||
db.query(*InventoryModel.__table__.columns) | ||
.filter(InventoryModel.user_id == user_id) | ||
.all() | ||
) | ||
|
||
|
||
def upsert_user_inventory(db: Session, user_id: str, inventory: list[Inventory]): | ||
stmt = insert(InventoryModel).values( | ||
[ | ||
{ | ||
"created_at": datetime.utcnow(), | ||
"user_id": user_id, | ||
**entry.dict( | ||
exclude={ | ||
"created_at", | ||
"deleted_at", | ||
"updated_at", | ||
} | ||
), | ||
} | ||
for entry in inventory | ||
] | ||
) | ||
stmt = stmt.on_conflict_do_update( | ||
constraint="inventory_pkey", | ||
set_={ | ||
"updated_at": datetime.utcnow(), | ||
"brand_name": stmt.excluded.brand_name, | ||
"medicine_name": stmt.excluded.medicine_name, | ||
"dosage": stmt.excluded.dosage, | ||
"dosage_unit": stmt.excluded.dosage_unit, | ||
"stock": stmt.excluded.stock, | ||
"medication_type": stmt.excluded.medication_type, | ||
"description": stmt.excluded.description, | ||
}, | ||
) | ||
db.execute(stmt) | ||
db.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException | ||
from sqlalchemy.orm import Session | ||
|
||
import app.api.user.controller as user_controller | ||
from app.database import get_db | ||
|
||
from .controller import get_user_inventory, upsert_user_inventory | ||
from .schemas import Inventory, UpsertInventory | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/", response_model=dict[str, str]) | ||
async def upsert_inventory( | ||
data: UpsertInventory, db: Session = Depends(get_db) | ||
): | ||
db_user = user_controller.get_user_by_id(db, id=data.user_id) | ||
if not db_user: | ||
raise HTTPException(status_code=400, detail="Account not found.") | ||
|
||
upsert_user_inventory(db, db_user.id, data.inventory) | ||
|
||
return {"status": "ok"} | ||
|
||
|
||
@router.get("/", response_model=List[Inventory]) | ||
async def read_user_inventory(user_id: str, db: Session = Depends(get_db)): | ||
db_user = user_controller.get_user_by_id(db, id=user_id) | ||
if not db_user: | ||
raise HTTPException(status_code=400, detail="Account not found.") | ||
|
||
return get_user_inventory(db, user_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from datetime import datetime | ||
from typing import List, Optional | ||
|
||
import uuid | ||
|
||
from pydantic import BaseModel, UUID4, validator | ||
|
||
class Inventory(BaseModel): | ||
inventory_id: Optional[str] | ||
created_at: Optional[datetime] | ||
updated_at: Optional[datetime] | ||
deleted_at: Optional[datetime] | ||
brand_name: Optional[str] | ||
medicine_name: Optional[str] | ||
stock: Optional[str] | ||
dosage: float | ||
dosage_unit: int | ||
medication_type: int | ||
description: Optional[str] | ||
|
||
@validator('inventory_id', pre=True) | ||
def validate_inventory_id(cls, v): | ||
if not isinstance(v, str): | ||
if isinstance(v, uuid.UUID): | ||
return str(v) | ||
return v | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
class UpsertInventory(BaseModel): | ||
user_id: str | ||
inventory: list[Inventory] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters