UserPhoneDone
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:
124
apps/crud.py
124
apps/crud.py
@@ -1,38 +1,140 @@
|
||||
import io
|
||||
|
||||
from flask import Response
|
||||
from helpers import S3_BUCKET, get_secret, s3_client
|
||||
from flask import jsonify, request
|
||||
from helpers import init_db_connection, CORS_HEADERS
|
||||
from PIL import Image
|
||||
|
||||
|
||||
# Create&Update function to upload or update user avatar S3/Minio
|
||||
def create_phone(user_id: str, file):
|
||||
def create_phone(user_id: str, data):
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Câu truy vấn SQL để thêm số điện thoại vào bảng UserPhone
|
||||
query = """
|
||||
INSERT INTO UserPhone (user_id, PhoneNumber, Prefix, Created, Modified)
|
||||
VALUES (%s, %s, %s, NOW(), NOW())
|
||||
"""
|
||||
# Lấy các trường từ data
|
||||
phone_number = data.get("phone_number")
|
||||
prefix = data.get("prefix", None) # Nếu không có prefix, có thể để null
|
||||
|
||||
# Thực thi câu truy vấn SQL
|
||||
cursor.execute(query, (user_id, phone_number, prefix))
|
||||
conn.commit() # Lưu thay đổi vào cơ sở dữ liệu
|
||||
|
||||
return result, 200
|
||||
result = cursor.fetchall(),
|
||||
|
||||
return result, 200, CORS_HEADERS
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
|
||||
|
||||
|
||||
def get_phone(user_id: str): # Read function to get user avatar from S3/Minio
|
||||
def filter_phone(user_id: str, paging): # Read function to get user avatar from S3/Minio
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
#Xay dung dieu kien loc
|
||||
conditions = ["user_id = %(user_id)s"]
|
||||
values = {"user_id": user_id} # Điều kiện cơ bản cho user_id
|
||||
|
||||
# Lọc theo phone_number
|
||||
if paging.filter.phone_number:
|
||||
conditions.append("LOWER(PhoneNumber) LIKE %(phone_number)s")
|
||||
values["phone_number"] = f"%{paging.filter.phone_number.lower()}%"
|
||||
|
||||
return Response(
|
||||
# Lọc theo prefix
|
||||
if paging.filter.prefix:
|
||||
conditions.append("Prefix = %(prefix)s")
|
||||
values["prefix"] = paging.filter.prefix
|
||||
|
||||
# Lọc theo ngày tạo
|
||||
if paging.filter.created_from:
|
||||
conditions.append("Created >= %(created_from)s")
|
||||
values["created_from"] = paging.filter.created_from
|
||||
|
||||
if paging.filter.created_to:
|
||||
conditions.append("Created <= %(created_to)s")
|
||||
values["created_to"] = paging.filter.created_to
|
||||
|
||||
# Lọc theo ngày sửa đổi
|
||||
if paging.filter.modified_from:
|
||||
conditions.append("Modified >= %(modified_from)s")
|
||||
values["modified_from"] = paging.filter.modified_from
|
||||
|
||||
if paging.filter.modified_to:
|
||||
conditions.append("Modified <= %(modified_to)s")
|
||||
values["modified_to"] = paging.filter.modified_to
|
||||
|
||||
), 200
|
||||
|
||||
# Ket hop dieu kien loc
|
||||
where_clause = "AND".join(conditions)
|
||||
if where_clause:
|
||||
where_clause = "WHERE" + where_clause
|
||||
|
||||
#Sap xep ket qua neu co:
|
||||
order_clause=""
|
||||
if paging.sortby:
|
||||
direction="ASC" if paging.asc else "DESC"
|
||||
order_clause = f"ORDER BY {paging.sortby} {direction}"
|
||||
|
||||
# Gop Truy van
|
||||
sql = f"""
|
||||
SELECT *, COUNT(*) OVER() AS total
|
||||
FROM ailbl_user_phone
|
||||
{where_clause}
|
||||
{order_clause}
|
||||
LIMIT %(limit)s OFFSET%(offset)s
|
||||
"""
|
||||
values["limit"] = paging.size
|
||||
values["offset"] = paging.page * paging.size
|
||||
|
||||
cursor.execute(sql, values) # Thuc Thi Cau Truy Van
|
||||
phones = cursor.fetchall()
|
||||
|
||||
return phones, 200, CORS_HEADERS
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# Delete Function to delete user avatar from S3/Minio
|
||||
def delete_phone(user_id: str) -> dict:
|
||||
def delete_phone(phone_id: str, user_id: str) -> dict:
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
|
||||
return result, 200
|
||||
query = "DELETE FROM ailbl_user_phone WHERE id = %s AND user_id = %s"
|
||||
cursor.execute(query, (phone_id, user_id))
|
||||
conn.commit() # Save DB
|
||||
return {"message": "Phone deleted successfully"}, 200, CORS_HEADERS
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
|
||||
|
||||
def exists_phone( phone_id:str, user_id:str):
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT 1
|
||||
FROM ailbl_user_phone
|
||||
WHERE id = %s AND user_id = %s;
|
||||
""", (phone_id, user_id))
|
||||
|
||||
row = cursor.fetchone() # Co ket qua thi tra ve du lieu 1 dong
|
||||
return row is not None # Nếu có dòng dữ liệu, trả về True (tồn tại số điện thoại), nếu không, trả về False
|
||||
except Exception as e:
|
||||
return False
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user