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)):

View File

@@ -17,6 +17,19 @@ shopping_event_products = Table(
Column('price', Float, nullable=False) # Price of this product at the time of this shopping event
)
class BrandInShop(Base):
__tablename__ = "brands_in_shops"
id = Column(Integer, primary_key=True, index=True)
shop_id = Column(Integer, ForeignKey("shops.id"), nullable=False)
brand_id = Column(Integer, ForeignKey("brands.id"), nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# Relationships
shop = relationship("Shop", back_populates="brands_in_shop")
brand = relationship("Brand", back_populates="shops_with_brand")
class Brand(Base):
__tablename__ = "brands"
@@ -27,6 +40,7 @@ class Brand(Base):
# Relationships
products = relationship("Product", back_populates="brand")
shops_with_brand = relationship("BrandInShop", back_populates="brand")
class GroceryCategory(Base):
__tablename__ = "grocery_categories"
@@ -82,6 +96,7 @@ class Shop(Base):
# Relationships
shopping_events = relationship("ShoppingEvent", back_populates="shop")
brands_in_shop = relationship("BrandInShop", back_populates="shop")
class ShoppingEvent(Base):
__tablename__ = "shopping_events"

View File

@@ -20,6 +20,28 @@ class Brand(BrandBase):
class Config:
from_attributes = True
# BrandInShop schemas
class BrandInShopBase(BaseModel):
shop_id: int
brand_id: int
class BrandInShopCreate(BrandInShopBase):
pass
class BrandInShopUpdate(BaseModel):
shop_id: Optional[int] = None
brand_id: Optional[int] = None
class BrandInShop(BrandInShopBase):
id: int
created_at: datetime
updated_at: Optional[datetime] = None
shop: "Shop"
brand: "Brand"
class Config:
from_attributes = True
# Grocery Category schemas
class GroceryCategoryBase(BaseModel):
name: str
@@ -168,4 +190,7 @@ class ShopStats(BaseModel):
shop_name: str
total_spent: float
visit_count: int
avg_per_visit: float
avg_per_visit: float
# Update forward references
BrandInShop.model_rebuild()