Files
py-ailbl-user-profile/apps/crud.py

103 lines
3.0 KiB
Python
Raw Normal View History

2025-12-04 16:24:54 +07:00
import io
from flask import Response
2025-12-08 07:50:09 +00:00
from helpers import init_db_connection, CORS_HEADERS
2025-12-04 16:24:54 +07:00
2025-12-08 10:06:44 +00:00
def get_profile(id):
2025-12-04 16:24:54 +07:00
try:
2025-12-08 07:50:09 +00:00
conn = init_db_connection()
cursor = conn.cursor()
2025-12-04 16:24:54 +07:00
2025-12-08 07:50:09 +00:00
# Truy van thong tin nguoi dung tu bang ailbl_user_profiles
2025-12-08 10:06:44 +00:00
query = "SELECT * FROM ailbl_user_profile WHERE id = %s"
cursor.execute(query, (id,))
2025-12-08 07:50:09 +00:00
profile = cursor.fetchone() # fetchone la gi ?
2025-12-04 16:24:54 +07:00
2025-12-08 07:50:09 +00:00
if profile:
return {
2025-12-08 10:06:44 +00:00
"id": profile[0],
2025-12-08 07:50:09 +00:00
"first_name": profile[1],
"last_name": profile[2],
"dob": profile[3],
"gender": profile[4],
2025-12-08 10:06:44 +00:00
"created": profile[5],
"modified": profile[6]
2025-12-08 07:50:09 +00:00
}, 200, CORS_HEADERS
else:
return {"error": "Profile not found"}, 404, CORS_HEADERS
2025-12-04 16:24:54 +07:00
except Exception as e:
2025-12-08 07:50:09 +00:00
return {"error": str(e)}, 500, CORS_HEADERS
finally:
if conn:
conn.close()
2025-12-04 16:24:54 +07:00
2025-12-08 10:06:44 +00:00
def update_profile(id, data):
2025-12-04 16:24:54 +07:00
try:
2025-12-08 07:50:09 +00:00
conn = init_db_connection()
cursor = conn.cursor()
# Cap nhat thong tin nguoi dung trong bang ailbl_user_profiles
query = """
2025-12-08 10:06:44 +00:00
UPDATE ailbl_user_profile
SET first_name = %s, last_name = %s, dob = %s, gender = %s, modified = NOW()
WHERE id = %s
2025-12-08 07:50:09 +00:00
"""
cursor.execute(query, (
data.get("first_name"),
data.get("last_name"),
data.get("dob"),
data.get("gender"),
2025-12-08 10:06:44 +00:00
id
2025-12-08 07:50:09 +00:00
))
conn.commit()
return {"message": "Profile updated successfully"}, 200, CORS_HEADERS
2025-12-04 16:24:54 +07:00
2025-12-08 07:50:09 +00:00
except Exception as e:
return {"error": str(e)}, 500, CORS_HEADERS
finally:
if conn:
conn.close()
2025-12-04 16:24:54 +07:00
2025-12-08 10:06:44 +00:00
def insert_profile(id, data):
2025-12-08 07:50:09 +00:00
try:
conn = init_db_connection()
cursor = conn.cursor()
2025-12-08 10:06:44 +00:00
# Tao moi thong tin nguoi dung trong bang ailbl_user_profile
2025-12-08 07:50:09 +00:00
query = """
2025-12-08 10:06:44 +00:00
INSERT INTO ailbl_user_profile (id, first_name, last_name, dob, gender, created, modified)
VALUES (%s, %s, %s, %s, %s, NOW(), NOW())
2025-12-08 07:50:09 +00:00
"""
cursor.execute(query, (
2025-12-08 10:06:44 +00:00
id,
2025-12-08 07:50:09 +00:00
data.get("first_name"),
data.get("last_name"),
data.get("dob"),
data.get("gender"),
))
conn.commit()
return {"message": "Profile created successfully"}, 201, CORS_HEADERS
2025-12-04 16:24:54 +07:00
except Exception as e:
2025-12-08 07:50:09 +00:00
return {"error": str(e)}, 500, CORS_HEADERS
finally:
if conn:
conn.close()
2025-12-04 16:24:54 +07:00
2025-12-08 10:06:44 +00:00
def delete_profile(id):
2025-12-04 16:24:54 +07:00
try:
2025-12-08 07:50:09 +00:00
conn = init_db_connection()
cursor = conn.cursor()
# Xoa thong tin nguoi dung trong bang ailbl_user_profiles
2025-12-08 10:06:44 +00:00
query = "DELETE FROM ailbl_user_profile WHERE id = %s"
cursor.execute(query, (id,))
2025-12-08 07:50:09 +00:00
conn.commit()
return {"message": "Profile deleted successfully"}, 200, CORS_HEADERS
2025-12-04 16:24:54 +07:00
except Exception as e:
2025-12-08 07:50:09 +00:00
return {"error": str(e)}, 500, CORS_HEADERS
finally:
if conn:
conn.close()