72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
from flask import Flask, jsonify, request
|
|
import uuid
|
|
|
|
app = Flask(__name__)
|
|
|
|
components = {
|
|
"comp-1": {
|
|
"uuid": "comp-1",
|
|
"name": "Intel Core i5",
|
|
"category": "processor",
|
|
"price": 25000
|
|
},
|
|
"comp-2": {
|
|
"uuid": "comp-2",
|
|
"name": "NVIDIA RTX 4070",
|
|
"category": "graphics_card",
|
|
"price": 60000
|
|
}
|
|
}
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def get_components():
|
|
return jsonify(list(components.values()))
|
|
|
|
@app.route('/<component_id>', methods=['GET'])
|
|
def get_component(component_id):
|
|
component = components.get(component_id)
|
|
if component:
|
|
return jsonify(component)
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
@app.route('/', methods=['POST'])
|
|
def create_component():
|
|
data = request.get_json()
|
|
component_id = str(uuid.uuid4())
|
|
component = {
|
|
'uuid': component_id,
|
|
"name": data['name'],
|
|
"category": data['category'],
|
|
"price": data['price']
|
|
|
|
}
|
|
components[component_id] = component
|
|
return jsonify(component), 201
|
|
|
|
@app.route('/<component_id>', methods=['PUT'])
|
|
def update_component(component_id):
|
|
try:
|
|
if component_id not in components:
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
data = request.get_json()
|
|
component = components[component_id]
|
|
|
|
component['name'] = data.get('name', component['name'])
|
|
component['category'] = data.get('category', component['category'])
|
|
component['price'] = data.get('price', component['price'])
|
|
|
|
return jsonify(component)
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/<component_id>', methods=['DELETE'])
|
|
def delete_component(component_id):
|
|
if component_id not in components:
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
del components[component_id]
|
|
return jsonify({"message": "Component deleted"}), 200
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5001, debug=True) |