fix: Upload image
Deskripsi: - Upload image job di arahkan ke storage server ## No Issue
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { user_funGetOneUserId } from "@/app_modules/fun_global/get_user_token";
|
||||
import _ from "lodash";
|
||||
import { v4 } from "uuid";
|
||||
import fs from "fs";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { MODEL_JOB } from "../../model/interface";
|
||||
import path from "path";
|
||||
const root = process.cwd();
|
||||
|
||||
export async function Job_funCreate(req: MODEL_JOB, file: FormData) {
|
||||
const authorId = await user_funGetOneUserId();
|
||||
|
||||
const dataImage: any = file.get("file");
|
||||
if (dataImage !== "null") {
|
||||
const fileName = dataImage.name;
|
||||
const fileExtension = _.lowerCase(dataImage.name.split(".").pop());
|
||||
const fRandomName = v4(fileName) + "." + fileExtension;
|
||||
|
||||
const upload = await prisma.images.create({
|
||||
data: {
|
||||
url: fRandomName,
|
||||
label: "JOB",
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
url: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!upload) return { status: 400, message: "Gagal upload gambar" };
|
||||
const uploadFolder = Buffer.from(await dataImage.arrayBuffer());
|
||||
fs.writeFileSync(
|
||||
path.join(root, `public/job/${upload.url}`),
|
||||
uploadFolder
|
||||
);
|
||||
const createDataWithImg = await prisma.job.create({
|
||||
data: {
|
||||
title: req.title,
|
||||
content: req.content,
|
||||
deskripsi: req.deskripsi,
|
||||
authorId: authorId,
|
||||
imagesId: upload.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
authorId: true,
|
||||
MasterStatus: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
title: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createDataWithImg) return { status: 400, message: "Gagal Disimpan" };
|
||||
revalidatePath("/dev/job/main/status");
|
||||
return {
|
||||
data: createDataWithImg,
|
||||
status: 201,
|
||||
message: "Berhasil Disimpan",
|
||||
};
|
||||
} else {
|
||||
const createDataWithoutImg = await prisma.job.create({
|
||||
data: {
|
||||
title: req.title,
|
||||
content: req.content,
|
||||
deskripsi: req.deskripsi,
|
||||
authorId: authorId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
authorId: true,
|
||||
MasterStatus: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
title: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createDataWithoutImg)
|
||||
return { status: 400, message: "Gagal Disimpan" };
|
||||
revalidatePath("/dev/job/main/status");
|
||||
return {
|
||||
data: createDataWithoutImg,
|
||||
status: 201,
|
||||
message: "Berhasil Disimpan",
|
||||
};
|
||||
}
|
||||
}
|
||||
37
src/app_modules/job/fun/create/fun_create_no_file.ts
Normal file
37
src/app_modules/job/fun/create/fun_create_no_file.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@/app/lib";
|
||||
import { user_funGetOneUserId } from "@/app_modules/fun_global";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { MODEL_JOB } from "../../model/interface";
|
||||
|
||||
export async function job_funCreateNoFile({ data }: { data: MODEL_JOB }) {
|
||||
const authorId = await user_funGetOneUserId();
|
||||
|
||||
const createNoImage = await prisma.job.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
content: data.content,
|
||||
deskripsi: data.deskripsi,
|
||||
authorId: authorId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
authorId: true,
|
||||
MasterStatus: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
title: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createNoImage) return { status: 400, message: "Gagal Disimpan" };
|
||||
revalidatePath("/dev/job/main/status");
|
||||
return {
|
||||
status: 201,
|
||||
message: "Berhasil Disimpan",
|
||||
data: createNoImage,
|
||||
};
|
||||
}
|
||||
131
src/app_modules/job/fun/create/fun_create_with_file.ts
Normal file
131
src/app_modules/job/fun/create/fun_create_with_file.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
"use server";
|
||||
|
||||
import prisma from "@/app/lib/prisma";
|
||||
import { user_funGetOneUserId } from "@/app_modules/fun_global/get_user_token";
|
||||
import _ from "lodash";
|
||||
import { v4 } from "uuid";
|
||||
import fs from "fs";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { MODEL_JOB } from "../../model/interface";
|
||||
import path from "path";
|
||||
import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun";
|
||||
import { DIRECTORY_ID } from "@/app/lib";
|
||||
|
||||
export async function job_funCreateWithFile({
|
||||
data,
|
||||
fileId,
|
||||
}: {
|
||||
data: MODEL_JOB;
|
||||
fileId: string;
|
||||
}) {
|
||||
const authorId = await user_funGetOneUserId();
|
||||
console.log(authorId);
|
||||
|
||||
const createDataWithoutImg = await prisma.job.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
content: data.content,
|
||||
deskripsi: data.deskripsi,
|
||||
authorId: authorId,
|
||||
imageId: fileId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
authorId: true,
|
||||
MasterStatus: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
title: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createDataWithoutImg) return { status: 400, message: "Gagal Disimpan" };
|
||||
revalidatePath("/dev/job/main/status");
|
||||
return {
|
||||
data: createDataWithoutImg,
|
||||
status: 201,
|
||||
message: "Berhasil Disimpan",
|
||||
};
|
||||
|
||||
// const dataImage: any = file.get("file");
|
||||
// if (dataImage !== "null") {
|
||||
// const fileName = dataImage.name;
|
||||
// const fileExtension = _.lowerCase(dataImage.name.split(".").pop());
|
||||
// const fRandomName = v4(fileName) + "." + fileExtension;
|
||||
|
||||
// const upload = await prisma.images.create({
|
||||
// data: {
|
||||
// url: fRandomName,
|
||||
// label: "JOB",
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// url: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// if (!upload) return { status: 400, message: "Gagal upload gambar" };
|
||||
// const uploadFolder = Buffer.from(await dataImage.arrayBuffer());
|
||||
// fs.writeFileSync(
|
||||
// path.join(root, `public/job/${upload.url}`),
|
||||
// uploadFolder
|
||||
// );
|
||||
// const createDataWithImg = await prisma.job.create({
|
||||
// data: {
|
||||
// title: req.title,
|
||||
// content: req.content,
|
||||
// deskripsi: req.deskripsi,
|
||||
// authorId: authorId,
|
||||
// imagesId: upload.id,
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// authorId: true,
|
||||
// MasterStatus: {
|
||||
// select: {
|
||||
// name: true,
|
||||
// },
|
||||
// },
|
||||
// title: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// if (!createDataWithImg) return { status: 400, message: "Gagal Disimpan" };
|
||||
// revalidatePath("/dev/job/main/status");
|
||||
// return {
|
||||
// data: createDataWithImg,
|
||||
// status: 201,
|
||||
// message: "Berhasil Disimpan",
|
||||
// };
|
||||
// } else {
|
||||
// const createDataWithoutImg = await prisma.job.create({
|
||||
// data: {
|
||||
// title: req.title,
|
||||
// content: req.content,
|
||||
// deskripsi: req.deskripsi,
|
||||
// authorId: authorId,
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// authorId: true,
|
||||
// MasterStatus: {
|
||||
// select: {
|
||||
// name: true,
|
||||
// },
|
||||
// },
|
||||
// title: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// if (!createDataWithoutImg)
|
||||
// return { status: 400, message: "Gagal Disimpan" };
|
||||
// revalidatePath("/dev/job/main/status");
|
||||
// return {
|
||||
// data: createDataWithoutImg,
|
||||
// status: 201,
|
||||
// message: "Berhasil Disimpan",
|
||||
// };
|
||||
// }
|
||||
}
|
||||
@@ -6,68 +6,51 @@ import { v4 } from "uuid";
|
||||
import fs from "fs";
|
||||
import { MODEL_JOB } from "../../model/interface";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { RouterJob } from "@/app/lib/router_hipmi/router_job";
|
||||
|
||||
export async function Job_EditById(req: MODEL_JOB, file: FormData) {
|
||||
// console.log(file);
|
||||
// console.log(req);
|
||||
// return { status: 200 };
|
||||
|
||||
const dataImage: any = file.get("file");
|
||||
if (dataImage !== "null") {
|
||||
const fileName = dataImage.name;
|
||||
const fileExtension = _.lowerCase(dataImage.name.split(".").pop());
|
||||
const fRandomName = v4(fileName) + "." + fileExtension;
|
||||
|
||||
const upload = await prisma.images.create({
|
||||
data: {
|
||||
url: fRandomName,
|
||||
label: "JOB",
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
url: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!upload) return { status: 400, message: "Gagal upload gambar" };
|
||||
const uploadFolder = Buffer.from(await dataImage.arrayBuffer());
|
||||
fs.writeFileSync(`./public/job/${upload.url}`, uploadFolder);
|
||||
|
||||
export async function job_EditById({
|
||||
data,
|
||||
imageId,
|
||||
}: {
|
||||
data: MODEL_JOB;
|
||||
imageId?: string;
|
||||
}) {
|
||||
if (imageId == undefined) {
|
||||
const updt = await prisma.job.update({
|
||||
where: {
|
||||
id: req.id,
|
||||
id: data.id,
|
||||
},
|
||||
data: {
|
||||
title: req.title,
|
||||
content: req.content,
|
||||
deskripsi: req.deskripsi,
|
||||
imagesId: upload.id,
|
||||
title: data.title,
|
||||
content: data.content,
|
||||
deskripsi: data.deskripsi,
|
||||
},
|
||||
});
|
||||
|
||||
if (!updt) return { status: 400, message: "Gagal Update" };
|
||||
revalidatePath("/dev/job/detail/draft");
|
||||
revalidatePath(RouterJob.status);
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
message: "Berhasil Disimpan",
|
||||
message: "Berhasil Update",
|
||||
};
|
||||
} else {
|
||||
const updt = await prisma.job.update({
|
||||
const updtWithFile = await prisma.job.update({
|
||||
where: {
|
||||
id: req.id,
|
||||
id: data.id,
|
||||
},
|
||||
data: {
|
||||
title: req.title,
|
||||
content: req.content,
|
||||
deskripsi: req.deskripsi,
|
||||
title: data.title,
|
||||
content: data.content,
|
||||
deskripsi: data.deskripsi,
|
||||
imageId: imageId,
|
||||
},
|
||||
});
|
||||
if (!updtWithFile) return { status: 400, message: "Gagal Update" };
|
||||
revalidatePath(RouterJob.status);
|
||||
|
||||
if (!updt) return { status: 400, message: "Gagal Update" };
|
||||
revalidatePath("/dev/job/detail/draft");
|
||||
return {
|
||||
status: 200,
|
||||
message: "Berhasil Disimpan",
|
||||
message: "Berhasil Update",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export async function job_getTwoForHomeView() {
|
||||
},
|
||||
where: {
|
||||
isActive: true,
|
||||
masterStatusId: "1"
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
5
src/app_modules/job/fun/index.ts
Normal file
5
src/app_modules/job/fun/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { job_funCreateNoFile } from "./create/fun_create_no_file";
|
||||
import { job_funCreateWithFile } from "./create/fun_create_with_file";
|
||||
|
||||
export { job_funCreateWithFile };
|
||||
export { job_funCreateNoFile };
|
||||
Reference in New Issue
Block a user