from fastapi import APIRouter from sqlalchemy import text from database.connection import engine from services.datasets.delete import delete_dataset_from_partition # import fungsi di atas from response import successRes, errorRes router = APIRouter() def serialize_row(row_dict): new_dict = {} for key, value in row_dict.items(): if hasattr(value, "isoformat"): new_dict[key] = value.isoformat() else: new_dict[key] = value return new_dict @router.get("/metadata") async def get_author_metadata( # user = Depends(get_current_user) ): """ Mengambil data author_metadata: - Admin → semua data - User → hanya data miliknya """ try: async with engine.begin() as conn: # if user.role == "admin": # query = text(""" # SELECT * # FROM backend.author_metadata # ORDER BY created_at DESC # """) # result = await conn.execute(query) # else: # query = text(""" # SELECT * # FROM backend.author_metadata # WHERE user_id = :uid # ORDER BY created_at DESC # """) # result = await conn.execute(query, {"uid": user.id}) # rows = result.fetchall() query = text(""" SELECT * FROM backend.author_metadata ORDER BY created_at DESC """) result = await conn.execute(query) rows = result.fetchall() # data = [dict(row._mapping) for row in rows] data = [serialize_row(dict(row._mapping)) for row in rows] return successRes( message="Berhasil mengambil data author metadata", data=data ) except Exception as e: print(f"[ERROR] Gagal ambil author_metadata: {e}") raise errorRes( status_code=500, message="Gagal mengambil data author_metadata", details=str(e) ) @router.delete("/delete/{user_id}/{metadata_id}") async def delete_dataset(user_id: int, metadata_id: int, title: str): """ Hapus dataset tertentu (berdasarkan user_id dan metadata_id) """ try: async with engine.begin() as conn: await delete_dataset_from_partition(conn, user_id, metadata_id, title) return successRes(message=f"Dataset {title} berhasil dihapus.", data="") except Exception as e: print(f"[ERROR] Gagal hapus dataset: {e}") raise errorRes(status_code=500, details=str(e), message="Gagal hapus dataset")