-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie.py
50 lines (45 loc) · 1.5 KB
/
movie.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
import uuid
import sqlite3
class Movie:
def __init__(self, movie_id, title, rating, cinema_id):
self.movie_id = movie_id
self.title = title
self.rating = rating
self.cinema_id = cinema_id
@staticmethod
def create_movie(title, cinema_id, db='cinema.db'):
movie_id = str(uuid.uuid4())
movie = Movie(movie_id, title, 0.0, cinema_id)
movie.save_to_db(db)
return movie
def save_to_db(self, db='cinema.db'):
conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO movies (movie_id, title, rating, cinema_id)
VALUES (?, ?, ?, ?)
''', (self.movie_id, self.title, self.rating, self.cinema_id))
conn.commit()
conn.close()
@staticmethod
def get_movie_by_id(movie_id, db='cinema.db'):
conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM movies WHERE movie_id = ?
''', (movie_id,))
movie_data = cursor.fetchone()
conn.close()
if movie_data:
return Movie(*movie_data)
return None
@staticmethod
def get_movies_by_cinema_id(cinema_id, db='cinema.db'):
conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM movies WHERE cinema_id = ?
''', (cinema_id,))
movies_data = cursor.fetchall()
conn.close()
return [Movie(*movie_data) for movie_data in movies_data]