-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
94 lines (56 loc) · 2.14 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
"""CRUD operations."""
from model import db, User, Restaurant, Like, connect_to_db
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func, desc
def create_user(email, password=None):
"""Create and return a new user."""
user = User(email=email, password=password)
db.session.add(user)
db.session.commit()
return user
def create_restaurant(yelp_id):
"""Create and return a new restaurant."""
res = Restaurant(yelp_id=yelp_id)
db.session.add(res)
db.session.commit()
return res
def create_like(user, res, group_name=None):
""" Create a like """
like = Like(user=user, res=res, group_name=group_name)
db.session.add(like)
db.session.commit()
return like
def get_all_users():
"""Return all users."""
return User.query.all()
def get_all_restaurants():
""" Return all restaurants"""
return Restaurant.query.all()
def get_user_by_id(user_id):
"""Return user by id."""
return User.query.get(user_id)
def get_user_by_email(email):
""" Return user by email """
return User.query.filter(User.email == email).first()
def password_match(email, password):
"""Check if password matches email."""
return User.query.filter(User.email == email,
User.password == password).first() != None
def email_exist(email):
""" Check if email matches"""
return User.query.filter(User.email == email).first() != None
def get_restaurant_by_id(yelp_id):
"""Return restaurant by id."""
return Restaurant.query.get(yelp_id)
def get_like(user, res, group_name=None):
""" Return like by user and restaurant """
return Like.query.filter(Like.user == user, Like.res == res, Like.group_name == group_name).first()
def delete_like(like):
"""Delete a like"""
db.session.delete(like)
db.session.commit()
def get_restaurant_with_most_likes(group_name):
return Like.query.with_entities(Like.yelp_id, func.count(Like.yelp_id).label('count_like')).filter(Like.group_name == group_name).group_by(Like.yelp_id).order_by(desc('count_like')).first()
if __name__ == '__main__':
from server import app
connect_to_db(app)