示例#1
0
    def check_out(cls, video_id, customer_id):
        new_rental = cls(video_id=video_id, customer_id=customer_id)
        if not new_rental:
            return None

        video = Video.get_video_by_id(new_rental.video_id)
        customer = Customer.get_customer_by_id(new_rental.customer_id)

        # If no available inventory, you can't rent the video
        if video.available_inventory <= 0:
            return None

        new_rental.save()
        video.available_inventory -= 1
        customer.videos_checked_out_count += 1
        video.save()
        customer.save()

        videos_checked_out_count = customer.videos_checked_out_count
        available_inventory = video.available_inventory

        return {
            # "id": new_rental.id,
            "video_id": new_rental.video_id,
            "customer_id": new_rental.customer_id,
            "videos_checked_out_count": videos_checked_out_count,
            "available_inventory": available_inventory,
            "due_date": new_rental.due_date
        }
示例#2
0
 def to_json(self):
     video = Video.get_video_by_id(self.video_id)
     return {
         "title": video.title,
         "due_date": self.due_date,
         "release_date": video.release_date,
         #"checkout_date": (self.due_date - timedelta(days=7))
     }
示例#3
0
def videos_delete(video_id):
    video = Video.get_video_by_id(video_id)
    if not video:
        return {"message": f"Video {video_id} was not found"}, 404

    video.delete()

    return {
        "id": video.id,
    }, 200
示例#4
0
def videos_update(video_id):
    video = Video.get_video_by_id(video_id)
    if not video:
        return {"message": f"Video {video_id} was not found"}, 404

    request_body = request.get_json()
    if invalid_video_data(request_body):
        return {"message": "Invalid data"}, 400

    video.title = request_body.get("title")
    video.release_date = request_body.get("release_date")
    video.total_inventory = request_body.get("total_inventory")

    video.save()

    return video.to_json(), 200
示例#5
0
def get_rentals_for_video(video_id):
    video = Video.get_video_by_id(video_id)
    if not video:
        return {"message": f"Video {id} not found"}, 404

    rentals = video.rentals

    results = []
    for rental in rentals:
        customer = Customer.query.get_or_404(rental.customer_id)
        if rental.status:
            results.append({
                "due_date": rental.due_date,
                "name": customer.name,
                "phone": customer.phone,
                "postal_code": int(customer.postal_code),
                "status": rental.status
            })

    return jsonify(results), 200
示例#6
0
def check_in_video():
    request_body = request.get_json()

    if invalid_rental_data(request_body):
        return {"message": "Invalid request body"}, 400

    video = Video.get_video_by_id(request_body["video_id"])

    if not video:
        return {"message": f"Video {request_body['video_id']} not found."}, 404

    customer = Customer.get_customer_by_id(request_body['customer_id'])

    if not customer:
        return {
            "message": f"Customer {request_body['customer_id']} not found"
        }, 404

    result = Rental.check_in(video_id=video.id, customer_id=customer.id)

    return result
示例#7
0
    def check_in(cls, customer_id, video_id):
        checked_in_rental = cls.query.filter(
            Rental.customer_id == customer_id).filter(
                Rental.video_id == video_id).filter(
                    Rental.status == "Checked_out").first()

        if not checked_in_rental:
            return {
                "message":
                f"No outstanding rentals for customer # {customer_id} and video {video_id}"
            }, 400

        checked_in_rental.status = None

        video = Video.get_video_by_id(checked_in_rental.video_id)
        customer = Customer.get_customer_by_id(checked_in_rental.customer_id)

        if not video:
            return {"message": f"Movie id: {video_id} not found"}, 404

        if not customer:
            return {"message": f"Customer id: {customer_id} not found"}, 404

        video.available_inventory += 1
        customer.videos_checked_out_count -= 1

        checked_in_rental.save()
        video.save()
        customer.save()

        videos_checked_out_count = customer.videos_checked_out_count
        available_inventory = video.available_inventory

        return {
            # "id": checked_in_rental.id,
            "video_id": checked_in_rental.video_id,
            "customer_id": checked_in_rental.customer_id,
            "videos_checked_out_count": videos_checked_out_count,
            "available_inventory": available_inventory,
        }, 200
示例#8
0
def videos_show(video_id):
    video = Video.get_video_by_id(video_id)
    if not video:
        return {"message": f"Video {video_id} was not found"}, 404

    return video.to_json(), 200