This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
184 lines (145 loc) · 5.53 KB
/
backend.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
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
import psycopg2
def connect():
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
def getCompany():
string = list()
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
cur.execute("SELECT * FROM company")
output = cur.fetchall()
# for i in output:
# string[i][0].append(i[0])
# string[i][1].append(i[1])
return output
def getCompanySearch(companyName):
string = list()
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
cur.execute("SELECT * FROM company WHERE name = '{}'".format(companyName))
output = cur.fetchall()
# for i in output:
# string[i][0].append(i[0])
# string[i][1].append(i[1])
return output
def addCompany(companyName, about):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
# print(companyName, about)
check = "SELECT EXISTS (SELECT 1 FROM COMPANY WHERE NAME = %s);"
cur.execute(check, (companyName,))
result = cur.fetchone()[0]
# return false if company exists
if result:
return False
# if problems close cur and conn and reopen for next operation
sql = "INSERT INTO COMPANY (NAME,ABOUT,RATE,NUM_RATE) VALUES (%s,%s,%s,%s);"
cur.execute(sql, (companyName, about, 0, 0))
conn.commit()
cur.close()
conn.close()
return True
def addReview(companyName, review, score, username):
print("Starting to add review for {}".format(companyName))
current = getCompanySearch(companyName)
# add review
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
sql = "INSERT INTO REVIEW (CNAME,REVIEW,SCORE,UNAME) VALUES (%s,%s,%s,%s);"
# print("sql statement: {}".format(sql))
cur.execute(sql, (companyName, review, score, username))
conn.commit()
cur.close()
# update company
newScore = (((current[0][2] * current[0][3]) + score) / (current[0][3] + 1))
reviewPlusPlus = (current[0][3] + 1)
cur = conn.cursor()
update = "UPDATE COMPANY SET RATE = %s, NUM_RATE = %s WHERE NAME = %s;"
# print("update statement: {}".format(update))
cur.execute(update, (newScore, reviewPlusPlus, companyName))
conn.commit()
cur.close()
conn.close()
def getReviews(companyName):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
command = "SELECT * FROM REVIEW WHERE CNAME = '{}';".format(companyName)
# print(command)
cur.execute(command)
reviews = cur.fetchall()
return reviews
def addCompany(companyName, about):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
# print(companyName, about)
check = "SELECT EXISTS (SELECT 1 FROM COMPANY WHERE NAME = '{}');".format(companyName)
cur.execute(check)
result = cur.fetchone()[0]
# return false if company exists
if result:
return False
# if problems close cur and conn and reopen for next operation
sql = "INSERT INTO COMPANY (NAME,ABOUT,RATE,NUM_RATE) VALUES (%s,%s,%s,%s);"
cur.execute(sql, (companyName, about, 0, 0))
conn.commit()
cur.close()
conn.close()
return True
def getReviews(companyName):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
command = "SELECT * FROM REVIEW WHERE CNAME = '{}';".format(companyName)
cur.execute(command)
reviews = cur.fetchall()
cur.close()
conn.close()
return reviews
def addUser(userName, password, email, fName, lName):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
check = "SELECT EXISTS (SELECT 1 FROM USERS WHERE UNAME = '{}');".format(userName)
cur.execute(check)
result = cur.fetchone()[0]
# return false if user exists
if result:
return False
command = "INSERT INTO USERS (UNAME, PASS, EMAIL, NUM_REVIEWS, FNAME, LNAME) VALUES ('{}','{}','{}', {},'{}','{}')".format(
userName, password, email, 0, fName, lName)
# print("Insert user command: {}".format(command))
cur.execute(command)
conn.commit()
cur.close()
conn.close()
return True
def checkPassword(email, password):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
check = "SELECT EXISTS (SELECT 1 FROM USERS WHERE EMAIL = '{}');".format(email)
# print(check)
cur.execute(check)
result = cur.fetchone()[0]
# print(result)
# return false if user exists
if not result:
return False
command = "SELECT PASS FROM USERS WHERE EMAIL = '{}';".format(email)
# print(command)
cur.execute(command)
correctPass = cur.fetchone()
cur.close()
conn.close()
# print("User: {} Psql: {}".format(password, correctPass))
if password == correctPass[0]:
# print("And all is right with the world")
return True
else:
# print('All is dispair once again')
return False
def userForEmail(email):
conn = psycopg2.connect("dbname=theCellar user=postgres password=steve host=localhost")
cur = conn.cursor()
command = "SELECT UNAME FROM USERS WHERE EMAIL = '{}';".format(email)
cur.execute(command)
username = cur.fetchone()
cur.close()
conn.close()
return username[0]