feat: Tambahkan pagination pada API mobile investasi
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com> Deskripsi: - Tambahkan pagination pada endpoint investor/route.ts - Tambahkan pagination pada endpoint news/route.ts untuk kategori all-news - Gunakan konstanta PAGINATION_DEFAULT_TAKE untuk jumlah data per halaman - Tambahkan metadata pagination (currentPage, totalData, totalPage, dataPerPage) Fixes #issue-number
This commit is contained in:
@@ -1,10 +1,16 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
|
import { PAGINATION_DEFAULT_TAKE } from "@/lib/constans-value/constansValue";
|
||||||
|
|
||||||
export { GET };
|
export { GET };
|
||||||
|
|
||||||
async function GET(request: Request, { params }: { params: { id: string } }) {
|
async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
|
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const page = Number(searchParams.get("page")) || 1;
|
||||||
|
const takeData = PAGINATION_DEFAULT_TAKE;
|
||||||
|
const skipData = page * takeData - takeData;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await prisma.investasi_Invoice.findMany({
|
const data = await prisma.investasi_Invoice.findMany({
|
||||||
@@ -29,13 +35,30 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
take: takeData,
|
||||||
|
skip: skipData,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const totalData = await prisma.investasi_Invoice.count({
|
||||||
|
where: {
|
||||||
|
investasiId: id,
|
||||||
|
statusInvoiceId: "1",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(totalData / takeData);
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
status: 200,
|
status: 200,
|
||||||
success: true,
|
success: true,
|
||||||
message: "Berhasil Mendapatkan Data",
|
message: "Berhasil Mendapatkan Data",
|
||||||
data: data,
|
data: data,
|
||||||
|
meta: {
|
||||||
|
currentPage: page,
|
||||||
|
totalData: totalData,
|
||||||
|
totalPage: totalPages,
|
||||||
|
dataPerPage: takeData,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import _ from "lodash";
|
|||||||
import { prisma } from "@/lib";
|
import { prisma } from "@/lib";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { sendNotificationInvestmentAddNews } from "@/lib/mobile/notification/notification-add-news-investment";
|
import { sendNotificationInvestmentAddNews } from "@/lib/mobile/notification/notification-add-news-investment";
|
||||||
|
import { PAGINATION_DEFAULT_TAKE } from "@/lib/constans-value/constansValue";
|
||||||
|
|
||||||
export { POST, GET, DELETE };
|
export { POST, GET, DELETE };
|
||||||
|
|
||||||
@@ -88,8 +89,13 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
console.log("id", id);
|
console.log("id", id);
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const category = searchParams.get("category");
|
const category = searchParams.get("category");
|
||||||
|
const page = Number(searchParams.get("page")) || 1;
|
||||||
|
const takeData = PAGINATION_DEFAULT_TAKE;
|
||||||
|
const skipData = page * takeData - takeData;
|
||||||
|
|
||||||
let fixData;
|
let fixData;
|
||||||
|
let meta = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (category === "one-news") {
|
if (category === "one-news") {
|
||||||
const data = await prisma.beritaInvestasi.findFirst({
|
const data = await prisma.beritaInvestasi.findFirst({
|
||||||
@@ -113,7 +119,7 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
|
|
||||||
fixData = newData;
|
fixData = newData;
|
||||||
} else if (category === "all-news") {
|
} else if (category === "all-news") {
|
||||||
fixData = await prisma.beritaInvestasi.findMany({
|
const newsData = await prisma.beritaInvestasi.findMany({
|
||||||
orderBy: {
|
orderBy: {
|
||||||
updatedAt: "desc",
|
updatedAt: "desc",
|
||||||
},
|
},
|
||||||
@@ -121,7 +127,27 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
investasiId: id,
|
investasiId: id,
|
||||||
active: true,
|
active: true,
|
||||||
},
|
},
|
||||||
|
take: takeData,
|
||||||
|
skip: skipData,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const totalData = await prisma.beritaInvestasi.count({
|
||||||
|
where: {
|
||||||
|
investasiId: id,
|
||||||
|
active: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(totalData / takeData);
|
||||||
|
|
||||||
|
fixData = newsData;
|
||||||
|
|
||||||
|
meta = {
|
||||||
|
currentPage: page,
|
||||||
|
totalData: totalData,
|
||||||
|
totalPage: totalPages,
|
||||||
|
dataPerPage: takeData,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
@@ -129,6 +155,7 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
success: true,
|
success: true,
|
||||||
message: "Berita berhasil diambil",
|
message: "Berita berhasil diambil",
|
||||||
data: fixData,
|
data: fixData,
|
||||||
|
...(meta && { meta }),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("[ERROR]", error);
|
console.log("[ERROR]", error);
|
||||||
|
|||||||
Reference in New Issue
Block a user