def update_ad(ad_id): current_user = User.find_by_email(auth.username()) ad = Ad.find(ad_id) if current_user.id == ad.creator_id: ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad.find(ad_id) if "title" in ad_data: ad.title = ad_data["title"] if "content" in ad_data: ad.content = ad_data["content"] if "price" in ad_data: ad.price = ad_data["price"] if "release_date" in ad_data: ad.release_date = ad_data["release_date"] if "is_active" in ad_data: ad.is_active = ad_data["is_active"] if "buyer" in ad_data: ad.buyer_id = ad_data["buyer"] return jsonify(ad.save().to_dict()), 201 else: return "Permission denied", 401
def delete_ad(ad_id): current_user = User.find_by_email(auth.username()) ad = Ad.find(ad_id) if current_user.id == ad.creator_id: ad = Ad.find(ad_id) ad.delete(ad_id) return "The ad was deleted", 200 else: return "Permission denied", 401
def buy_ad(user_id, ad_id): ad = Ad.find(ad_id) user = User.find(user_id) ad.is_active = 0 ad.buyer_id = user.name return jsonify(ad.save().to_dict())
def get_ad(ad_id): return jsonify(Ad.find(ad_id).to_dict())
def view_ad(ad_id): return render_template("ad.html", ad=Ad.find(ad_id))