Sinkronisasi UI & API Admin - User Menu Desa, Submenu Gallery
This commit is contained in:
@@ -1,25 +1,57 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// /api/berita/findManyPaginated.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
async function galleryFotoFindMany() {
|
||||
try {
|
||||
const data = await prisma.galleryFoto.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
});
|
||||
async function galleryFotoFindMany(context: Context) {
|
||||
// Ambil parameter dari query
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const search = (context.query.search as string) || '';
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch gallery foto",
|
||||
data,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch gallery foto",
|
||||
};
|
||||
}
|
||||
// Buat where clause
|
||||
const where: any = { isActive: true };
|
||||
|
||||
// Tambahkan pencarian (jika ada)
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ name: { contains: search, mode: 'insensitive' } },
|
||||
{ deskripsi: { contains: search, mode: 'insensitive' } },
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil data dan total count secara paralel
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.galleryFoto.findMany({
|
||||
where,
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.galleryFoto.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil ambil foto dengan pagination",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error di findMany paginated:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data foto",
|
||||
};
|
||||
}
|
||||
}
|
||||
export default galleryFotoFindMany
|
||||
|
||||
export default galleryFotoFindMany;
|
||||
@@ -0,0 +1,18 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function galleryFotoFindRecent() {
|
||||
const result = await prisma.galleryFoto.findMany({
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
take: 3, // ambil 4 data terbaru
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: result,
|
||||
};
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import galleryFotoDelete from "./del";
|
||||
import galleryFotoFindMany from "./find-many";
|
||||
import galleryFotoUpdate from "./updt";
|
||||
import galleryFotoFindUnique from "./findUnique";
|
||||
import galleryFotoFindRecent from "./findRecent";
|
||||
|
||||
const GalleryFoto = new Elysia({ prefix: "/gallery/foto", tags: ["Desa/Gallery/Foto"] })
|
||||
.get("/find-many", galleryFotoFindMany)
|
||||
@@ -18,6 +19,7 @@ const GalleryFoto = new Elysia({ prefix: "/gallery/foto", tags: ["Desa/Gallery/F
|
||||
imagesId: t.String(),
|
||||
}),
|
||||
})
|
||||
.get("/find-recent", galleryFotoFindRecent)
|
||||
.delete("/del/:id", galleryFotoDelete)
|
||||
.put("/:id", async (context) => {
|
||||
const response = await galleryFotoUpdate(context);
|
||||
|
||||
@@ -1,22 +1,53 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// /api/berita/findManyPaginated.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
async function galleryVideoFindMany() {
|
||||
try {
|
||||
const data = await prisma.galleryVideo.findMany({
|
||||
where: { isActive: true },
|
||||
});
|
||||
async function galleryVideoFindMany(context: Context) {
|
||||
// Ambil parameter dari query
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const search = (context.query.search as string) || '';
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch gallery video",
|
||||
data,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch gallery video",
|
||||
};
|
||||
}
|
||||
// Buat where clause
|
||||
const where: any = { isActive: true };
|
||||
|
||||
// Tambahkan pencarian (jika ada)
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ name: { contains: search, mode: 'insensitive' } }
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil data dan total count secara paralel
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.galleryVideo.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.galleryVideo.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil ambil video dengan pagination",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error di findMany paginated:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data video",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default galleryVideoFindMany;
|
||||
@@ -1,12 +1,65 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export const fileStorageFindMany = async (context: Context) => {
|
||||
const category = context.query?.category as string | undefined;
|
||||
|
||||
const data = await prisma.fileStorage.findMany({
|
||||
where: category ? { category } : {},
|
||||
});
|
||||
|
||||
return { data };
|
||||
type WhereClause = {
|
||||
category?: string;
|
||||
isActive?: boolean;
|
||||
OR?: Array<{
|
||||
name?: { contains: string; mode: 'insensitive' };
|
||||
realName?: { contains: string; mode: 'insensitive' };
|
||||
}>;
|
||||
};
|
||||
|
||||
export const fileStorageFindMany = async (context: Context) => {
|
||||
try {
|
||||
// Get query parameters with defaults
|
||||
const page = Math.max(Number(context.query?.page) || 1, 1);
|
||||
const limit = 10; // Fixed at 10 items per page
|
||||
const category = context.query?.category as string | undefined;
|
||||
const search = context.query?.search as string | undefined;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Build where clause with proper TypeScript types
|
||||
const where: WhereClause = { isActive: true };
|
||||
|
||||
if (category) where.category = category;
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ name: { contains: search, mode: 'insensitive' } },
|
||||
{ realName: { contains: search, mode: 'insensitive' } },
|
||||
];
|
||||
}
|
||||
|
||||
// Get paginated data and total count
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.fileStorage.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.fileStorage.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error in fileStorageFindMany:', error);
|
||||
return {
|
||||
data: [],
|
||||
meta: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
totalPages: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user