UI & API Menu Inovasi, SubMenu Program Kreatif Desa
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Elysia from "elysia";
|
||||
import DesaDigital from "./desa-digital";
|
||||
import ProgramKreatif from "./program-kreatif";
|
||||
import KolaborasiInovasi from "./kolaborasi-inovasi";
|
||||
|
||||
const Inovasi = new Elysia({
|
||||
prefix: "/api/inovasi",
|
||||
@@ -8,5 +9,6 @@ const Inovasi = new Elysia({
|
||||
})
|
||||
.use(DesaDigital)
|
||||
.use(ProgramKreatif)
|
||||
.use(KolaborasiInovasi)
|
||||
|
||||
export default Inovasi;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreateKolaborasiInovasi = {
|
||||
name: string;
|
||||
tahun: number;
|
||||
slug: string;
|
||||
deskripsi: string;
|
||||
kolaborator: string;
|
||||
imageId: string;
|
||||
}
|
||||
|
||||
export default async function kolaborasiInovasiCreate(context: Context){
|
||||
const body = context.body as FormCreateKolaborasiInovasi;
|
||||
|
||||
await prisma.kolaborasiInovasi.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
tahun: body.tahun,
|
||||
slug: body.slug,
|
||||
deskripsi: body.deskripsi,
|
||||
kolaborator: body.kolaborator,
|
||||
imageId: body.imageId,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create kolaborasi inovasi",
|
||||
data: {
|
||||
...body,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function kolaborasiInovasiDelete(context: Context){
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID kolaborasi inovasi diperlukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = await prisma.kolaborasiInovasi.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Kolaborasi inovasi berhasil dihapus",
|
||||
data: deleted,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error delete kolaborasi inovasi:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat menghapus kolaborasi inovasi",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
// Di findMany.ts
|
||||
export default async function kolaborasiInovasiFindMany(context: Context) {
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
try {
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.kolaborasiInovasi.findMany({
|
||||
where: { isActive: true },
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.kolaborasiInovasi.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch kolaborasi inovasi with pagination",
|
||||
data,
|
||||
page,
|
||||
totalPages,
|
||||
total,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many paginated error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch kolaborasi inovasi with pagination",
|
||||
data: [],
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function kolaborasiInovasiFindUnique(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID kolaborasi inovasi diperlukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const kolaborasiInovasi = await prisma.kolaborasiInovasi.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!kolaborasiInovasi) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Kolaborasi inovasi tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: kolaborasiInovasi,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error findUnique kolaborasi inovasi:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data kolaborasi inovasi",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import kolaborasiInovasiFindMany from "./findMany";
|
||||
import kolaborasiInovasiFindUnique from "./findUnique";
|
||||
import kolaborasiInovasiCreate from "./create";
|
||||
import kolaborasiInovasiUpdate from "./updt";
|
||||
import kolaborasiInovasiDelete from "./del";
|
||||
|
||||
const KolaborasiInovasi = new Elysia({
|
||||
prefix: "/kolaborasiinovasi",
|
||||
tags: ["Inovasi/Kolaborasi Inovasi"],
|
||||
})
|
||||
.get("/find-many", kolaborasiInovasiFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await kolaborasiInovasiFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
.post("/create", kolaborasiInovasiCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
tahun: t.Number(),
|
||||
slug: t.String(),
|
||||
deskripsi: t.String(),
|
||||
kolaborator: t.String(),
|
||||
imageId: t.String(),
|
||||
}),
|
||||
})
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await kolaborasiInovasiUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
tahun: t.Number(),
|
||||
slug: t.String(),
|
||||
deskripsi: t.String(),
|
||||
kolaborator: t.String(),
|
||||
imageId: t.String(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
.delete("/del/:id", kolaborasiInovasiDelete);
|
||||
export default KolaborasiInovasi;
|
||||
@@ -0,0 +1,51 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdateKolaborasiInovasi = {
|
||||
id: string;
|
||||
name?: string;
|
||||
tahun?: number;
|
||||
slug?: string;
|
||||
deskripsi?: string;
|
||||
kolaborator?: string;
|
||||
imageId?: string;
|
||||
};
|
||||
export default async function kolaborasiInovasiUpdate(context: Context) {
|
||||
const body = context.body as FormUpdateKolaborasiInovasi;
|
||||
const id = context.params?.id; // ambil dari URL param
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID kolaborasi inovasi wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await prisma.kolaborasiInovasi.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
tahun: body.tahun,
|
||||
slug: body.slug,
|
||||
deskripsi: body.deskripsi,
|
||||
kolaborator: body.kolaborator,
|
||||
imageId: body.imageId,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Kolaborasi inovasi berhasil diupdate",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error update kolaborasi inovasi:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengupdate kolaborasi inovasi",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user