Program/mooc/crud/crud_coupon_location.py

32 lines
1.4 KiB
Python
Raw Normal View History

from sqlalchemy.orm import Session
from . import models, schemas
def get_coupon_location(db: Session, coupon_location_id: int):
return db.query(models.CouponLocation).filter(models.CouponLocation.id == coupon_location_id).first()
def get_coupon_locations(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.CouponLocation).offset(skip).limit(limit).all()
def create_coupon_location(db: Session, coupon_location: schemas.CouponLocationCreate):
db_coupon_location = models.CouponLocation(**coupon_location.dict())
db.add(db_coupon_location)
db.commit()
db.refresh(db_coupon_location)
return db_coupon_location
def update_coupon_location(db: Session, coupon_location_id: int, coupon_location: schemas.CouponLocationUpdate):
db_coupon_location = db.query(models.CouponLocation).filter(models.CouponLocation.id == coupon_location_id).first()
if db_coupon_location:
for var, value in vars(coupon_location).items():
setattr(db_coupon_location, var, value) if value else None
db.add(db_coupon_location)
db.commit()
db.refresh(db_coupon_location)
return db_coupon_location
def delete_coupon_location(db: Session, coupon_location_id: int):
db_coupon_location = db.query(models.CouponLocation).filter(models.CouponLocation.id == coupon_location_id).first()
if db_coupon_location:
db.delete(db_coupon_location)
db.commit()
return db_coupon_location