112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_cors import CORS
|
|
from datetime import datetime
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
# 配置数据库
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///media.db'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
db = SQLAlchemy(app)
|
|
|
|
# 定义数据模型
|
|
class Media(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
title = db.Column(db.String(200), nullable=False)
|
|
type = db.Column(db.String(50), nullable=False) # game, book, movie, anime, other
|
|
status = db.Column(db.String(50), nullable=False) # completed, in_progress, plan_to_watch
|
|
rating = db.Column(db.Float)
|
|
notes = db.Column(db.Text)
|
|
platform = db.Column(db.String(100)) # 平台信息
|
|
date = db.Column(db.Date) # 完成/观看日期
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# 创建所有数据库表
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
# API 路由
|
|
@app.route('/api/media', methods=['GET'])
|
|
def get_all_media():
|
|
media_list = Media.query.all()
|
|
return jsonify([{
|
|
'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])
|
|
|
|
@app.route('/api/media', methods=['POST'])
|
|
def create_media():
|
|
data = request.json
|
|
new_media = Media(
|
|
title=data['title'],
|
|
type=data['type'],
|
|
status=data['status'],
|
|
rating=data.get('rating'),
|
|
notes=data.get('notes'),
|
|
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()
|
|
return jsonify({
|
|
'id': new_media.id,
|
|
'title': new_media.title,
|
|
'type': new_media.type,
|
|
'status': new_media.status,
|
|
'rating': new_media.rating,
|
|
'notes': new_media.notes,
|
|
'platform': new_media.platform,
|
|
'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()
|
|
}), 201
|
|
|
|
@app.route('/api/media/<int:media_id>', methods=['PUT'])
|
|
def update_media(media_id):
|
|
media = Media.query.get_or_404(media_id)
|
|
data = request.json
|
|
|
|
media.title = data.get('title', media.title)
|
|
media.type = data.get('type', media.type)
|
|
media.status = data.get('status', media.status)
|
|
media.rating = data.get('rating', media.rating)
|
|
media.notes = data.get('notes', media.notes)
|
|
media.platform = data.get('platform', media.platform)
|
|
if data.get('date'):
|
|
media.date = datetime.strptime(data['date'], '%Y-%m-%d').date()
|
|
|
|
db.session.commit()
|
|
return jsonify({
|
|
'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()
|
|
})
|
|
|
|
@app.route('/api/media/<int:media_id>', methods=['DELETE'])
|
|
def delete_media(media_id):
|
|
media = Media.query.get_or_404(media_id)
|
|
db.session.delete(media)
|
|
db.session.commit()
|
|
return '', 204
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |