upd: api mobile
Deskripsi: - test api mobile upload gambar banner - elysia No Issues
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -41,4 +41,6 @@ next-env.d.ts
|
|||||||
# folder foto kandidat
|
# folder foto kandidat
|
||||||
/public/image/
|
/public/image/
|
||||||
/public/file/
|
/public/file/
|
||||||
certificates
|
certificates
|
||||||
|
|
||||||
|
test.png
|
||||||
@@ -12,6 +12,8 @@
|
|||||||
"seed": "npx tsx prisma/seed.ts"
|
"seed": "npx tsx prisma/seed.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@elysiajs/cors": "^1.3.1",
|
||||||
|
"@elysiajs/swagger": "^1.3.0",
|
||||||
"@hookstate/core": "^4.0.1",
|
"@hookstate/core": "^4.0.1",
|
||||||
"@hookstate/localstored": "^4.0.2",
|
"@hookstate/localstored": "^4.0.2",
|
||||||
"@mantine/carousel": "^7.11.1",
|
"@mantine/carousel": "^7.11.1",
|
||||||
@@ -42,6 +44,7 @@
|
|||||||
"dayjs": "^1.11.11",
|
"dayjs": "^1.11.11",
|
||||||
"echarts": "^5.5.1",
|
"echarts": "^5.5.1",
|
||||||
"echarts-for-react": "^3.0.2",
|
"echarts-for-react": "^3.0.2",
|
||||||
|
"elysia": "^1.3.1",
|
||||||
"embla-carousel-autoplay": "^7.1.0",
|
"embla-carousel-autoplay": "^7.1.0",
|
||||||
"embla-carousel-react": "^7.1.0",
|
"embla-carousel-react": "^7.1.0",
|
||||||
"iron-session": "^8.0.2",
|
"iron-session": "^8.0.2",
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import { prisma } from "@/module/_global";
|
import { prisma } from "@/module/_global";
|
||||||
import { funGetUserByCookies } from "@/module/auth";
|
import { funGetUserById } from "@/module/auth";
|
||||||
import { createLogUser } from "@/module/user";
|
import { createLogUserMobile } from "@/module/user";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
|
||||||
// HAPUS PROJECT YG TELAH DIBATALKAN
|
// HAPUS PROJECT YG TELAH DIBATALKAN
|
||||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
try {
|
||||||
const user = await funGetUserByCookies()
|
const { user } = await request.json()
|
||||||
if (user.id == undefined) {
|
const userMobile = await funGetUserById({ id: String(user) })
|
||||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
|
||||||
|
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { id } = context.params
|
const { id } = context.params
|
||||||
@@ -24,7 +26,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
{
|
{
|
||||||
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
|
success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan",
|
||||||
},
|
},
|
||||||
{ status: 404 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,8 +40,8 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
})
|
})
|
||||||
|
|
||||||
// create log user
|
// create log user
|
||||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus data kegiatan', table: 'project', data: String(id) })
|
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus data kegiatan', table: 'project', data: String(id), user: userMobile.id })
|
||||||
return NextResponse.json({ success: true, message: "Kegiatan berhasil dihapus", user: user.id }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Kegiatan berhasil dihapus" }, { status: 200 });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
51
src/app/api/v2/[[...slug]]/route.ts
Normal file
51
src/app/api/v2/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import Elysia from "elysia";
|
||||||
|
import { swagger } from "@elysiajs/swagger";
|
||||||
|
import { t } from "elysia";
|
||||||
|
import fs from "fs/promises";
|
||||||
|
import cors from "@elysiajs/cors";
|
||||||
|
const Api = new Elysia({
|
||||||
|
prefix: "/api/v2"
|
||||||
|
})
|
||||||
|
.onError((c) => {
|
||||||
|
console.log(c)
|
||||||
|
return c
|
||||||
|
})
|
||||||
|
// send a file
|
||||||
|
.post("/test", async (c) => {
|
||||||
|
try {
|
||||||
|
// const { file } = c.body
|
||||||
|
|
||||||
|
console.log("terima file")
|
||||||
|
// const buffer = Buffer.from(await file.arrayBuffer());
|
||||||
|
// await fs.writeFile("./test.png", buffer);
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "File uploaded successfully",
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(JSON.stringify(error));
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "File uploaded failed",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
body: t.Object({
|
||||||
|
name: t.String(),
|
||||||
|
file: t.File(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const ApiServer = new Elysia()
|
||||||
|
.use(cors({
|
||||||
|
origin: "*",
|
||||||
|
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||||
|
}))
|
||||||
|
.use(swagger({
|
||||||
|
path: "/api/v2/docs"
|
||||||
|
}))
|
||||||
|
.use(Api)
|
||||||
|
|
||||||
|
export const GET = ApiServer.handle
|
||||||
|
export const POST = ApiServer.handle
|
||||||
18
src/app/api/v2/_test/route.ts
Normal file
18
src/app/api/v2/_test/route.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import fs from "fs/promises";
|
||||||
|
|
||||||
|
export const POST = async (request: Request) => {
|
||||||
|
try {
|
||||||
|
console.log("[POST]")
|
||||||
|
const formData = await request.formData()
|
||||||
|
// const file = formData.get("file") as File;
|
||||||
|
// const buffer = Buffer.from(await file.arrayBuffer());
|
||||||
|
// await fs.writeFile("./test.png", buffer);
|
||||||
|
return NextResponse.json({ success: true, message: "Berhasil mendapatkan banner", data: "file" }, { status: 200 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal menambahkan banner, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user