1+ from flask import Flask , jsonify , request
2+ from flask_restful import Api , Resource
3+ from flasgger import Swagger
4+
5+ import book_review
6+
7+ app = Flask (__name__ )
8+ api = Api (app )
9+ swagger = Swagger (app )
10+
11+ class UppercaseText (Resource ):
12+
13+ def get (self ):
14+ """
15+ This method responds to the GET request for this endpoint and returns the data in uppercase.
16+ ---
17+ tags:
18+ - Text Processing
19+ parameters:
20+ - name: text
21+ in: query
22+ type: string
23+ required: true
24+ description: The text to be converted to uppercase
25+ responses:
26+ 200:
27+ description: A successful GET request
28+ content:
29+ application/json:
30+ schema:
31+ type: object
32+ properties:
33+ text:
34+ type: string
35+ description: The text in uppercase
36+ """
37+ text = request .args .get ('text' )
38+
39+ return jsonify ({"text" : text .upper ()})
40+
41+ class Records (Resource ):
42+
43+ def get (self ):
44+ """
45+ This method responds to the GET request for returning a number of books.
46+ ---
47+ tags:
48+ - Records
49+ parameters:
50+ - name: count
51+ in: query
52+ type: integer
53+ required: false
54+ description: The number of books to return
55+ - name: sort
56+ in: query
57+ type: string
58+ enum: ['ASC', 'DESC']
59+ required: false
60+ description: Sort order for the books
61+ responses:
62+ 200:
63+ description: A successful GET request
64+ content:
65+ application/json:
66+ schema:
67+ type: object
68+ properties:
69+ books:
70+ type: array
71+ items:
72+ type: object
73+ properties:
74+ title:
75+ type: string
76+ description: The title of the book
77+ author:
78+ type: string
79+ description: The author of the book
80+ """
81+
82+ count = request .args .get ('count' ) # Default to returning 10 books if count is not provided
83+ sort = request .args .get ('sort' )
84+
85+ # Get all the books
86+ books = book_review .get_all_records (count = count , sort = sort )
87+
88+ return jsonify ({"books" : books })
89+
90+ class AddRecord (Resource ):
91+
92+ def post (self ):
93+ """
94+ This method responds to the POST request for adding a new record to the DB table.
95+ ---
96+ tags:
97+ - Records
98+ parameters:
99+ - in: body
100+ name: body
101+ required: true
102+ schema:
103+ id: BookReview
104+ required:
105+ - Book
106+ - Rating
107+ properties:
108+ Book:
109+ type: string
110+ description: the name of the book
111+ Rating:
112+ type: integer
113+ description: the rating of the book (1-10)
114+ responses:
115+ 200:
116+ description: A successful POST request
117+ content:
118+ application/json:
119+ schema:
120+ type: object
121+ properties:
122+ message:
123+ type: string
124+ """
125+
126+ data = request .json
127+ print (data )
128+
129+ # Check if 'Book' and 'Rating' are present in the request body
130+ if 'Book' not in data or 'Rating' not in data :
131+ return jsonify ({"message" : "Bad request, missing 'Book' or 'Rating' in the request body" })
132+ # Call the add_record function to add the record to the DB table
133+ success = book_review .add_record (data )
134+
135+ if success :
136+ return jsonify ({"message" : "Record added successfully" })
137+ else :
138+ return jsonify ({"message" : "Failed to add record" })
139+
140+
141+
142+ api .add_resource (AddRecord , "/add-record" )
143+ api .add_resource (Records , "/records" )
144+ api .add_resource (UppercaseText , "/uppercase" )
145+
146+ if __name__ == "__main__" :
147+ app .run (debug = True )
0 commit comments