feat: enhance media API with structured responses and error handling; add pagination support
This commit is contained in:
224
app.py
224
app.py
@@ -30,83 +30,171 @@ with app.app_context():
|
|||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
# API 路由
|
# API 路由
|
||||||
@app.route('/api/media', methods=['GET'])
|
@app.route('/api/media/list', methods=['GET'])
|
||||||
def get_all_media():
|
def get_all_media():
|
||||||
media_list = Media.query.all()
|
try:
|
||||||
return jsonify([{
|
media_list = Media.query.all()
|
||||||
'id': media.id,
|
return jsonify({
|
||||||
'title': media.title,
|
"code": 0,
|
||||||
'type': media.type,
|
"data": [{
|
||||||
'status': media.status,
|
'id': media.id,
|
||||||
'rating': media.rating,
|
'title': media.title,
|
||||||
'notes': media.notes,
|
'type': media.type,
|
||||||
'platform': media.platform,
|
'status': media.status,
|
||||||
'date': media.date.isoformat() if media.date else None,
|
'rating': media.rating,
|
||||||
'created_at': media.created_at.isoformat(),
|
'notes': media.notes,
|
||||||
'updated_at': media.updated_at.isoformat()
|
'platform': media.platform,
|
||||||
} for media in media_list])
|
'date': media.date.isoformat() if media.date else None,
|
||||||
|
'created_at': media.created_at.isoformat(),
|
||||||
|
'updated_at': media.updated_at.isoformat()
|
||||||
|
} for media in media_list],
|
||||||
|
"message": "Success"
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"code": 1, "data": {}, "message": str(e)}), 500
|
||||||
|
|
||||||
@app.route('/api/media', methods=['POST'])
|
@app.route('/api/media/create', methods=['POST'])
|
||||||
def create_media():
|
def create_media():
|
||||||
data = request.json
|
try:
|
||||||
new_media = Media(
|
data = request.json
|
||||||
title=data['title'],
|
new_media = Media(
|
||||||
type=data['type'],
|
title=data['title'],
|
||||||
status=data['status'],
|
type=data['type'],
|
||||||
rating=data.get('rating'),
|
status=data['status'],
|
||||||
notes=data.get('notes'),
|
rating=data.get('rating'),
|
||||||
platform=data.get('platform'),
|
notes=data.get('notes'),
|
||||||
date=datetime.strptime(data['date'], '%Y-%m-%d').date() if data.get('date') else None
|
platform=data.get('platform'),
|
||||||
)
|
date=datetime.strptime(data['date'], '%Y-%m-%d').date() if data.get('date') else None
|
||||||
db.session.add(new_media)
|
)
|
||||||
db.session.commit()
|
db.session.add(new_media)
|
||||||
return jsonify({
|
db.session.commit()
|
||||||
'id': new_media.id,
|
return jsonify({
|
||||||
'title': new_media.title,
|
"code": 0,
|
||||||
'type': new_media.type,
|
"data": {
|
||||||
'status': new_media.status,
|
'id': new_media.id,
|
||||||
'rating': new_media.rating,
|
'title': new_media.title,
|
||||||
'notes': new_media.notes,
|
'type': new_media.type,
|
||||||
'platform': new_media.platform,
|
'status': new_media.status,
|
||||||
'date': new_media.date.isoformat() if new_media.date else None,
|
'rating': new_media.rating,
|
||||||
'created_at': new_media.created_at.isoformat(),
|
'notes': new_media.notes,
|
||||||
'updated_at': new_media.updated_at.isoformat()
|
'platform': new_media.platform,
|
||||||
}), 201
|
'date': new_media.date.isoformat() if new_media.date else None,
|
||||||
|
'created_at': new_media.created_at.isoformat(),
|
||||||
|
'updated_at': new_media.updated_at.isoformat()
|
||||||
|
},
|
||||||
|
"message": "Created successfully"
|
||||||
|
}), 201
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({"code": 2, "data": {}, "message": str(e)}), 500
|
||||||
|
|
||||||
@app.route('/api/media/<int:media_id>', methods=['PUT'])
|
@app.route('/api/media/updateById/<int:media_id>', methods=['PUT'])
|
||||||
def update_media(media_id):
|
def update_media(media_id):
|
||||||
media = Media.query.get_or_404(media_id)
|
try:
|
||||||
data = request.json
|
media = Media.query.get_or_404(media_id)
|
||||||
|
data = request.json
|
||||||
|
|
||||||
media.title = data.get('title', media.title)
|
media.title = data.get('title', media.title)
|
||||||
media.type = data.get('type', media.type)
|
media.type = data.get('type', media.type)
|
||||||
media.status = data.get('status', media.status)
|
media.status = data.get('status', media.status)
|
||||||
media.rating = data.get('rating', media.rating)
|
media.rating = data.get('rating', media.rating)
|
||||||
media.notes = data.get('notes', media.notes)
|
media.notes = data.get('notes', media.notes)
|
||||||
media.platform = data.get('platform', media.platform)
|
media.platform = data.get('platform', media.platform)
|
||||||
if data.get('date'):
|
if data.get('date'):
|
||||||
media.date = datetime.strptime(data['date'], '%Y-%m-%d').date()
|
media.date = datetime.strptime(data['date'], '%Y-%m-%d').date()
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'id': media.id,
|
"code": 0,
|
||||||
'title': media.title,
|
"data": {
|
||||||
'type': media.type,
|
'id': media.id,
|
||||||
'status': media.status,
|
'title': media.title,
|
||||||
'rating': media.rating,
|
'type': media.type,
|
||||||
'notes': media.notes,
|
'status': media.status,
|
||||||
'platform': media.platform,
|
'rating': media.rating,
|
||||||
'date': media.date.isoformat() if media.date else None,
|
'notes': media.notes,
|
||||||
'created_at': media.created_at.isoformat(),
|
'platform': media.platform,
|
||||||
'updated_at': media.updated_at.isoformat()
|
'date': media.date.isoformat() if media.date else None,
|
||||||
})
|
'created_at': media.created_at.isoformat(),
|
||||||
|
'updated_at': media.updated_at.isoformat()
|
||||||
|
},
|
||||||
|
"message": "Updated successfully"
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({"code": 2, "data": {}, "message": str(e)}), 500
|
||||||
|
|
||||||
@app.route('/api/media/<int:media_id>', methods=['DELETE'])
|
@app.route('/api/media/deleteById/<int:media_id>', methods=['DELETE'])
|
||||||
def delete_media(media_id):
|
def delete_media(media_id):
|
||||||
media = Media.query.get_or_404(media_id)
|
try:
|
||||||
db.session.delete(media)
|
media = Media.query.get_or_404(media_id)
|
||||||
db.session.commit()
|
db.session.delete(media)
|
||||||
return '', 204
|
db.session.commit()
|
||||||
|
return jsonify({"code": 0, "data": {}, "message": "Deleted successfully"}), 204
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({"code": 2, "data": {}, "message": str(e)}), 500
|
||||||
|
|
||||||
|
@app.route('/api/media/page', methods=['GET'])
|
||||||
|
def get_media_page():
|
||||||
|
try:
|
||||||
|
type = request.args.get('type')
|
||||||
|
current_page = int(request.args.get('currentPage', 1))
|
||||||
|
page_size = int(request.args.get('pageSize', 10))
|
||||||
|
title = request.args.get('title', '')
|
||||||
|
start_date = request.args.get('startDate')
|
||||||
|
end_date = request.args.get('endDate')
|
||||||
|
sort_by = request.args.get('sortBy', 'date')
|
||||||
|
sort_type = request.args.get('sortType', 'desc')
|
||||||
|
|
||||||
|
if not type:
|
||||||
|
return jsonify({"code": 1, "data": {}, "message": "Type is required"}), 400
|
||||||
|
|
||||||
|
query = Media.query.filter(Media.type == type)
|
||||||
|
if title:
|
||||||
|
query = query.filter(Media.title.like(f'%{title}%'))
|
||||||
|
if start_date:
|
||||||
|
query = query.filter(Media.date >= datetime.strptime(start_date, '%Y-%m-%d').date())
|
||||||
|
if end_date:
|
||||||
|
query = query.filter(Media.date <= datetime.strptime(end_date, '%Y-%m-%d').date())
|
||||||
|
|
||||||
|
if sort_by == 'date':
|
||||||
|
if sort_type == 'asc':
|
||||||
|
query = query.order_by(Media.date.asc())
|
||||||
|
else:
|
||||||
|
query = query.order_by(Media.date.desc())
|
||||||
|
elif sort_by == 'score':
|
||||||
|
if sort_type == 'asc':
|
||||||
|
query = query.order_by(Media.rating.asc())
|
||||||
|
else:
|
||||||
|
query = query.order_by(Media.rating.desc())
|
||||||
|
|
||||||
|
pagination = query.paginate(page=current_page, per_page=page_size, error_out=False)
|
||||||
|
media_list = pagination.items
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
"code": 0,
|
||||||
|
"data": {
|
||||||
|
"list": [{
|
||||||
|
'id': media.id,
|
||||||
|
'title': media.title,
|
||||||
|
'type': media.type,
|
||||||
|
'status': media.status,
|
||||||
|
'rating': media.rating,
|
||||||
|
'notes': media.notes,
|
||||||
|
'platform': media.platform,
|
||||||
|
'date': media.date.isoformat() if media.date else None,
|
||||||
|
'created_at': media.created_at.isoformat(),
|
||||||
|
'updated_at': media.updated_at.isoformat()
|
||||||
|
} for media in media_list],
|
||||||
|
"total": pagination.total,
|
||||||
|
"currentPage": current_page,
|
||||||
|
"pageSize": page_size
|
||||||
|
},
|
||||||
|
"message": "Success"
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"code": 1, "data": {}, "message": str(e)}), 500
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
Reference in New Issue
Block a user