Files
newsletter-hackernews/app/api/news/route.ts
2024-11-23 09:13:15 +01:00

34 lines
672 B
TypeScript

import prisma from '@prisma/prisma';
import { formatApiResponse } from '@utils/formatApiResponse';
import {
INTERNAL_SERVER_ERROR,
STATUS_INTERNAL_SERVER_ERROR,
STATUS_OK
} from '@utils/statusCodes';
export async function GET() {
try {
const news = await prisma.news.findMany({
orderBy: {
createdAt: 'desc'
},
take: 50,
select: {
id: true,
title: true,
by: true
}
});
if (news) {
return formatApiResponse(STATUS_OK, news);
}
} catch (error) {
console.error(error);
return formatApiResponse(
STATUS_INTERNAL_SERVER_ERROR,
INTERNAL_SERVER_ERROR
);
}
}