def create():
    req_data = request.get_json()
    try:
        data = rentableitem_schema.load(req_data)
    except marshmallow.exceptions.ValidationError as e:
        return custom_response(e.messages, 401)

    rentableitem = RentableItemModel(data)

    user_in_db = UserModel.get_user_by_name(data.get('owner_name'))

    if not user_in_db:
        message = {
            'error': 'User does not exists exist, please supply another name'
        }
        return custom_response(message, 400)

    if rentableitem.category not in RentableItemModel.categoryTypes:
        message = {
            'error':
            'Category does not exists exist, please supply another category'
        }
        return custom_response(message, 400)

    rentableitem.owner_id = user_in_db.id
    rentableitem.owner = user_in_db
    rentableitem.owner_name = user_in_db.name
    rentableitem.rented = False
    rentableitem.save()

    ser_rentableitem = rentableitem_schema.dump(rentableitem)
    return custom_response(ser_rentableitem, 200)
def rent_item(rentableitem_id):
    req_data = request.get_json()
    data = rentperiod_schema.load(req_data)
    start_date = data.get("start_date")
    end_date = data.get("end_date")
    name = data.get("user_name")

    user = UserModel.get_user_by_name(name)

    if not user:
        message = {
            'error': 'User does not exists exist, please supply another name'
        }
        return custom_response(message, 400)
    rentableitem = RentableItemModel.get_rentableitem_by_id(rentableitem_id)

    if not rentableitem:
        return custom_response({'error': 'rentable item not found'}, 404)

    periods_of_rental_of_current_item = RentPeriodModel.get_all_rent_periods_with_rentableitem_id(
        rentableitem.id)
    for period_of_rental in periods_of_rental_of_current_item:
        if start_date < period_of_rental.end_date and end_date > period_of_rental.start_date:
            return custom_response(
                {'error': 'the item is already rented in that period'}, 404)

    if not (start_date >= rentableitem.available_start_date
            and end_date <= rentableitem.available_end_date):
        return custom_response(
            {'error': 'the owner does not rent the item in that period'}, 404)

    rent_period = RentPeriodModel(data)
    rent_period.rentableitem = rentableitem
    rent_period.rentableitem_id = rentableitem.id
    rent_period.save()

    rentableitem.rented = True
    db.session.commit()
    return custom_response("rent succes", 200)
def create():
    req_data = request.get_json()

    data = review_schema.load(req_data)

    review = ReviewItemModel(data)

    if not (1 <= review.rating <= 5):
        message = {'error': 'Rating must be between 1 and 5'}
        return custom_response(message, 400)

    user_in_db = UserModel.get_user_by_name(data.get('owner_name'))
    rentableitem_in_db = RentableItemModel.get_rentableitem_by_id(
        data.get('rentableitem_id'))

    if not user_in_db:
        message = {
            'error': 'User does not exists exist, please supply another name'
        }
        return custom_response(message, 400)

    if not rentableitem_in_db:
        message = {
            'error':
            'Rentable item does not exists exist, please supply another id'
        }
        return custom_response(message, 401)

    review.owner_id = user_in_db.id
    review.owner = user_in_db
    review.owner_name = user_in_db.name
    review.rentableitem = rentableitem_in_db
    review.rentableitem_id = rentableitem_in_db.id

    review.save()

    ser_review = review_schema.dump(review)
    return custom_response(ser_review, 200)