-
Notifications
You must be signed in to change notification settings - Fork 0
/
customers.py
75 lines (68 loc) · 2.81 KB
/
customers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
import sqlite3
class Customers:
"""
Class representing a customer of a restaurant.
"""
def __init__(self):
"""
Initialize a customer instance.
"""
self.conn = sqlite3.connect("pizza_restaurant.db")
self.cursor = self.conn.cursor()
self.create_table()
def create_table(self):
"""
Creates necessary tables if they do not exist yet.
:return: None
"""
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(200) NOT NULL,
address TEXT NOT NULL
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS temp_customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
table_no TINYINT NOT NULL,
first_name VARCHAR(100),
last_name VARCHAR(100)
)
''')
self.conn.commit()
def add_perm_customer(self, first_name, last_name, email, address):
"""
Adds a permanent customer to the database.
:param first_name: (str) First name of the customer.
:param last_name: (str) Last name of the customer.
:param email: (str) Email of the customer.
:param address: (str) The address of the customer.
:return: Last row of the cursor position
"""
self.cursor.execute('INSERT INTO customers (first_name, last_name, email, address) VALUES (?,?,?,?)',
(first_name, last_name, email, address))
self.conn.commit()
return self.cursor.lastrowid
def add_temp_customer(self, table_no, first_name, last_name):
"""
Adds a permanent customer to the database.
:param table_no: (int) Table no of the customer.
:param first_name: (str) First name of the customer.
:param last_name: (str) Last name of the customer.
:return: Last row of the cursor position
"""
self.cursor.execute('INSERT INTO temp_customers (table_no, first_name, last_name) VALUES (?,?,?)',
(table_no, first_name, last_name))
self.conn.commit()
return self.cursor.lastrowid
def get_table_no(self, customer_id):
"""
Returns the table no of the customer who has the exact customer id with the parameter.
:param customer_id: id of the customer.
:return: Table no
"""
table_no = self.cursor.execute('SELECT table_no FROM temp_customers WHERE id=?', (customer_id,)).fetchone()
return table_no[0] if table_no else None