def vehicles(id=None): if request.method == 'GET': if id is not None: vehicle = Vehicle.query.get(id) if not vehicle: return jsonify({"fail": "Vehicle not found"}), 404 return jsonify({ "success": "Vehicle found", "vehicle": vehicle.serialize() }), 200 else: vehicles = Vehicle.query.all() vehicles = list(map(lambda vehicle: vehicle.serialize(), vehicles)) return jsonify({ "total": len(vehicles), "results": vehicles }), 200 if request.method == 'POST': name = request.json.get('name') model = request.json.get('model') vehicle_class = request.json.get('vehicle_class') crew = request.json.get('crew') manufacturer = request.json.get('manufacturer') cargo_capacity = request.json.get('cargo_capacity') cost_in_credits = request.json.get('cost_in_credits') consumables = request.json.get('consumables') vehicle = Vehicle() vehicle.name = name vehicle.model = model vehicle.vehicle_class = vehicle_class vehicle.crew = crew vehicle.manufacturer = manufacturer vehicle.cargo_capacity = cargo_capacity vehicle.cost_in_credits = cost_in_credits vehicle.consumables = consumables vehicle.save() return jsonify({ "success": "vehicle created!", "vehicle": vehicle.serialize() }), 201 if request.method == 'PUT': pass if request.method == 'DELETE': vehicle = Vehicle.query.get(id) if not vehicle: return jsonify({"fail": "vehicle not found"}), 404 vehicle.delete() return jsonify({"success": "vehicle deleted"}), 200
def create_vehicle(): name = request.json.get('name') model = request.json.get('model') starship_class = request.json.get('starship_class') manufacturer = request.json.get('manufacturer') cost_in_credits = request.json.get('cost_in_credits') length = request.json.get('length') crew = request.json.get('crew') passengers = request.json.get('passengers') hyperdrive_rating = request.json.get('hyperdrive_rating') mglt = request.json.get('mglt') cargo_capacity = request.json.get('cargo_capacity') consumables = request.json.get('consumables') pilots = request.json.get('pilots') created = request.json.get('created') edited = request.json.get('edited') if not name: return jsonify({"Mensaje": "El nombre no puede estar vacio"}) new_vehicle = Vehicle() new_vehicle.name = name new_vehicle.model = model new_vehicle.starship_class = starship_class new_vehicle.manufacturer = manufacturer new_vehicle.cost_in_credits = cost_in_credits new_vehicle.length = length new_vehicle.crew = crew new_vehicle.passengers = passengers new_vehicle.max_atmosphering_speed = max_atmosphering_speed new_vehicle.hyperdrive_rating = hyperdrive_rating new_vehicle.mglt = mglt new_vehicle.cargo_capacity = cargo_capacity new_vehicle.consumables = consumables new_vehicle.pilots = pilots db.session.add(new_vehicle) db.session.commit() return jsonify({"Mensaje": "Vehiculo creado exitosamente"}), 201