UI Desa Gallery Done
API Desa Gallery Done
This commit is contained in:
32
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/create.ts
Normal file
32
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/create.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = Prisma.GalleryFotoGetPayload<{
|
||||
select: {
|
||||
name: true;
|
||||
imagesId: true;
|
||||
deskripsi: true;
|
||||
|
||||
};
|
||||
}>;
|
||||
async function galleryFotoCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
await prisma.galleryFoto.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
imagesId: body.imagesId,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create gallery foto",
|
||||
data: {
|
||||
...body,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default galleryFotoCreate
|
||||
53
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/del.ts
Normal file
53
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/del.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
const galleryFotoDelete = async (context: Context) => {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const foto = await prisma.galleryFoto.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!foto) {
|
||||
return {
|
||||
status: 404,
|
||||
body: "Foto tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
// Hapus file gambar dari filesystem jika ada
|
||||
if (foto.imageGalleryFoto) {
|
||||
try {
|
||||
const filePath = path.join(foto.imageGalleryFoto.path, foto.imageGalleryFoto.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: foto.imageGalleryFoto.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.galleryFoto.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: "Foto berhasil dihapus",
|
||||
};
|
||||
}
|
||||
|
||||
export default galleryFotoDelete
|
||||
25
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/find-many.ts
Normal file
25
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/find-many.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
async function galleryFotoFindMany() {
|
||||
try {
|
||||
const data = await prisma.galleryFoto.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
});
|
||||
|
||||
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",
|
||||
};
|
||||
}
|
||||
}
|
||||
export default galleryFotoFindMany
|
||||
@@ -0,0 +1,49 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function galleryFotoFindUnique(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 tidak valid",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await prisma.galleryFoto.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gallery foto tidak ditemukan",
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Success fetch gallery foto by ID",
|
||||
data,
|
||||
}, { status: 200 });
|
||||
} catch (e) {
|
||||
console.error("Find by ID error:", e);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil gallery foto: " + (e instanceof Error ? e.message : 'Unknown error'),
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
34
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/index.ts
Normal file
34
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/index.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import galleryFotoCreate from "./create";
|
||||
import galleryFotoDelete from "./del";
|
||||
import galleryFotoFindMany from "./find-many";
|
||||
import galleryFotoUpdate from "./updt";
|
||||
import galleryFotoFindUnique from "./findUnique";
|
||||
|
||||
const GalleryFoto = new Elysia({ prefix: "/gallery/foto", tags: ["Desa/Gallery/Foto"] })
|
||||
.get("/find-many", galleryFotoFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await galleryFotoFindUnique(new Request(context.request));
|
||||
return response;
|
||||
})
|
||||
.post("/create", galleryFotoCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imagesId: t.String(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", galleryFotoDelete)
|
||||
.put("/:id", async (context) => {
|
||||
const response = await galleryFotoUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
imagesId: t.String(),
|
||||
}),
|
||||
})
|
||||
|
||||
export default GalleryFoto
|
||||
90
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/updt.ts
Normal file
90
src/app/api/[[...slugs]]/_lib/desa/gallery/foto/updt.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
type FormUpdate = Prisma.GalleryFotoGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
imagesId: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
async function galleryFotoUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const { name, deskripsi, imagesId } = body;
|
||||
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, message: "ID tidak diberikan" }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
const existing = await prisma.galleryFoto.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
imageGalleryFoto: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Gallery foto tidak ditemukan",
|
||||
}),
|
||||
{ status: 404, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
if (existing.imagesId && existing.imagesId !== imagesId) {
|
||||
const oldImage = existing.imageGalleryFoto;
|
||||
if (oldImage) {
|
||||
try {
|
||||
const filePath = path.join(oldImage.path, oldImage.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: oldImage.id },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Gagal hapus gambar lama:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.galleryFoto.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name,
|
||||
deskripsi,
|
||||
imagesId,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Success update gallery foto",
|
||||
data: updated,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error updating gallery foto:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate gallery foto",
|
||||
}),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
}
|
||||
export default galleryFotoUpdate;
|
||||
30
src/app/api/[[...slugs]]/_lib/desa/gallery/video/create.ts
Normal file
30
src/app/api/[[...slugs]]/_lib/desa/gallery/video/create.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = Prisma.GalleryVideoGetPayload<{
|
||||
select: {
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
linkVideo: true;
|
||||
}
|
||||
}>
|
||||
async function galleryVideoCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
await prisma.galleryVideo.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
deskripsi: body.deskripsi,
|
||||
linkVideo: body.linkVideo,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create gallery video",
|
||||
data: {
|
||||
...body,
|
||||
},
|
||||
};
|
||||
}
|
||||
export default galleryVideoCreate;
|
||||
34
src/app/api/[[...slugs]]/_lib/desa/gallery/video/del.ts
Normal file
34
src/app/api/[[...slugs]]/_lib/desa/gallery/video/del.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
const galleryVideoDelete = async (context: Context) => {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
status: 400,
|
||||
body: "ID tidak diberikan",
|
||||
};
|
||||
}
|
||||
|
||||
const galleryVideo = await prisma.galleryVideo.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!galleryVideo) {
|
||||
return {
|
||||
status: 404,
|
||||
body: "Gallery video tidak ditemukan",
|
||||
};
|
||||
}
|
||||
// Hapus gallery video dari database
|
||||
await prisma.galleryVideo.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: "Gallery video berhasil dihapus",
|
||||
};
|
||||
}
|
||||
export default galleryVideoDelete;
|
||||
@@ -0,0 +1,22 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
async function galleryVideoFindMany() {
|
||||
try {
|
||||
const data = await prisma.galleryVideo.findMany({
|
||||
where: { isActive: true },
|
||||
});
|
||||
|
||||
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",
|
||||
};
|
||||
}
|
||||
}
|
||||
export default galleryVideoFindMany;
|
||||
@@ -0,0 +1,46 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function galleryVideoFindUnique(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 tidak valid",
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await prisma.galleryVideo.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gallery video tidak ditemukan",
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
success: true,
|
||||
message: "Success fetch gallery video by ID",
|
||||
data,
|
||||
}, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return Response.json({
|
||||
success: false,
|
||||
message: "Gagal mengambil gallery video: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
39
src/app/api/[[...slugs]]/_lib/desa/gallery/video/index.ts
Normal file
39
src/app/api/[[...slugs]]/_lib/desa/gallery/video/index.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import galleryVideoFindMany from "./find-many";
|
||||
import galleryVideoFindUnique from "./findUnique";
|
||||
import galleryVideoCreate from "./create";
|
||||
import galleryVideoDelete from "./del";
|
||||
import galleryVideoUpdate from "./updt";
|
||||
|
||||
const GalleryVideo = new Elysia({
|
||||
prefix: "/gallery/video",
|
||||
tags: ["Desa/Gallery/Video"],
|
||||
})
|
||||
.get("/find-many", galleryVideoFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await galleryVideoFindUnique(new Request(context.request));
|
||||
return response;
|
||||
})
|
||||
.post("/create", galleryVideoCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
linkVideo: t.String(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", galleryVideoDelete)
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await galleryVideoUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
deskripsi: t.String(),
|
||||
linkVideo: t.String(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
export default GalleryVideo;
|
||||
85
src/app/api/[[...slugs]]/_lib/desa/gallery/video/updt.ts
Normal file
85
src/app/api/[[...slugs]]/_lib/desa/gallery/video/updt.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdate = Prisma.GalleryVideoGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
name: true;
|
||||
deskripsi: true;
|
||||
linkVideo: true;
|
||||
}
|
||||
}>;
|
||||
|
||||
async function galleryVideoUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
const {
|
||||
name,
|
||||
deskripsi,
|
||||
linkVideo,
|
||||
} = body;
|
||||
|
||||
if (!id) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false,
|
||||
message: "ID tidak boleh kosong",
|
||||
}), {
|
||||
status: 400,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const existing = await prisma.galleryVideo.findUnique({
|
||||
where: {id}
|
||||
})
|
||||
|
||||
if (!existing) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false,
|
||||
message: "Gallery video tidak ditemukan",
|
||||
}), {
|
||||
status: 404,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const updated = await prisma.galleryVideo.update({
|
||||
where: {id},
|
||||
data: {
|
||||
name,
|
||||
deskripsi,
|
||||
linkVideo,
|
||||
}
|
||||
})
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
success: true,
|
||||
message: "Gallery video berhasil diupdate",
|
||||
data: updated,
|
||||
}), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Update error:", error);
|
||||
return new Response(JSON.stringify({
|
||||
success: false,
|
||||
message: "Gagal mengupdate gallery video: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}), {
|
||||
status: 500,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
export default galleryVideoUpdate;
|
||||
@@ -3,11 +3,15 @@ import Berita from "./berita";
|
||||
import Pengumuman from "./pengumuman";
|
||||
import ProfileDesa from "./profile/profile_desa";
|
||||
import PotensiDesa from "./potensi";
|
||||
import GalleryFoto from "./gallery/foto";
|
||||
import GalleryVideo from "./gallery/video";
|
||||
|
||||
const Desa = new Elysia({ prefix: "/api/desa", tags: ["Desa"] })
|
||||
.use(Berita)
|
||||
.use(Pengumuman)
|
||||
.use(ProfileDesa)
|
||||
.use(PotensiDesa)
|
||||
.use(GalleryFoto)
|
||||
.use(GalleryVideo)
|
||||
|
||||
export default Desa;
|
||||
|
||||
Reference in New Issue
Block a user