-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
204 lines (160 loc) · 4.86 KB
/
Copy pathapp.py
File metadata and controls
204 lines (160 loc) · 4.86 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from flask import Flask, jsonify
from flask_cors import CORS
from routes.bookRoutes import BookRoutes
from routes.route import Routes
from routes.userRoutes import UserRoutes
from models.user import db
from utils.redis import redis
from flask_migrate import Migrate
from dotenv import load_dotenv
from flask_jwt_extended import jwt_required, get_jwt
from controller.userController import jwt
import os
from datetime import timedelta
# initialize the flask app
app = Flask(__name__)
CORS(app)
# Load environment variables
load_dotenv()
# Get environment variables
userName = os.getenv('DATABASE_USER')
userPassword = os.getenv('DATABASE_PASSWORD')
host = os.getenv('HOST')
dataBaseName = os.getenv('DATABASE_NAME')
routeDefault = os.getenv('ROUTE_DEFAULT')
secretKey = os.getenv('SECRET_KEY')
# Configure your MySQL database
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{}:{}@{}/{}'.format(
userName, userPassword, host, dataBaseName)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
migrate = Migrate(app, db)
app.config['JWT_SECRET_KEY'] = secretKey
jwt.init_app(app)
with app.app_context():
db.create_all()
# Defining the main routes
defaultRoute = routeDefault
@app.route(defaultRoute + '/', methods=['GET'], strict_slashes=False)
def home():
"""
This function handles the home route of the application.
It returns the result of the index() function from the Routes module.
"""
return Routes.index()
@app.route(defaultRoute + '/status', methods=['GET'], strict_slashes=False)
def status():
"""
Returns the status of the application.
"""
return Routes.status()
@app.route(defaultRoute + '/stats', methods=['GET'], strict_slashes=False)
def stats():
"""
Returns the statistics for the library management system.
:return: The statistics data.
"""
return Routes.stats()
@app.route(defaultRoute + '/insert', methods=['POST'], strict_slashes=False)
@jwt_required()
def add():
"""
Add a book to the library.
Returns:
The result of inserting the book.
"""
return BookRoutes.insert_book()
@app.route(defaultRoute + '/delete', methods=['DELETE'], strict_slashes=False)
@jwt_required()
def delete():
"""
Deletes a book from the library.
Returns:
The result of the delete operation.
"""
return BookRoutes.delete()
@app.route(defaultRoute + '/search', methods=['GET'], strict_slashes=False)
def search():
"""
This function handles the search endpoint of the Library Management System API.
It calls the searchBooks() function from the BookRoutes module to perform the search.
"""
return BookRoutes.searchBooks()
@app.route(defaultRoute + '/search/update', methods=['GET'], strict_slashes=False)
def searchupdate():
"""
This function handles the search update route.
It calls the searchAll() function from the BookRoutes module.
"""
return BookRoutes.searchAll()
@app.route(defaultRoute + '/list', methods=['GET'], strict_slashes=False)
def list_books():
"""
Retrieves a list of books from the database.
Returns:
A list of books.
"""
return BookRoutes.get_book_list()
@app.route(defaultRoute + '/list/category', methods=['GET'], strict_slashes=False)
def list_books_by_category():
"""
Retrieve a list of books by category.
Returns:
A list of books filtered by category.
"""
return BookRoutes.listAllBycategory()
@app.route(defaultRoute + '/update', methods=['PUT'], strict_slashes=False)
@jwt_required()
def update():
"""
Update a book in the library.
Returns:
The updated book information.
"""
return BookRoutes.updateBook()
# User routes
@app.route(defaultRoute + '/login', methods=['POST'], strict_slashes=False)
def login():
"""
Login a user.
Returns:
The result of the login.
"""
return UserRoutes.loginUser()
@app.route(defaultRoute + '/register', methods=['POST'], strict_slashes=False)
def register():
"""
Register a user.
Returns:
The result of the registration.
"""
return UserRoutes.registerUser()
@app.route(defaultRoute + "/user", methods=["GET"])
@jwt_required()
def getuser():
return UserRoutes.getUser()
@app.route(defaultRoute + "/logout", methods=["DELETE"])
@jwt_required()
def logout():
jti = get_jwt()['tokens']
timeExpiration = timedelta(hours=2)
redis.set(jti, ex=timeExpiration)
return UserRoutes.logoutUser()
'''
@app.route(defaultRoute + '/role', methods=['GET'])
@jwt_required
def role():
return UserRoutes.roles()
'''
@app.errorhandler(404)
def not_found(error):
"""
Error handler for 404 status code.
Args:
error: The error object.
Returns:
A JSON response with the error details.
"""
return jsonify({"success": False, "error": 404, "message": "Resource Not Found"}), 404
if __name__ == '__main__':
app.run(debug=True)