More app directory refactoring
This commit is contained in:
parent
b312a8c862
commit
8bbeaa25cf
55 changed files with 903 additions and 899 deletions
|
|
@ -1,32 +0,0 @@
|
|||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { getUserMetadata, saveUserMetadata } from "@/app/_lib/opensearch";
|
||||
|
||||
export const POST = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getToken({
|
||||
req,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return res.redirect("/login");
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return res.status(500).json({ message: "Only POST requests are allowed" });
|
||||
}
|
||||
|
||||
const { email }: any = session;
|
||||
const { name, query } = JSON.parse(req.body);
|
||||
const result = await getUserMetadata(email);
|
||||
const { savedSearches } = result;
|
||||
await saveUserMetadata(email, {
|
||||
savedSearches: [...savedSearches, { name, query }]
|
||||
});
|
||||
const { savedSearches: updatedSavedSearches } = await getUserMetadata(email);
|
||||
return res.json(updatedSavedSearches);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
22
apps/leafcutter/app/api/searches/create/route.ts
Normal file
22
apps/leafcutter/app/api/searches/create/route.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/_lib/auth";
|
||||
import { getUserMetadata, saveUserMetadata } from "@/app/_lib/opensearch";
|
||||
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const session = await getServerSession(authOptions);
|
||||
const { user: { email } }: any = session;
|
||||
const { name, query } = await req.json();
|
||||
const result = await getUserMetadata(email);
|
||||
const { savedSearches } = result;
|
||||
await saveUserMetadata(email, {
|
||||
savedSearches: [...savedSearches, { name, query }]
|
||||
});
|
||||
const { savedSearches: updatedSavedSearches } = await getUserMetadata(email);
|
||||
|
||||
return NextResponse.json(updatedSavedSearches);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { getUserMetadata, saveUserMetadata } from "@/app/_lib/opensearch";
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getToken({
|
||||
req,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return res.redirect("/login");
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return res.status(500).json({ message: "Only POST requests are allowed" });
|
||||
}
|
||||
|
||||
const { email }: any = session;
|
||||
const { name } = JSON.parse(req.body);
|
||||
const { savedSearches } = await getUserMetadata(email);
|
||||
const updatedSavedSearches = savedSearches.filter((search: any) => search.name !== name);
|
||||
const result = await saveUserMetadata(email, { savedSearches: updatedSavedSearches });
|
||||
|
||||
return res.json({ result });
|
||||
};
|
||||
|
||||
export default handler;
|
||||
|
||||
|
||||
|
||||
19
apps/leafcutter/app/api/searches/delete/route.ts
Normal file
19
apps/leafcutter/app/api/searches/delete/route.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/_lib/auth";
|
||||
import { getUserMetadata, saveUserMetadata } from "@/app/_lib/opensearch";
|
||||
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const session = await getServerSession(authOptions);
|
||||
const { user: { email } }: any = session;
|
||||
const { name } = await req.json();
|
||||
const { savedSearches } = await getUserMetadata(email);
|
||||
const updatedSavedSearches = savedSearches.filter((search: any) => search.name !== name);
|
||||
const result = await saveUserMetadata(email, { savedSearches: updatedSavedSearches });
|
||||
|
||||
return NextResponse.json({ result });
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { getUserMetadata } from "@/app/_lib/opensearch";
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getToken({
|
||||
req,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return res.redirect("/login");
|
||||
}
|
||||
|
||||
if (req.method !== "GET") {
|
||||
return res.status(500).json({ message: "Only GET requests are allowed" });
|
||||
}
|
||||
|
||||
const { email }: any = session;
|
||||
const { savedSearches } = await getUserMetadata(email);
|
||||
|
||||
return res.json(savedSearches);
|
||||
};
|
||||
|
||||
export default handler;
|
||||
12
apps/leafcutter/app/api/searches/list/route.ts
Normal file
12
apps/leafcutter/app/api/searches/list/route.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/_lib/auth";
|
||||
import { getUserMetadata } from "@/app/_lib/opensearch";
|
||||
|
||||
export const GET = async () => {
|
||||
const session = await getServerSession(authOptions);
|
||||
const { user: { email } }: any = session;
|
||||
const { savedSearches } = await getUserMetadata(email);
|
||||
|
||||
return NextResponse.json(savedSearches);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue