forked from COMP491-Team-Education/Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpy_site.py
62 lines (52 loc) · 1.41 KB
/
webpy_site.py
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
import psycopg2
# Import your plugin functions
from plugin import connect, get_book, add_book, remove_book, close
# Database connection details
db_details = {
'host': 'your_host',
'database': 'your_db',
'user': 'your_username',
'password': 'your_password'
}
# Connect to the database
conn, cur = connect(**db_details)
# Define URL mappings
urls = (
'/book/title/(.*)', 'GetBookByTitle',
'/book/author/(.*)', 'GetBookByAuthor',
'/book/add', 'AddBook',
'/book/remove/(.*)', 'RemoveBook'
)
app = web.application(urls, globals())
class GetBookByTitle:
def GET(self, title):
book = get_book(title, cur)
if book:
return book
else:
return "Book not found"
class GetBookByAuthor:
def GET(self, author):
book = get_book(author, cur)
if book:
return book
else:
return "Book not found"
class AddBook:
def POST(self):
data = web.input()
success = add_book(data.title, data.author, data.picture, cur)
if success:
return "Book added successfully"
else:
return "Error adding book"
class RemoveBook:
def POST(self, title):
success = remove_book(title, cur)
if success:
return "Book removed successfully"
else:
return "Error removing book"
if __name__ == "__main__":
app.run()
close(cur, conn)