CRUD_UserProfile
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 21s
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 21s
This commit is contained in:
140
apps/crud.py
140
apps/crud.py
@@ -1,65 +1,109 @@
|
||||
import io
|
||||
|
||||
from flask import Response
|
||||
from helpers import S3_BUCKET, get_secret, s3_client
|
||||
from helpers import init_db_connection, CORS_HEADERS
|
||||
from PIL import Image
|
||||
|
||||
|
||||
# Create&Update function to upload or update user avatar S3/Minio
|
||||
def update_or_create_avatar(user_id: str, file):
|
||||
def get_profile(user_id):
|
||||
try:
|
||||
file_data = file.read()
|
||||
# Bản chất là đường dẫn trong bucket + tên file = user_id
|
||||
object_name = f"{get_secret('S3_PREFIX')}/{user_id}"
|
||||
result = s3_client.put_object(
|
||||
Bucket=S3_BUCKET,
|
||||
Key=object_name,
|
||||
Body=io.BytesIO(file_data),
|
||||
ContentLength=len(file_data),
|
||||
ContentType=file.content_type,
|
||||
)
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
return result, 200
|
||||
# Truy van thong tin nguoi dung tu bang ailbl_user_profiles
|
||||
query = "SELECT * FROM ailbl_user_profiles WHERE user_id = %s"
|
||||
cursor.execute(query, (user_id,))
|
||||
profile = cursor.fetchone() # fetchone la gi ?
|
||||
|
||||
if profile:
|
||||
return {
|
||||
"user_id": profile[0],
|
||||
"first_name": profile[1],
|
||||
"last_name": profile[2],
|
||||
"dob": profile[3],
|
||||
"gender": profile[4],
|
||||
"address": profile[5],
|
||||
"phone": profile[6],
|
||||
"created": profile[7],
|
||||
"modified": profile[8]
|
||||
}, 200, CORS_HEADERS
|
||||
else:
|
||||
return {"error": "Profile not found"}, 404, CORS_HEADERS
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500, CORS_HEADERS
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
def update_profile(user_id, data):
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
# Cap nhat thong tin nguoi dung trong bang ailbl_user_profiles
|
||||
query = """
|
||||
UPDATE ailbl_user_profiles
|
||||
SET first_name = %s, last_name = %s, dob = %s, gender = %s, address = %s, phone = %s, modified = NOW()
|
||||
WHERE user_id = %s
|
||||
"""
|
||||
cursor.execute(query, (
|
||||
data.get("first_name"),
|
||||
data.get("last_name"),
|
||||
data.get("dob"),
|
||||
data.get("gender"),
|
||||
data.get("address"),
|
||||
data.get("phone"),
|
||||
user_id
|
||||
))
|
||||
conn.commit()
|
||||
return {"message": "Profile updated successfully"}, 200, CORS_HEADERS
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
return {"error": str(e)}, 500, CORS_HEADERS
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_avatar_url(user_id: str): # Read function to get user avatar from S3/Minio
|
||||
def insert_profile(user_id, data):
|
||||
try:
|
||||
response = s3_client.get_object(
|
||||
Bucket=S3_BUCKET,
|
||||
Key=f"{get_secret('S3_PREFIX')}/{user_id}"
|
||||
)
|
||||
# image_data = response["body"].read(content_type)
|
||||
image_data = response['Body'].read()
|
||||
|
||||
with Image.open(io.BytesIO(image_data)) as img:
|
||||
fmt = img.format.lower() # ví dụ: 'jpeg', 'png', 'webp'
|
||||
content_type = f"image/{'jpeg' if fmt == 'jpg' else fmt}"
|
||||
# return Response(
|
||||
# io.BytesIO(image_data),
|
||||
# content_type=content_type,
|
||||
# direct_passthrough=True,
|
||||
# )
|
||||
|
||||
return Response(
|
||||
image_data,
|
||||
content_type=content_type,
|
||||
direct_passthrough=True
|
||||
), 200
|
||||
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
# Tao moi thong tin nguoi dung trong bang ailbl_user_profiles
|
||||
query = """
|
||||
INSERT INTO ailbl_user_profiles (user_id, first_name, last_name, dob, gender, address, phone, created, modified)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, NOW(), NOW())
|
||||
"""
|
||||
cursor.execute(query, (
|
||||
user_id,
|
||||
data.get("first_name"),
|
||||
data.get("last_name"),
|
||||
data.get("dob"),
|
||||
data.get("gender"),
|
||||
data.get("address"),
|
||||
data.get("phone")
|
||||
))
|
||||
conn.commit()
|
||||
return {"message": "Profile created successfully"}, 201, CORS_HEADERS
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
return {"error": str(e)}, 500, CORS_HEADERS
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# Delete Function to delete user avatar from S3/Minio
|
||||
def delete_avatar(user_id: str) -> dict:
|
||||
def delete_profile(user_id):
|
||||
try:
|
||||
result = s3_client.delete_object(
|
||||
Bucket=S3_BUCKET,
|
||||
Key=f"{get_secret('S3_PREFIX')}/{user_id}"
|
||||
)
|
||||
return result, 200
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Xoa thong tin nguoi dung trong bang ailbl_user_profiles
|
||||
query = "DELETE FROM ailbl_user_profiles WHERE user_id = %s"
|
||||
cursor.execute(query, (user_id,))
|
||||
|
||||
conn.commit()
|
||||
return {"message": "Profile deleted successfully"}, 200, CORS_HEADERS
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
return {"error": str(e)}, 500, CORS_HEADERS
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user