UI & API Menu Inovasi, SubMenu : Kolaborasi Inovasi & Info Teknologi
This commit is contained in:
@@ -2,6 +2,7 @@ import Elysia from "elysia";
|
||||
import DesaDigital from "./desa-digital";
|
||||
import ProgramKreatif from "./program-kreatif";
|
||||
import KolaborasiInovasi from "./kolaborasi-inovasi";
|
||||
import InfoTekno from "./info-teknologi";
|
||||
|
||||
const Inovasi = new Elysia({
|
||||
prefix: "/api/inovasi",
|
||||
@@ -10,5 +11,6 @@ const Inovasi = new Elysia({
|
||||
.use(DesaDigital)
|
||||
.use(ProgramKreatif)
|
||||
.use(KolaborasiInovasi)
|
||||
.use(InfoTekno)
|
||||
|
||||
export default Inovasi;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = Prisma.InfoTeknoGetPayload<{
|
||||
select: {
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
imageId: true;
|
||||
}
|
||||
}>
|
||||
export default async function infoTeknoCreate(context: Context){
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
await prisma.infoTekno.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
imageId: body.imageId,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create info teknologi",
|
||||
data: {
|
||||
...body,
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/app/api/[[...slugs]]/_lib/inovasi/info-teknologi/del.ts
Normal file
54
src/app/api/[[...slugs]]/_lib/inovasi/info-teknologi/del.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
export default async function infoTeknoDelete(context: Context) {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const infoTekno = await prisma.infoTekno.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!infoTekno) {
|
||||
return {
|
||||
status: 404,
|
||||
body: "Info teknologi tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
if (infoTekno.image) {
|
||||
try {
|
||||
const filePath = path.join(
|
||||
infoTekno.image.path,
|
||||
infoTekno.image.name
|
||||
);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: infoTekno.image.id },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Gagal hapus file image:", error);
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.infoTekno.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Info teknologi berhasil dihapus",
|
||||
status: 200,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function infoTeknoFindMany() {
|
||||
try {
|
||||
const data = await prisma.infoTekno.findMany({
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch info teknologi",
|
||||
data,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Find many error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch info teknologi",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function infoTeknoFindUnique(request: Request) {
|
||||
const url = new URL(request.url);
|
||||
const pathSegments = url.pathname.split("/");
|
||||
const id = pathSegments[pathSegments.length - 1];
|
||||
|
||||
if (!id) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "ID tidak ditemukan",
|
||||
}, {status: 400});
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof id !== 'string') {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "ID tidak valid",
|
||||
}, {status: 400});
|
||||
}
|
||||
|
||||
const data = await prisma.infoTekno.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Info teknologi tidak ditemukan",
|
||||
}, {status: 404});
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Success fetch info teknologi by ID",
|
||||
data,
|
||||
}, {status: 200});
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil info teknologi: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}, {status: 500});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import infoTeknoCreate from "./create";
|
||||
import infoTeknoDelete from "./del";
|
||||
import infoTeknoUpdate from "./updt";
|
||||
import infoTeknoFindUnique from "./findUnique";
|
||||
import infoTeknoFindMany from "./findMany";
|
||||
|
||||
const InfoTekno = new Elysia({
|
||||
prefix: "/infotekno",
|
||||
tags: ["Inovasi/Info Tekno"],
|
||||
})
|
||||
.post("/create", infoTeknoCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imageId: t.String(),
|
||||
}),
|
||||
})
|
||||
.get("/find-many", infoTeknoFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await infoTeknoFindUnique(context.request);
|
||||
return response;
|
||||
})
|
||||
.delete("/del/:id", infoTeknoDelete)
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await infoTeknoUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imageId: t.String(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
export default InfoTekno;
|
||||
103
src/app/api/[[...slugs]]/_lib/inovasi/info-teknologi/updt.ts
Normal file
103
src/app/api/[[...slugs]]/_lib/inovasi/info-teknologi/updt.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
type FormUpdate = Prisma.InfoTeknoGetPayload<{
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
deskripsi: true
|
||||
imageId: true
|
||||
}
|
||||
}>
|
||||
export default async function infoTeknoUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const {
|
||||
name,
|
||||
deskripsi,
|
||||
imageId
|
||||
} = body;
|
||||
|
||||
if (!id) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false,
|
||||
message: "ID tidak ditemukan",
|
||||
}), {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const existing = await prisma.infoTekno.findUnique({
|
||||
where: {id},
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
})
|
||||
|
||||
if (!existing) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false,
|
||||
message: "Info tekno tidak ditemukan",
|
||||
}), {
|
||||
status: 404,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (existing.imageId && existing.imageId !== imageId) {
|
||||
const oldImage = existing.image;
|
||||
if (oldImage) {
|
||||
try {
|
||||
const filePath = path.join(oldImage.path, oldImage.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: oldImage.id },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Gagal hapus gambar lama:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.infoTekno.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
deskripsi,
|
||||
imageId,
|
||||
}
|
||||
})
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
success: true,
|
||||
message: "Info teknologi berhasil diupdate",
|
||||
data: updated,
|
||||
}), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error updating info teknologi:", error);
|
||||
return new Response(JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate info teknologi",
|
||||
}), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,9 @@ export default async function kolaborasiInovasiFindUnique(context: Context) {
|
||||
try {
|
||||
const kolaborasiInovasi = await prisma.kolaborasiInovasi.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
});
|
||||
|
||||
if (!kolaborasiInovasi) {
|
||||
|
||||
Reference in New Issue
Block a user