Jumat, 30 May 2025 :
Yang Sudah Di Kerjakan * Tampilan UI Admin di menu inovasi * API Create, edit dan delete potensi Yang Lagi Dikerjakan: * Progress Tampilan UI Admin Di Menu Lingkungan Yang Akan Dikerjakan: * API Menu Lain * Tampilan UI Admin Di Menu Pendidikan
This commit is contained in:
@@ -15,76 +15,6 @@ type FormUpdate = Prisma.BeritaGetPayload<{
|
||||
};
|
||||
}>;
|
||||
|
||||
// async function beritaUpdate(context: Context) {
|
||||
// const body = (await context.body) as FormUpdate;
|
||||
|
||||
// const {
|
||||
// id,
|
||||
// judul,
|
||||
// deskripsi,
|
||||
// content,
|
||||
// kategoriBeritaId,
|
||||
// imageId,
|
||||
// } = body;
|
||||
|
||||
// if (!id) {
|
||||
// return {
|
||||
// status: 400,
|
||||
// body: "ID tidak boleh kosong",
|
||||
// };
|
||||
// }
|
||||
|
||||
// const existing = await prisma.berita.findUnique({
|
||||
// where: { id },
|
||||
// include: {
|
||||
// image: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// if (!existing) {
|
||||
// return {
|
||||
// status: 404,
|
||||
// body: "Berita tidak ditemukan",
|
||||
// };
|
||||
// }
|
||||
|
||||
// // Cek jika imageId diubah
|
||||
// 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); // hapus file dari filesystem
|
||||
// await prisma.fileStorage.delete({
|
||||
// where: { id: oldImage.id }, // hapus record dari DB
|
||||
// });
|
||||
// } catch (err) {
|
||||
// console.error("Gagal hapus gambar lama:", err);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// const updated = await prisma.berita.update({
|
||||
// where: { id },
|
||||
// data: {
|
||||
// judul,
|
||||
// deskripsi,
|
||||
// content,
|
||||
// kategoriBeritaId,
|
||||
// imageId,
|
||||
// },
|
||||
// });
|
||||
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "Berita berhasil diupdate (gambar lama juga dihapus jika ada)",
|
||||
// data: updated,
|
||||
// };
|
||||
// }
|
||||
|
||||
// export default beritaUpdate;
|
||||
|
||||
|
||||
async function beritaUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string; // ambil dari URL
|
||||
|
||||
@@ -2,11 +2,12 @@ import Elysia from "elysia";
|
||||
import Berita from "./berita";
|
||||
import Pengumuman from "./pengumuman";
|
||||
import ProfileDesa from "./profile/profile_desa";
|
||||
|
||||
import PotensiDesa from "./potensi";
|
||||
|
||||
const Desa = new Elysia({ prefix: "/api/desa", tags: ["Desa"] })
|
||||
.use(Berita)
|
||||
.use(Pengumuman)
|
||||
.use(ProfileDesa)
|
||||
.use(PotensiDesa)
|
||||
|
||||
export default Desa;
|
||||
|
||||
33
src/app/api/[[...slugs]]/_lib/desa/potensi/create.ts
Normal file
33
src/app/api/[[...slugs]]/_lib/desa/potensi/create.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = Prisma.PotensiDesaGetPayload<{
|
||||
select: {
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
kategori: true;
|
||||
imageId: true;
|
||||
content: true;
|
||||
}
|
||||
}>
|
||||
export default async function potensiDesaCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
await prisma.potensiDesa.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
kategori: body.kategori,
|
||||
imageId: body.imageId,
|
||||
content: body.content,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create potensi desa",
|
||||
data: {
|
||||
...body,
|
||||
},
|
||||
};
|
||||
}
|
||||
54
src/app/api/[[...slugs]]/_lib/desa/potensi/del.ts
Normal file
54
src/app/api/[[...slugs]]/_lib/desa/potensi/del.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import path from "path";
|
||||
import { Context } from "vm";
|
||||
import fs from "fs/promises";
|
||||
|
||||
const potensiDesaDelete = async (context: Context) => {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const potensiDesa = await prisma.potensiDesa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!potensiDesa) {
|
||||
return {
|
||||
status: 404,
|
||||
body: "Potensi Desa tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
// Hapus file gambar dari filesystem jika ada
|
||||
if (potensiDesa.image) {
|
||||
try {
|
||||
const filePath = path.join(potensiDesa.image.path, potensiDesa.image.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: potensiDesa.image.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus file image:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// Hapus potensi desa dari DB
|
||||
await prisma.potensiDesa.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Potensi Desa dan file terkait berhasil dihapus",
|
||||
};
|
||||
};
|
||||
|
||||
export default potensiDesaDelete;
|
||||
26
src/app/api/[[...slugs]]/_lib/desa/potensi/find-many.ts
Normal file
26
src/app/api/[[...slugs]]/_lib/desa/potensi/find-many.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
async function potensiDesaFindMany() {
|
||||
try {
|
||||
const data = await prisma.potensiDesa.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch potensi desa",
|
||||
data,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch potensi desa",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default potensiDesaFindMany
|
||||
51
src/app/api/[[...slugs]]/_lib/desa/potensi/find-unique.ts
Normal file
51
src/app/api/[[...slugs]]/_lib/desa/potensi/find-unique.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function findUnique(
|
||||
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 boleh kosong",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
if( typeof id !== 'string') {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "ID harus berupa string",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await prisma.potensiDesa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if(!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Potensi Desa tidak ditemukan",
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Success fetch potensi desa by ID",
|
||||
data,
|
||||
}, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil potensi desa: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
}
|
||||
45
src/app/api/[[...slugs]]/_lib/desa/potensi/index.ts
Normal file
45
src/app/api/[[...slugs]]/_lib/desa/potensi/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import Elysia from "elysia";
|
||||
import createPotensiDesa from "./create";
|
||||
import { t } from "elysia";
|
||||
import potensiDesaDelete from "./del";
|
||||
import potensiDesaFindMany from "./find-many";
|
||||
import potensiDesaUpdate from "./updt";
|
||||
import findUnique from "./find-unique";
|
||||
|
||||
|
||||
const PotensiDesa = new Elysia({
|
||||
prefix: "/potensi", tags: ["Desa/Potensi"]
|
||||
})
|
||||
.post("/create", createPotensiDesa, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
kategori: t.String(),
|
||||
imageId: t.String(),
|
||||
content: t.String(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", potensiDesaDelete)
|
||||
.get("/find-many", potensiDesaFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await findUnique(new Request(context.request));
|
||||
return response;
|
||||
})
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await potensiDesaUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
kategori: t.String(),
|
||||
imageId: t.String(),
|
||||
content: t.String(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
|
||||
export default PotensiDesa
|
||||
98
src/app/api/[[...slugs]]/_lib/desa/potensi/updt.ts
Normal file
98
src/app/api/[[...slugs]]/_lib/desa/potensi/updt.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
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.PotensiDesaGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
kategori: true;
|
||||
imageId: true;
|
||||
content: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
export default async function potensiDesaUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const {
|
||||
name,
|
||||
deskripsi,
|
||||
kategori,
|
||||
imageId,
|
||||
content,
|
||||
} = 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.potensiDesa.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
image: true,
|
||||
}
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, message: "Potensi Desa 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.potensiDesa.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
deskripsi,
|
||||
kategori,
|
||||
imageId,
|
||||
content,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Potensi Desa berhasil diupdate",
|
||||
data: updated,
|
||||
}),
|
||||
{ status: 200, headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error updating potensi desa:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate potensi desa",
|
||||
}),
|
||||
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user