0% found this document useful (0 votes)
8 views2 pages

python code for app

This document outlines a basic API for a board game rental service using Flask in Python. It includes routes for listing games, viewing game details, renting and returning games, viewing active rentals, and recommending games based on customer preferences. Sample data for games and customers is provided to simulate a database.

Uploaded by

stavya.agg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

python code for app

This document outlines a basic API for a board game rental service using Flask in Python. It includes routes for listing games, viewing game details, renting and returning games, viewing active rentals, and recommending games based on customer preferences. Sample data for games and customers is provided to simulate a database.

Uploaded by

stavya.agg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Board Game Rental App - Backend in Flask (Python)

# This code outlines a basic API for a board game rental service.

from flask import Flask, jsonify, request

app = Flask(__name__)

# Sample data to simulate a database


games = [
{"id": 1, "name": "Catan", "genre": "Strategy", "available": True, "price": 50,
"popularity": 8},
{"id": 2, "name": "Monopoly", "genre": "Family", "available": True, "price":
30, "popularity": 7},
{"id": 3, "name": "Codenames", "genre": "Party", "available": True, "price":
20, "popularity": 9},
]

customers = [
{"name": "Alice", "preferred_genres": ["Strategy", "Party"], "rental_history":
[1, 3]},
{"name": "Bob", "preferred_genres": ["Family"], "rental_history": [2]},
]

rentals = []

# Route to list all games


@app.route('/games', methods=['GET'])
def get_games():
return jsonify(games)

# Route to view details of a specific game


@app.route('/games/<int:game_id>', methods=['GET'])
def get_game(game_id):
game = next((game for game in games if game['id'] == game_id), None)
if game:
return jsonify(game)
return jsonify({"error": "Game not found"}), 404

# Route to rent a game


@app.route('/rent', methods=['POST'])
def rent_game():
data = request.json
game_id = data.get('game_id')
customer_name = data.get('customer_name')

game = next((game for game in games if game['id'] == game_id), None)


customer = next((customer for customer in customers if customer['name'] ==
customer_name), None)

if not game:
return jsonify({"error": "Game not found"}), 404
if not customer:
return jsonify({"error": "Customer not found"}), 404
if not game['available']:
return jsonify({"error": "Game is currently unavailable"}), 400

game['available'] = False
rental = {"game_id": game_id, "customer_name": customer_name}
rentals.append(rental)
# Update customer's rental history
customer['rental_history'].append(game_id)

return jsonify({"message": "Game rented successfully", "rental": rental})

# Route to return a game


@app.route('/return', methods=['POST'])
def return_game():
data = request.json
game_id = data.get('game_id')

game = next((game for game in games if game['id'] == game_id), None)

if not game:
return jsonify({"error": "Game not found"}), 404
if game['available']:
return jsonify({"error": "Game is already marked as available"}), 400

game['available'] = True
rentals[:] = [rental for rental in rentals if rental['game_id'] != game_id]

return jsonify({"message": "Game returned successfully"})

# Route to view all active rentals


@app.route('/rentals', methods=['GET'])
def get_rentals():
return jsonify(rentals)

# Route to recommend games based on customer preferences


@app.route('/recommend/<customer_name>', methods=['GET'])
def recommend_games(customer_name):
customer = next((customer for customer in customers if customer['name'] ==
customer_name), None)

if not customer:
return jsonify({"error": "Customer not found"}), 404

preferred_genres = customer['preferred_genres']
rented_games = customer['rental_history']

recommended_games = [
game for game in games
if game['genre'] in preferred_genres and game['id'] not in rented_games and
game['available']
]

# Sort recommendations by popularity


recommended_games.sort(key=lambda x: x['popularity'], reverse=True)

return jsonify({"recommended_games": recommended_games})

if __name__ == '__main__':
app.run(debug=True)

You might also like