def create(isbn): uid = g.user.uid with db.auto_commit(): Book.query.filter_by(isbn=isbn).first_or_404() gift = Gift.query.filter_by(isbn=isbn, uid=uid).first() if gift: raise DuplicateGift() gift = Gift() gift.isbn = isbn gift.uid = uid db.session.add(gift) return Success()
def create(isbn): uid = g.user.uid with db.auto_commit(): Book.query.filter_by(isbn=isbn).first() gift = Gift.query.filter_by(uid=uid, isbn=isbn, launched=False).first() if gift: raise DuplicateGift() gift = Gift() gift.uid = uid gift.isbn = isbn db.session.add(gift) return Success()
def create(isbn): # url = http://localhost:5000/v1/gift/<int:isbn> uid = g.user.uid with db.auto_commit(): Book.query.filter_by(isbn=isbn).first_or_404() gift = Gift.query.filter_by(isbn=isbn, uid=uid).first_or_404() if gift: raise DuplicateGift() gift = Gift() gift.isbn = isbn gift.uid = uid db.session.add(gift) return Success()
def create(isbn): uid = g.user.uid with db.auto_commit(): # 先查询有没有这本书, 没有会自己返回错误 Book.query.filter_by(isbn=isbn).first_or_404() # 再看是否有本同样的书正在赠送 gift = Gift.query.filter_by(uid=uid, isbn=isbn).first() if gift: raise DuplicateGift() gift = Gift() gift.uid = uid gift.isbn = isbn db.session.add(gift) return Success()