From 73e7d261e4c571c8125fa9c97c83add09756d4ae Mon Sep 17 00:00:00 2001 From: alexalmeida2001 <47391414+alexalmeida2001@users.noreply.github.com> Date: Sun, 6 Dec 2020 12:30:47 +0000 Subject: [PATCH] Update database.py --- database.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/database.py b/database.py index 3bc5a91..95b58e7 100644 --- a/database.py +++ b/database.py @@ -23,7 +23,8 @@ def log_in(email, password): if results: for i in results: - print("Welcome " + i[1]) + print("Welcome " + i[1]+"your balance is") + return 1 else: print("Email and password not recognised") @@ -55,7 +56,15 @@ def view_movieinfo(filme): movies = c.fetchall() for x in movies: print(x) - results = c.fetchall() + + if movies: + for i in movies: + break + else: + print("\n---movie name not correct---\n") + return(0) + + return(x[1]) conn.commit() conn.close() @@ -86,7 +95,66 @@ def compra_filme(custo,saldo,email): c = conn.cursor() saldofinal= saldo-custo c.execute("UPDATE users SET balance=(%s) WHERE email='"+email+"'",(saldofinal,)) + conn.commit() + conn.close() + +def create_rent(custo,email,moviename): + conn = psycopg2.connect("host=localhost dbname=projeto user=postgres password=postgres") + c = conn.cursor() + timeavaible=c.execute("SELECT * FROM movies WHERE name ='"+moviename+"'") + results = c.fetchall() + for x in results: + break + + userid=c.execute("SELECT * FROM users WHERE email ='"+email+"'") + results2 = c.fetchall() + for y in results2: + break + + + type=x[9] + c.execute("INSERT INTO rent(clientid,date,price,dateend,usermail,timeavaible,type,movieid) VALUES ( %s, CURRENT_TIMESTAMP , %s, CURRENT_TIMESTAMP + %s * INTERVAL '1 month','"+email+"',%s,'"+type+"',%s)",(y[4],custo,x[8],x[8],x[0])) #falta tempo aos filmes + + c.execute("SELECT date + timeavaible * INTERVAL '1 month' FROM rent;") + results3 = c.fetchall() + for z in results3: + break + + # c.execute("INSERT INTO rent(dateend) VALUES('z')") + + conn.commit() + conn.close() + +def view_rent(email): + conn = psycopg2.connect("host=localhost dbname=projeto user=postgres password=postgres") + c = conn.cursor() + c.execute("SELECT * FROM rent WHERE usermail = '" + email + "'") + results = c.fetchall() + for x in results: + print(x) conn.commit() conn.close() + + +def view_avaible_movies(email): + conn = psycopg2.connect("host=localhost dbname=projeto user=postgres password=postgres") + c = conn.cursor() + c.execute("SELECT * FROM rent WHERE dateend >= CURRENT_TIMESTAMP AND usermail='"+email+"'") + results = c.fetchall() + id = 0 + for x in results: + #print(x[8]) + id=x[8] + c.execute("SELECT * FROM movies WHERE itemid = " + str(x[8])) + movies = c.fetchall() + for y in movies: + print(y[1]) + #results2 = c.fetchall() + #print("movies and tv shows available:") + #for y in results2: + # print(y[2]) + print("movies not available any more:") + c.execute("SELECT * FROM rent WHERE dateend <= CURRENT_TIMESTAMP AND usermail='" + email + "'") + results = c.fetchall()