from flask import Flask, request, jsonify
from datetime import datetime
app = Flask(__name__)
# In-memory storage (for simplicity, use a database in production)
stations = ["Tokyo", "Shinagawa", "Shin-Yokohama", "Nagoya", "Kyoto", "Shin-Osaka"]
trains = [
{"id": 1, "name": "Nozomi 1", "departure_time": "08:00", "route": stations, "seats": 100},
{"id": 2, "name": "Hikari 5", "departure_time": "09:00", "route": stations, "seats": 120},
{"id": 3, "name": "Kodama 11", "departure_time": "10:00", "route": stations, "seats": 80},
]
reservations = []
@app.route("/trains", methods=["GET"])
def get_trains():
return jsonify(trains)
@app.route("/stations", methods=["GET"])
def get_stations():
return jsonify(stations)
@app.route("/availability", methods=["POST"])
def check_availability():
data = request.json
required_fields = ["train_id", "departure", "destination"]
# Validate input
for field in required_fields:
if field not in data:
return jsonify({"error": f"'{field}' is required"}), 400
train_id = data["train_id"]
departure = data["departure"]
destination = data["destination"]
# Validate train and stations
train = next((train for train in trains if train["id"] == train_id), None)
if not train:
return jsonify({"error": "Invalid train ID"}), 400
if departure not in stations or destination not in stations:
return jsonify({"error": "Invalid station"}), 400
if stations.index(departure) >= stations.index(destination):
return jsonify({"error": "Destination must be after departure"}), 400
# Check available seats
reserved_seats = sum(
1 for res in reservations
if res["train_id"] == train_id and
stations.index(res["departure"]) <= stations.index(departure) < stations.index(res["destination"])
)
available_seats = train["seats"] - reserved_seats
return jsonify({"available_seats": available_seats})
@app.route("/reserve", methods=["POST"])
def reserve_ticket():
data = request.json
required_fields = ["train_id", "departure", "destination", "name"]
# Validate input
for field in required_fields:
if field not in data:
return jsonify({"error": f"'{field}' is required"}), 400
train_id = data["train_id"]
departure = data["departure"]
destination = data["destination"]
name = data["name"]
# Validate train and stations
train = next((train for train in trains if train["id"] == train_id), None)
if not train:
return jsonify({"error": "Invalid train ID"}), 400
if departure not in stations or destination not in stations:
return jsonify({"error": "Invalid station"}), 400
if stations.index(departure) >= stations.index(destination):
return jsonify({"error": "Destination must be after departure"}), 400
# Check available seats
reserved_seats = sum(
1 for res in reservations
if res["train_id"] == train_id and
stations.index(res["departure"]) <= stations.index(departure) < stations.index(res["destination"])
)
if reserved_seats >= train["seats"]:
return jsonify({"error": "No available seats"}), 400
# Create reservation
reservation = {
"id": len(reservations) + 1,
"train_id": train_id,
"train_name": train["name"],
"departure": departure,
"destination": destination,
"name": name,
"reservation_time": datetime.now().isoformat()
}
reservations.append(reservation)
return jsonify(reservation), 201
@app.route("/reservations", methods=["GET"])
def get_reservations():
return jsonify(reservations)
if __name__ == "__main__":
app.run(debug=True)
コメント