diff --git a/app/forms.py b/app/forms.py new file mode 100644 index 0000000..5234b8f --- /dev/null +++ b/app/forms.py @@ -0,0 +1,14 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, SubmitField + +class SearchForm(FlaskForm): + asset_name = StringField('Asset Name') + submit = SubmitField('Search') + +class UpdateForm(FlaskForm): + asset_name = StringField('Asset Name') + submit = SubmitField('Update') + +class DeleteForm(FlaskForm): + asset_name = StringField('Asset Name') + submit = SubmitField('Delete') diff --git a/app/models.py b/app/models.py index bf6b257..ca538f3 100644 --- a/app/models.py +++ b/app/models.py @@ -1,10 +1,60 @@ -from app import db +from flask import Blueprint, jsonify, request +from app.models import Asset, db -class Demo(db.Model): - id = db.Column(db.Integer, primary_key=True) - info1 = db.Column(db.String(80), unique=True, nullable=False) - info2 = db.Column(db.String(120), unique=True, nullable=False) +routes = Blueprint('routes', __name__) - def __init__(self, info1, info2): - self.info1 = info1 - self.info2 = info2 \ No newline at end of file +@routes.route('/assets', methods=['GET']) +def get_assets(): + name = request.args.get('name') + type = request.args.get('type') + page = request.args.get('page', 1, type=int) + limit = request.args.get('limit', 10, type=int) + + assets = Asset.query.filter_by(name=name, type=type).paginate(page, limit) + + result = { + 'assets': [asset.serialize() for asset in assets.items], + 'total': assets.total, + 'pages': assets.pages, + 'has_next': assets.has_next, + 'has_prev': assets.has_prev + } + + return jsonify(result), 200 + +@routes.route('/assets/', methods=['GET']) +def get_asset(asset_id): + asset = Asset.query.get(asset_id) + + if asset is None: + return jsonify({'message': 'Asset not found'}), 404 + + return jsonify(asset.serialize()), 200 + +@routes.route('/assets/', methods=['PUT']) +def update_asset(asset_id): + asset = Asset.query.get(asset_id) + + if asset is None: + return jsonify({'message': 'Asset not found'}), 404 + + data = request.get_json() + + asset.name = data.get('name', asset.name) + asset.type = data.get('type', asset.type) + + db.session.commit() + + return jsonify(asset.serialize()), 200 + +@routes.route('/assets/', methods=['DELETE']) +def delete_asset(asset_id): + asset = Asset.query.get(asset_id) + + if asset is None: + return jsonify({'message': 'Asset not found'}), 404 + + db.session.delete(asset) + db.session.commit() + + return jsonify({'message': 'Asset deleted'}), 200 diff --git a/app/routes.py b/app/routes.py index c91be32..9c77065 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,55 +1,23 @@ -from flask import request, jsonify -from app import app, db -from app.models import Demo +from flask import Flask, request -# 添加用户 -@app.route('/demos', methods=['POST']) -def add_demo(): - data = request.get_json() - new_demo = Demo(info1=data['info1'], info2=data['info2']) - db.session.add(new_demo) - db.session.commit() - return jsonify({'message': 'Demo added successfully'}) +app = Flask(__name__) -# 获取所有用户 -@app.route('/demos', methods=['GET']) -def get_demos(): - demos = Demo.query.all() - demo_list = [] - for demo in demos: - demo_data = {'id': demo.id, 'info1': demo.info1, 'info2': demo.info2} - demo_list.append(demo_data) - return jsonify({'demos': demo_list}) +@app.route('/assets', methods=['GET']) +def search_assets(): + # handle GET request to search assets + query_params = request.args + # process query parameters and perform asset search + # return search results -# 获取单个用户 -@app.route('/demos/', methods=['GET']) -def get_demo(demo_id): - demo = Demo.query.get(demo_id) - if demo: - demo_data = {'id': demo.id, 'info1': demo.info1, 'info2': demo.info2} - return jsonify(demo_data) - return jsonify({'message': 'Demo not found'}), 404 +@app.route('/assets/', methods=['PUT']) +def update_asset(asset_id): + # handle PUT request to update asset + # retrieve asset information from request body + # update asset information in database + # return updated asset information -# 更新用户信息 -@app.route('/demos/', methods=['PUT']) -def update_demo(demo_id): - demo = Demo.query.get(demo_id) - if not demo: - return jsonify({'message': 'Demo not found'}), 404 - - data = request.get_json() - demo.info1 = data['info1'] - demo.info2 = data['info2'] - db.session.commit() - return jsonify({'message': 'Demo updated successfully'}) - -# 删除用户 -@app.route('/demos/', methods=['DELETE']) -def delete_demo(demo_id): - demo = Demo.query.get(demo_id) - if not demo: - return jsonify({'message': 'Demo not found'}), 404 - - db.session.delete(demo) - db.session.commit() - return jsonify({'message': 'Demo deleted successfully'}) +@app.route('/assets/', methods=['DELETE']) +def delete_asset(asset_id): + # handle DELETE request to delete asset + # delete asset from database + # return success message diff --git a/app/static b/app/static new file mode 100644 index 0000000..3bd4797 --- /dev/null +++ b/app/static @@ -0,0 +1 @@ +/* Add your CSS code here */ diff --git a/app/templates b/app/templates new file mode 100644 index 0000000..8fa4687 --- /dev/null +++ b/app/templates @@ -0,0 +1,10 @@ +from flask import render_template + +def search(): + return render_template('search.html') + +def update(): + return render_template('update.html') + +def delete(): + return render_template('delete.html') diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..9a6e4a5 --- /dev/null +++ b/app/utils.py @@ -0,0 +1,73 @@ +from flask import Flask, jsonify, request + +app = Flask(__name__) + +assets = [ + { + "id": 1, + "name": "Asset 1", + "description": "This is asset 1" + }, + { + "id": 2, + "name": "Asset 2", + "description": "This is asset 2" + }, + { + "id": 3, + "name": "Asset 3", + "description": "This is asset 3" + } +] + +def get_asset(asset_id): + for asset in assets: + if asset["id"] == asset_id: + return asset + return None + +def update_asset(asset_id, data): + for asset in assets: + if asset["id"] == asset_id: + asset.update(data) + return asset + return None + +def delete_asset(asset_id): + for asset in assets: + if asset["id"] == asset_id: + assets.remove(asset) + return True + return False + +@app.route("/assets/", methods=["GET"]) +def get_asset_route(asset_id): + asset = get_asset(asset_id) + if asset: + return jsonify(asset) + else: + return jsonify({"error": "Asset not found"}), 404 + +@app.route("/assets/", methods=["PUT"]) +def update_asset_route(asset_id): + asset = get_asset(asset_id) + if asset: + data = request.get_json() + updated_asset = update_asset(asset_id, data) + if updated_asset: + return jsonify(updated_asset) + else: + return jsonify({"error": "Failed to update asset"}), 500 + else: + return jsonify({"error": "Asset not found"}), 404 + +@app.route("/assets/", methods=["DELETE"]) +def delete_asset_route(asset_id): + asset = get_asset(asset_id) + if asset: + if delete_asset(asset_id): + return jsonify({"message": "Asset deleted successfully"}) + else: + return jsonify({"error": "Failed to delete asset"}), 500 + else: + return jsonify({"error": "Asset not found"}), 404