-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_cafe.py
61 lines (51 loc) · 1.69 KB
/
get_cafe.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
import pymysql
import config
from urllib import parse
def lambda_handler(event, context):
# db setting
try:
conn = pymysql.connect(
host=config.db_hostname,
user=config.db_username,
password=config.db_password,
db=config.db_name
)
except pymysql.MySQLError as e:
return {
"success": False,
"message": "Database Error"
}
cursor = conn.cursor(pymysql.cursors.DictCursor)
cafe_id = event['params']['path']['cafe-id']
sql = '\
SELECT c.id, c.name, count(r.id) as cnt_review, \
ROUND(AVG(r.star), 2) as avg_star, ROUND(AVG(r.noise), 1) as avg_noise, ROUND(AVG(r.light), 1) as avg_light, ROUND(AVG(r.chair), 1) as avg_chair \
FROM cafe as c \
LEFT JOIN review as r ON c.id = r.cafe_id \
WHERE c.id = ' + str(cafe_id)
cursor.execute(sql)
data = cursor.fetchall()
if not data[0]['name']:
return {
"success": False,
"message": "카페 정보가 존재하지 않습니다."
}
# 대표 이미지 url 조회
sql = '\
SELECT image_url as thumbnail \
FROM image \
WHERE cafe_id = %s \
ORDER BY id DESC LIMIT 1'
cursor.execute(sql, cafe_id)
thumbnail = cursor.fetchone()
if not thumbnail:
data[0]['thumbnail'] = config.default_image_url
else:
thumbnail_name = parse.quote(thumbnail.get('thumbnail').split(config.s3_base_url)[1])
data[0]['thumbnail'] = config.s3_base_url + thumbnail_name
conn.commit()
conn.close()
return {
"success": True,
"data": data[0]
}