-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (55 loc) · 1.84 KB
/
main.py
File metadata and controls
78 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
# Mock Data
books = [
{"id": 1, "title": "Book One", "author": "Author A", "category": "Fiction"},
{"id": 2, "title": "Book Two", "author": "Author B", "category": "Non-Fiction"},
]
authors = ["Author A", "Author B", "Author C"]
categories = ["Fiction", "Non-Fiction", "Biography"]
# Home Page
@app.route("/")
def home():
return render_template("index.html")
# Books Page
@app.route("/books", methods=["GET"])
def get_books():
return render_template("books.html", books=books)
@app.route("/books", methods=["POST"])
def add_book():
data = request.json
books.append(data)
return jsonify(data), 201
@app.route("/books/<int:book_id>", methods=["PUT"])
def update_book(book_id):
data = request.json
for book in books:
if book["id"] == book_id:
book.update(data)
return jsonify(book)
return jsonify({"error": "Book not found"}), 404
@app.route("/books/<int:book_id>", methods=["DELETE"])
def delete_book(book_id):
global books
books = [book for book in books if book["id"] != book_id]
return jsonify({"message": "Book deleted"})
# Authors Page
@app.route("/authors", methods=["GET"])
def get_authors():
return render_template("authors.html", authors=authors)
@app.route("/authors", methods=["POST"])
def add_author():
data = request.json
authors.append(data["name"])
return jsonify({"name": data["name"]}), 201
# Categories Page
@app.route("/categories", methods=["GET"])
def get_categories():
return render_template("categories.html", categories=categories)
@app.route("/categories", methods=["POST"])
def add_category():
data = request.json
categories.append(data["name"])
return jsonify({"name": data["name"]}), 201
if __name__ == "__main__":
app.run(debug=True)