brands-in-shops feature implemented

This commit is contained in:
2025-05-27 23:41:04 +02:00
parent 7037be370e
commit 2846bcbb1c
9 changed files with 559 additions and 36 deletions

View File

@@ -272,6 +272,75 @@ def delete_brand(brand_id: int, db: Session = Depends(get_db)):
db.commit()
return {"message": "Brand deleted successfully"}
# BrandInShop endpoints
@app.post("/brands-in-shops/", response_model=schemas.BrandInShop)
def create_brand_in_shop(brand_in_shop: schemas.BrandInShopCreate, db: Session = Depends(get_db)):
# Validate shop exists
shop = db.query(models.Shop).filter(models.Shop.id == brand_in_shop.shop_id).first()
if shop is None:
raise HTTPException(status_code=404, detail="Shop not found")
# Validate brand exists
brand = db.query(models.Brand).filter(models.Brand.id == brand_in_shop.brand_id).first()
if brand is None:
raise HTTPException(status_code=404, detail="Brand not found")
# Check if this combination already exists
existing = db.query(models.BrandInShop).filter(
models.BrandInShop.shop_id == brand_in_shop.shop_id,
models.BrandInShop.brand_id == brand_in_shop.brand_id
).first()
if existing:
raise HTTPException(status_code=400, detail="This brand is already associated with this shop")
db_brand_in_shop = models.BrandInShop(**brand_in_shop.dict())
db.add(db_brand_in_shop)
db.commit()
db.refresh(db_brand_in_shop)
return db_brand_in_shop
@app.get("/brands-in-shops/", response_model=List[schemas.BrandInShop])
def read_brands_in_shops(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
brands_in_shops = db.query(models.BrandInShop).offset(skip).limit(limit).all()
return brands_in_shops
@app.get("/brands-in-shops/shop/{shop_id}", response_model=List[schemas.BrandInShop])
def read_brands_in_shop(shop_id: int, db: Session = Depends(get_db)):
# Validate shop exists
shop = db.query(models.Shop).filter(models.Shop.id == shop_id).first()
if shop is None:
raise HTTPException(status_code=404, detail="Shop not found")
brands_in_shop = db.query(models.BrandInShop).filter(models.BrandInShop.shop_id == shop_id).all()
return brands_in_shop
@app.get("/brands-in-shops/brand/{brand_id}", response_model=List[schemas.BrandInShop])
def read_shops_with_brand(brand_id: int, db: Session = Depends(get_db)):
# Validate brand exists
brand = db.query(models.Brand).filter(models.Brand.id == brand_id).first()
if brand is None:
raise HTTPException(status_code=404, detail="Brand not found")
shops_with_brand = db.query(models.BrandInShop).filter(models.BrandInShop.brand_id == brand_id).all()
return shops_with_brand
@app.get("/brands-in-shops/{brand_in_shop_id}", response_model=schemas.BrandInShop)
def read_brand_in_shop(brand_in_shop_id: int, db: Session = Depends(get_db)):
brand_in_shop = db.query(models.BrandInShop).filter(models.BrandInShop.id == brand_in_shop_id).first()
if brand_in_shop is None:
raise HTTPException(status_code=404, detail="Brand in shop association not found")
return brand_in_shop
@app.delete("/brands-in-shops/{brand_in_shop_id}")
def delete_brand_in_shop(brand_in_shop_id: int, db: Session = Depends(get_db)):
brand_in_shop = db.query(models.BrandInShop).filter(models.BrandInShop.id == brand_in_shop_id).first()
if brand_in_shop is None:
raise HTTPException(status_code=404, detail="Brand in shop association not found")
db.delete(brand_in_shop)
db.commit()
return {"message": "Brand in shop association deleted successfully"}
# Grocery Category endpoints
@app.post("/grocery-categories/", response_model=schemas.GroceryCategory)
def create_grocery_category(category: schemas.GroceryCategoryCreate, db: Session = Depends(get_db)):