FixNameFile
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:
135
apps/crud.py
135
apps/crud.py
@@ -1,47 +1,55 @@
|
||||
import io
|
||||
|
||||
from filters import PhonePage
|
||||
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, 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())
|
||||
INSERT INTO ailbl_user_phone (user_id, phone_number, prefix, area_code, created, modified)
|
||||
VALUES (%s, %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
|
||||
|
||||
# Nếu không có prefix, có thể để null
|
||||
prefix = data.get("prefix", None)
|
||||
area_code = data.get("area_code")
|
||||
# Thực thi câu truy vấn SQL
|
||||
cursor.execute(query, (user_id, phone_number, prefix))
|
||||
cursor.execute(query, (user_id, phone_number, prefix, area_code))
|
||||
conn.commit() # Lưu thay đổi vào cơ sở dữ liệu
|
||||
|
||||
result = cursor.fetchall(),
|
||||
|
||||
return result, 200, CORS_HEADERS
|
||||
return {
|
||||
"user_id": user_id,
|
||||
"phone_number": phone_number,
|
||||
"prefix": prefix,
|
||||
"area": area_code,
|
||||
"status": "added"
|
||||
}, 200, CORS_HEADERS
|
||||
|
||||
# result = cursor.fetchall()
|
||||
# return {"message": "Phone created successfully"}, 200, CORS_HEADERS
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
|
||||
return {"error": str(e)}, 500, CORS_HEADERS
|
||||
|
||||
|
||||
def filter_phone(user_id: str, paging): # Read function to get user avatar from S3/Minio
|
||||
def filter_phone(user_id: str, paging):
|
||||
conn = None # Khởi tạo conn với giá trị None
|
||||
cursor = None
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
#Xay dung dieu kien loc
|
||||
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")
|
||||
@@ -69,72 +77,95 @@ def filter_phone(user_id: str, paging): # Read function to get user avatar from
|
||||
if paging.filter.modified_to:
|
||||
conditions.append("Modified <= %(modified_to)s")
|
||||
values["modified_to"] = paging.filter.modified_to
|
||||
|
||||
|
||||
# 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"
|
||||
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
|
||||
"""
|
||||
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
|
||||
|
||||
cursor.execute(sql, values) # Thuc Thi Cau Truy Van
|
||||
phones = cursor.fetchall()
|
||||
|
||||
|
||||
# Chuyển kết quả thành danh sách các đối tượng với tên trường rõ ràng
|
||||
phone_list = []
|
||||
for phone in phones:
|
||||
phone_dict = {
|
||||
"id": phone[0],
|
||||
"user_id": phone[1],
|
||||
"phone_number": phone[2],
|
||||
"prefix": phone[3],
|
||||
"area_code": phone[4],
|
||||
"created": phone[5],
|
||||
"modified": phone[6]
|
||||
}
|
||||
phone_list.append(phone_dict)
|
||||
|
||||
return phones, 200, CORS_HEADERS
|
||||
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}, 500
|
||||
finally:
|
||||
if conn:
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# Delete Function to delete user avatar from S3/Minio
|
||||
def delete_phone(phone_id: str, user_id: str) -> dict:
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
|
||||
query = "DELETE FROM ailbl_user_phone WHERE id = %s AND user_id = %s"
|
||||
cursor.execute(query, (phone_id, user_id))
|
||||
conn.commit() # Save DB
|
||||
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):
|
||||
def exists_phone(user_id: str, phone_number: str = None, phone_id: str = None):
|
||||
try:
|
||||
conn = init_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT 1
|
||||
FROM ailbl_user_phone
|
||||
WHERE id = %s AND user_id = %s;
|
||||
|
||||
if phone_number: # Nếu là `POST`, kiểm tra sự tồn tại của phone_number
|
||||
cursor.execute("""
|
||||
SELECT 1
|
||||
FROM ailbl_user_phone
|
||||
WHERE user_id = %s AND phone_number = %s
|
||||
""", (user_id, phone_number))
|
||||
|
||||
elif phone_id: # Nếu là `DELETE`, kiểm tra sự tồn tại của phone_id
|
||||
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
|
||||
|
||||
row = cursor.fetchone() # Co ket qua thi tra ve du lieu 1 dong
|
||||
# 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
|
||||
return row is not None
|
||||
except Exception as e:
|
||||
return False
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if conn:
|
||||
conn.close() # Đảm bảo đóng kết nối sau khi xong
|
||||
|
||||
Reference in New Issue
Block a user