from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Textbook(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
author = db.Column(db.String(80))
year = db.Column(db.Integer)
description = db.Column(db.Text)
def __repr__(self):
return f'
]]>
from flask import Flask, request, jsonify
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///textbooks.db'
db.init_app(app)
@app.route('/api/textbooks', methods=['POST'])
def create_textbook():
data = request.get_json()
textbook = Textbook(title=data['title'], author=data['author'], year=data['year'], description=data['description'])
db.session.add(textbook)
db.session.commit()
return jsonify({'message': 'Textbook created successfully'}), 201
if __name__ == '__main__':
app.run(debug=True)
]]>