-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
162 lines (122 loc) · 4.79 KB
/
db.py
File metadata and controls
162 lines (122 loc) · 4.79 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
import psycopg2
DB_CONFIG = {
"dbname": "tickets",
"user": "postgres",
"password": "admin",
"host": "localhost",
"port": "5432"
}
def connect_db():
return psycopg2.connect(**DB_CONFIG)
def init_db():
with connect_db() as conn, conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS events
(
id SERIAL PRIMARY KEY,
title VARCHAR(100) UNIQUE
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS seats
(
id SERIAL PRIMARY KEY,
seat_name VARCHAR(10) UNIQUE
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS tickets
(
id SERIAL PRIMARY KEY,
ticket_name VARCHAR(100) UNIQUE,
is_booked BOOLEAN NOT NULL DEFAULT FALSE
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS events_seats
(
id SERIAL PRIMARY KEY,
event_id INT REFERENCES events(id) ON DELETE CASCADE,
seat_id INT REFERENCES seats(id) ON DELETE CASCADE
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS events_tickets
(
id SERIAL PRIMARY KEY,
event_id INT REFERENCES events(id) ON DELETE CASCADE,
ticket_id INT REFERENCES tickets(id) ON DELETE CASCADE
);
""")
def get_all_events():
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM events")
return cur.fetchall()
def get_all_seats():
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM seats")
return cur.fetchall()
def get_all_tickets():
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM tickets")
return cur.fetchall()
def get_all_tickets_by_booking_status(is_booked):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM tickets WHERE is_booked = %s", (is_booked,))
return cur.fetchall()
def get_event_info_by_id(event_id):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM events WHERE id = %s", (event_id,))
event_info = cur.fetchone()
cur.execute("""
SELECT s.seat_name
FROM events_seats es
JOIN seats s ON es.seat_id = s.id
WHERE es.event_id = %s""", (event_id,))
seats = cur.fetchall()
cur.execute("""
SELECT t.ticket_name
FROM events_tickets et
JOIN tickets t ON et.ticket_id = t.id
WHERE et.event_id = %s""", (event_id,))
tickets = cur.fetchall()
return {
"event_info": event_info,
"seats": seats,
"tickets": tickets
}
def create_event(title):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("INSERT INTO events (title) VALUES(%s)", (title,))
def search_event(query):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM events WHERE title ILIKE %s;", (f"%{query}%",))
events = cur.fetchall()
return events
def delete_event(query_delete):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("DELETE FROM events WHERE title =%s", (query_delete,))
def edit_event(event_name, new_event_name):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("UPDATE events SET title = %s WHERE title = %s", (new_event_name, event_name,))
def create_seat(seat_name: int):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("INSERT INTO seats (seat_name) VALUES(%s)", (seat_name,)) # перепроверить табличку
def delete_seat(query_delete: int):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("DELETE FROM seats WHERE seat_name =%s", (query_delete,))
def edit_seat(seat_name, new_seat_name):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("UPDATE seats SET seat_name = %s WHERE seat_name = %s", (new_seat_name, seat_name,))
def search_seat(query):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM seats WHERE seat_name ILIKE %s;", (f"%{query}%",))
seats = cur.fetchall()
return seats
def search_ticket(query):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("SELECT * FROM tickets WHERE ticket_name ILIKE %s;", (f"%{query}%",))
return cur.fetchall()
def edit_ticket_booking(ticket_id, is_booked):
with connect_db() as conn, conn.cursor() as cur:
cur.execute("UPDATE tickets SET is_booked = %s WHERE id = %s", (is_booked, ticket_id,))