feat: base pages and news fetching with cron job
This commit is contained in:
68
app/api/cron/route.ts
Normal file
68
app/api/cron/route.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import prisma from '../../../prisma/prisma';
|
||||
import { NewsSchema } from '../../utils/types';
|
||||
import { singleNews, topNews } from '../../utils/urls';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (
|
||||
request.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`
|
||||
) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const response = await hackernewsApi();
|
||||
|
||||
return new NextResponse(JSON.stringify(response), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
|
||||
async function hackernewsApi() {
|
||||
const topstories: number[] = await fetch(topNews).then((res) => res.json());
|
||||
|
||||
console.log('topstories', topstories);
|
||||
|
||||
const newsPromises = topstories
|
||||
.splice(0, Number(process.env.NEWS_LIMIT))
|
||||
.map(async (id) => {
|
||||
console.log('id', id);
|
||||
const sourceNews: z.infer<typeof NewsSchema> = await fetch(
|
||||
singleNews(id)
|
||||
).then((res) => res.json());
|
||||
|
||||
console.log('sourceNews', sourceNews);
|
||||
|
||||
return await prisma.news.upsert({
|
||||
create: {
|
||||
id,
|
||||
title: sourceNews.title,
|
||||
text: sourceNews.text,
|
||||
type: sourceNews.type,
|
||||
by: sourceNews.by,
|
||||
time: sourceNews.time,
|
||||
url: sourceNews.url,
|
||||
score: sourceNews.score,
|
||||
},
|
||||
update: {
|
||||
title: sourceNews.title,
|
||||
text: sourceNews.text,
|
||||
type: sourceNews.type,
|
||||
by: sourceNews.by,
|
||||
time: sourceNews.time,
|
||||
url: sourceNews.url,
|
||||
score: sourceNews.score,
|
||||
},
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const newsIds = await Promise.all(newsPromises);
|
||||
|
||||
return newsIds.map((news) => news.id);
|
||||
}
|
||||
32
app/api/subscribe/route.ts
Normal file
32
app/api/subscribe/route.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from '../../../prisma/prisma';
|
||||
import { ResponseSchema, SubscribeFormSchema } from '../../utils/types';
|
||||
|
||||
export const dynamic = 'force-dynamic'; // defaults to force-static
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const validation = SubscribeFormSchema.safeParse(body);
|
||||
if (!validation.success) {
|
||||
return new Response('Bad request', { status: 400 });
|
||||
}
|
||||
|
||||
const { email, targetingAllowed } = validation.data;
|
||||
|
||||
await prisma.user.upsert({
|
||||
create: {
|
||||
email,
|
||||
targetingAllowed,
|
||||
},
|
||||
update: {
|
||||
targetingAllowed,
|
||||
},
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
|
||||
const message: z.infer<typeof ResponseSchema> = {
|
||||
message: `${email} subscribed!`,
|
||||
};
|
||||
return new Response(JSON.stringify(message), { status: 200 });
|
||||
}
|
||||
29
app/api/unsubscribe/route.ts
Normal file
29
app/api/unsubscribe/route.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from 'zod';
|
||||
import prisma from '../../../prisma/prisma';
|
||||
import { ResponseSchema, UnsubscribeFormSchema } from '../../utils/types';
|
||||
|
||||
export const dynamic = 'force-dynamic'; // defaults to force-static
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const validation = UnsubscribeFormSchema.safeParse(body);
|
||||
if (!validation.success) {
|
||||
return new Response('Bad request', { status: 400 });
|
||||
}
|
||||
|
||||
const { email } = validation.data;
|
||||
|
||||
try {
|
||||
await prisma.user.delete({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
const message: z.infer<typeof ResponseSchema> = {
|
||||
message: `${email} unsubscribe!`,
|
||||
};
|
||||
return new Response(JSON.stringify(message), { status: 200 });
|
||||
}
|
||||
Reference in New Issue
Block a user