-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud.py
384 lines (224 loc) · 9.87 KB
/
crud.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""CRUD operations"""
from model import (db, User, Game, UserGame, ListedGame, WantedGame,
Mechanic, GameMechanic, Category, GameCategory, Publisher,
GamePublisher, Designer, GameDesigner, Artist, GameArtist,
connect_to_db)
def create_user(username, fname, lname, email, password, birthdate=None):
"""Create and return a new user"""
user = User(username=username, fname=fname, lname=lname,
email=email, password=password, birthdate=birthdate)
db.session.add(user)
db.session.commit()
return user
def lookup_user(username):
"""Returns True if username exists in User table"""
user = User.query.filter_by(username=username).first()
return user
def lookup_email(email):
"""Returns True if email exists in User table"""
email = User.query.filter_by(email=email).first()
return email
def get_email_by_username(username):
"""Takes in username and returns email of matching user"""
user = User.query.filter_by(username=username).first()
return user.email
def get_password(username):
"""Takes in username and returns matching user's password"""
password = db.session.query(User.password).filter_by(
username=username).one()
return password[0]
def create_game(name, description=None, publish_year=None, min_age=None,
min_players=None, max_players=None,
min_playtime=None, max_playtime=None,
image_url=None, msrp=None, atlas_id=None):
"""Create and return a new game"""
game = Game(name=name, description=description, publish_year=publish_year,
min_age=min_age, min_players=min_players,
max_players=max_players, min_playtime=min_playtime,
max_playtime=max_playtime, image_url=image_url, msrp=msrp,
atlas_id=atlas_id)
db.session.add(game)
db.session.commit()
return game
def get_game_by_name(name):
"""Takes in string and finds match with Game in db, if any"""
existing_game = db.session.query(Game).filter_by(name=name).first()
if existing_game:
return existing_game
else:
return None
def get_game_by_atlas_id(atlas_id):
"""Takes in atlas_id and returns matching Game, if any"""
existing_game = db.session.query(Game).filter_by(atlas_id=atlas_id).first()
if existing_game:
return existing_game
else:
return None
# def get_game_details(user_game):
# """Takes in UserGame and returns corresponding"""
# # url, name, min_age, min_players, max_players = db.session.query(Game.image_url, Game.name, Game.min_age, Game.min_players, Game.max_players).join(UserGame).filter(
# # UserGame.id==user_game.id).one()
# game = db.session.query(Game).select_from(Game).join(UserGame).join(GamePublisher).join(Publisher).filter(UserGame.id==user_game.id).one()
# return game
def create_user_game(user_id, game_id, own=True):
"""Create and return an instance of game owned by user"""
user_game = UserGame(user_id=user_id, game_id=game_id, own=own)
db.session.add(user_game)
db.session.commit()
return user_game
def get_user_game_by_id(id):
"""Takes in id and finds corresponding UserGame"""
user_game = UserGame.query.get(id)
return user_game
def get_user_current_own_games(username):
"""Takes in username and returns user's UserGames"""
own_games = db.session.query(UserGame).join(User).filter(
User.username==username, UserGame.own==True).all()
return own_games
def get_user_ever_own_games(username):
"""Takes in username and returns UserGames ever owned"""
ever_own_games = db.session.query(UserGame).join(User).filter(
User.username==username).all()
return ever_own_games
def update_user_game_to_false(id):
"""Finds UserGame by id and updates Own boolean to false"""
user_game = UserGame.query.get(id)
user_game.own = False
db.session.commit()
return user_game
def create_listed_game(user_games_id, condition, price, comment=None, active=True):
"""Takes ID of existing UserGame object and creates ListedGame object"""
listed_game = ListedGame(user_games_id=user_games_id, condition=condition,
price=price, comment=comment, active=active)
db.session.add(listed_game)
db.session.commit()
return listed_game
def update_listed_game(user_game_id, condition, price, comment=None):
"""Takes ID of existing UserGame, updates, and returns updated ListedGame"""
listed_game = db.session.query(ListedGame).filter_by(
user_games_id=user_game_id).one()
listed_game.condition = condition
listed_game.price = price
listed_game.comment = comment
db.session.commit()
updated_game = db.session.query(ListedGame).filter_by(
user_games_id=user_game_id).one()
return updated_game
def update_listed_game_to_false(user_game_id):
"""Finds ListedGame by user_games_id and updates Active boolean to false"""
listed_game = ListedGame.query.filter_by(user_games_id=user_game_id).first()
if listed_game:
listed_game.active = False
db.session.commit()
return listed_game
def update_listed_game_to_true(user_game_id):
"""Finds ListedGame by user_games_id and updates Active boolean to true"""
listed_game = ListedGame.query.filter_by(user_games_id=user_game_id).one()
listed_game.active = True
db.session.commit()
return listed_game
def get_user_listed_games(username):
"""Takes in a username and returns user's listed games"""
listed_games = db.session.query(ListedGame).select_from(
ListedGame).join(UserGame).join(User).join(
Game).filter(User.username==username,
ListedGame.active==True).all()
return listed_games
def get_marketplace_listings(search_terms):
"""Returns all ListedGames from all Users"""
listed_games = db.session.query(ListedGame).select_from(ListedGame).join(
UserGame).join(Game).filter(
ListedGame.active==True, Game.name.ilike(f'%{search_terms}%')).all()
return listed_games
def get_listing_details(listing_id):
"""Takes in listing ID and returns ListedGame object"""
listed_game = db.session.query(ListedGame).filter_by(id=listing_id).one()
return listed_game
def delete_listed_game(id):
"""Finds ListedGame by id and removes row"""
listed_game = ListedGame.query.filter_by(user_games_id=id).first()
db.session.delete(listed_game)
db.session.commit()
def get_listed_game_by_id(user_game_id):
"""Takes in a UserGame id and returns a matching ListedGame, if any"""
listed_game = db.session.query(ListedGame).filter_by(
user_games_id=user_game_id).first()
if listed_game:
return listed_game
else:
return None
def create_wanted_game(user_id, game_id):
"""Create and return an instance of game wanted by user"""
wanted_game = WantedGame(user_id=user_id, game_id=game_id)
db.session.add(wanted_game)
db.session.commit()
return wanted_game
def get_user_wanted_games(username):
"""Takes in username and returns user's WantedGames"""
wanted_games = db.session.query(WantedGame).join(User).filter(
User.username==username).all()
return wanted_games
def delete_wanted_game(id):
"""Finds WantedGame by id and removes row"""
wanted_game = WantedGame.query.get(id)
db.session.delete(wanted_game)
db.session.commit()
#####Removed Rating component####
# def create_rating(user_id, game_id, rating, comment=None):
# """Create and return a game rating"""
# rating = Rating(user_id=user_id, game_id=game_id,
# rating=rating, comment=comment)
# db.session.add(rating)
# db.session.commit()
# return rating
def create_mechanic(name, atlas_id=None):
"""Create and return a new gaming mechanic"""
mechanic = Mechanic(name=name, atlas_id=atlas_id)
db.session.add(mechanic)
db.session.commit()
return mechanic
def create_game_mechanic(game_id, mechanic_id):
"""Create and return a new game tagged with mechanic instance"""
game_mechanic = GameMechanic(game_id=game_id, mechanic_id=mechanic_id)
db.session.add(game_mechanic)
db.session.commit()
return game_mechanic
def create_category(name, atlas_id=None):
"""Create and return a new gaming category"""
category = Category(name=name, atlas_id=atlas_id)
db.session.add(category)
db.session.commit()
return category
def create_game_category(game_id, category_id):
"""Create and return a new game tagged with category instance"""
game_category = GameCategory(game_id=game_id, category_id=category_id)
db.session.add(game_category)
db.session.commit()
return game_category
def create_publisher(name):
"""Create and return a new gaming category"""
publisher = Publisher(name=name)
db.session.add(publisher)
db.session.commit()
return publisher
def create_game_publisher(game_id, publisher_id):
"""Create and return a new game tagged with publisher instance"""
game_publisher = GamePublisher(game_id=game_id, publisher_id=publisher_id)
db.session.add(game_publisher)
db.session.commit()
return game_publisher
def create_designer(name):
"""Create and return a new game designer"""
designer = Designer(name=name)
db.session.add(designer)
db.session.commit()
return designer
def create_game_designer(game_id, designer_id):
"""Create and return a new game tagged with designer instance"""
game_designer = GameDesigner(game_id=game_id, designer_id=designer_id)
db.session.add(game_designer)
db.session.commit()
return game_designer
if __name__ == '__main__':
from server import app
connect_to_db(app)