UI & API Menu Inovasi, SubMenu Program Kreatif Desa
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import Elysia from "elysia";
|
||||
import DesaDigital from "./desa-digital";
|
||||
import ProgramKreatif from "./program-kreatif";
|
||||
|
||||
const Inovasi = new Elysia({
|
||||
prefix: "/api/inovasi",
|
||||
tags: ["Inovasi"],
|
||||
})
|
||||
.use(DesaDigital)
|
||||
.use(ProgramKreatif)
|
||||
|
||||
export default Inovasi;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreateProgramKreatif = {
|
||||
name: string;
|
||||
slug: string;
|
||||
deskripsi: string;
|
||||
icon: string;
|
||||
}
|
||||
|
||||
export default async function programKreatifCreate(context: Context){
|
||||
const body = context.body as FormCreateProgramKreatif;
|
||||
|
||||
await prisma.programKreatif.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
slug: body.slug,
|
||||
deskripsi: body.deskripsi,
|
||||
icon: body.icon,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create program kreatif",
|
||||
data: {
|
||||
...body,
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/app/api/[[...slugs]]/_lib/inovasi/program-kreatif/del.ts
Normal file
33
src/app/api/[[...slugs]]/_lib/inovasi/program-kreatif/del.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function programKreatifDelete(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID program kreatif tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = await prisma.programKreatif.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Program kreatif berhasil dihapus",
|
||||
data: deleted,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error delete program kreatif:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat menghapus program kreatif",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
// Di findMany.ts
|
||||
export default async function programKreatifFindMany(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.programKreatif.findMany({
|
||||
where: { isActive: true },
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.programKreatif.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch program kreatif with pagination",
|
||||
data,
|
||||
page,
|
||||
totalPages,
|
||||
total,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many paginated error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch program kreatif 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 programKreatifFindUnique(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID program kreatif diperlukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const programKreatif = await prisma.programKreatif.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!programKreatif) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Program kreatif tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: programKreatif,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error findUnique program kreatif:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data program kreatif",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import programKreatifFindMany from "./findMany";
|
||||
import programKreatifFindUnique from "./findUnique";
|
||||
import programKreatifCreate from "./create";
|
||||
import programKreatifUpdate from "./updt";
|
||||
import programKreatifDelete from "./del";
|
||||
|
||||
const ProgramKreatif = new Elysia({
|
||||
prefix: "/programkreatif",
|
||||
tags: ["Inovasi/Program Kreatif"],
|
||||
})
|
||||
.get("/find-many", programKreatifFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await programKreatifFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
.post("/create", programKreatifCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
slug: t.String(),
|
||||
deskripsi: t.String(),
|
||||
icon: t.String(),
|
||||
}),
|
||||
})
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await programKreatifUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
slug: t.String(),
|
||||
deskripsi: t.String(),
|
||||
icon: t.String(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
.delete("/del/:id", programKreatifDelete);
|
||||
export default ProgramKreatif;
|
||||
@@ -0,0 +1,48 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdateProgramKreatif = {
|
||||
id: string;
|
||||
name?: string;
|
||||
slug?: string;
|
||||
deskripsi?: string;
|
||||
icon?: string;
|
||||
};
|
||||
export default async function programKreatifUpdate(context: Context) {
|
||||
const body = context.body as FormUpdateProgramKreatif;
|
||||
const id = context.params?.id; // ambil dari URL param
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID program kreatif wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await prisma.programKreatif.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
slug: body.slug,
|
||||
deskripsi: body.deskripsi,
|
||||
icon: body.icon,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Program kreatif berhasil diupdate",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error update program kreatif:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengupdate program kreatif",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user