150 lines
2.9 KiB
Python
150 lines
2.9 KiB
Python
from fastapi import (
|
|
APIRouter,
|
|
Depends,
|
|
HTTPException,
|
|
UploadFile,
|
|
File
|
|
)
|
|
|
|
from sqlalchemy.orm import Session
|
|
import pandas as pd
|
|
from io import StringIO
|
|
|
|
from ..database import get_db
|
|
from ..models import Komentar
|
|
from ..schemas import KomentarCreate, KomentarResponse
|
|
|
|
|
|
router = APIRouter(
|
|
prefix="/data",
|
|
tags=["Kelola Data"]
|
|
)
|
|
|
|
|
|
# =========================================================
|
|
# GET SEMUA DATA
|
|
# =========================================================
|
|
|
|
@router.get("/", response_model=list[KomentarResponse])
|
|
def get_all_data(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
data = db.query(Komentar).all()
|
|
|
|
return data
|
|
|
|
|
|
# =========================================================
|
|
# TAMBAH DATA
|
|
# =========================================================
|
|
|
|
@router.post("/", response_model=KomentarResponse)
|
|
def create_data(
|
|
data: KomentarCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
|
|
komentar_baru = Komentar(
|
|
komentar=data.komentar
|
|
)
|
|
|
|
db.add(komentar_baru)
|
|
db.commit()
|
|
db.refresh(komentar_baru)
|
|
|
|
return komentar_baru
|
|
|
|
|
|
# =========================================================
|
|
# HAPUS DATA
|
|
# =========================================================
|
|
|
|
@router.delete("/{id}")
|
|
def delete_data(
|
|
id: int,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
|
|
data = db.query(Komentar).filter(
|
|
Komentar.id == id
|
|
).first()
|
|
|
|
if not data:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="Data komentar tidak ditemukan"
|
|
)
|
|
|
|
db.delete(data)
|
|
db.commit()
|
|
|
|
return {
|
|
"message": "Data berhasil dihapus"
|
|
}
|
|
|
|
|
|
# =========================================================
|
|
# IMPORT CSV
|
|
# =========================================================
|
|
|
|
@router.post("/import-csv")
|
|
async def import_csv(
|
|
file: UploadFile = File(...),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
|
|
# Cek format file
|
|
if not file.filename.endswith(".csv"):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="File harus berformat CSV"
|
|
)
|
|
|
|
# Baca file
|
|
contents = await file.read()
|
|
|
|
try:
|
|
df = pd.read_csv(
|
|
StringIO(
|
|
contents.decode("utf-8")
|
|
)
|
|
)
|
|
|
|
except Exception:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="File CSV tidak dapat dibaca"
|
|
)
|
|
|
|
# Cek kolom komentar
|
|
if "komentar" not in df.columns:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="CSV harus memiliki kolom 'komentar'"
|
|
)
|
|
|
|
jumlah_data = 0
|
|
|
|
# Masukkan data ke database
|
|
for _, row in df.iterrows():
|
|
|
|
komentar = str(
|
|
row["komentar"]
|
|
).strip()
|
|
|
|
if komentar:
|
|
|
|
data_baru = Komentar(
|
|
komentar=komentar
|
|
)
|
|
|
|
db.add(data_baru)
|
|
|
|
jumlah_data += 1
|
|
|
|
db.commit()
|
|
|
|
return {
|
|
"message": "Import CSV berhasil",
|
|
"jumlah_data": jumlah_data
|
|
} |