Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions src/app/admin/components/preview/preview-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import PreviewText from "@app/admin/components/preview/components/preview-text";
import PreviewLink from "@app/admin/components/preview/components/preview-link";
import dynamic from "next/dynamic";
import Preview from "@app/admin/components/preview/components/preview";
const PreviewImage = dynamic(
() => import("@app/admin/components/preview/components/preview-image"),
);
const PreviewVideo = dynamic(
() => import("@app/admin/components/preview/components/preview-video"),
);
const PreviewDivider = dynamic(
() => import("@app/admin/components/preview/components/preview-divider"),
);
const PreviewCalendar = dynamic(
() => import("@app/admin/components/preview/components/preview-calendar"),
);
const PreviewEvent = dynamic(
() => import("@app/admin/components/preview/components/preview-event"),
);

interface Props {
isOpen: boolean;
Expand Down
21 changes: 12 additions & 9 deletions src/app/api/link/add/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ScheduleType {
}

interface NewData {
id: number;
type: number;
sequence: number;
url?: string;
Expand Down Expand Up @@ -43,6 +44,16 @@ export async function POST(request: NextRequest) {
{ status: 401 },
);
}
const client = await clientPromise;
const db = client.db("linkle");
const collection = db.collection<UserDocument>("userdata");
const decoded = jwt.verify(
token,
process.env.JWT_SECRET as string,
) as JwtPayload;
const userId = decoded.userId;
const userDocument = await collection.findOne({ userId });
const newId = userDocument?.data.length ?? 0;

const body = await request.json();
const {
Expand All @@ -59,16 +70,8 @@ export async function POST(request: NextRequest) {
schedule,
} = body;

const client = await clientPromise;
const db = client.db("linkle");
const collection = db.collection<UserDocument>("userdata");
const decoded = jwt.verify(
token,
process.env.JWT_SECRET as string,
) as JwtPayload;
const userId = decoded.userId;

const newData: NewData = {
id: newId,
type,
sequence,
url,
Expand Down
54 changes: 54 additions & 0 deletions src/app/api/link/delete/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";
import jwt from "jsonwebtoken";
import clientPromise from "../../../../lib/mongodb";
import { Collection } from "mongodb";

interface JwtPayload {
userId: string;
}
interface UserDocument {
userId: string;
data: Array<{ id: string }>;
}

export async function POST(request: NextRequest) {
try {
const token = request.cookies.get("token")?.value;
if (!token) {
return NextResponse.json(
{ message: "Cookie not found" },
{ status: 401 },
);
}

const { id } = await request.json();
const client = await clientPromise;
const db = client.db("linkle");
const collection: Collection<UserDocument> = db.collection("userdata");

const decoded = jwt.verify(
token,
process.env.JWT_SECRET as string,
) as JwtPayload;
const userId = decoded.userId;

const result = await collection.updateOne(
{ userId: userId },
{ $pull: { data: { id: id } } },
);

if (result.modifiedCount > 0) {
return NextResponse.json({ message: "Data deleted successfully" });
} else {
return NextResponse.json({ message: "Data not found" }, { status: 404 });
}
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return NextResponse.json(
{ message: "Failed to authenticate", error: errorMessage },
{ status: 500 },
);
}
}
107 changes: 107 additions & 0 deletions src/app/api/link/update/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";
import jwt from "jsonwebtoken";
import clientPromise from "../../../../lib/mongodb";
import { Collection } from "mongodb";

interface JwtPayload {
userId: string;
}
interface UserDocument {
userId: string;
data: Array<{ id: string }>;
}

interface ScheduleType {
id: number;
title: string;
url: string;
dateStart: string;
dateEnd: string;
}

interface NewData {
id: number;
type: number;
sequence: number;
url?: string;
style?: number;
title?: string;
imgUrl?: string;
subText01?: string;
subText02?: string;
dateStart?: string;
dateEnd?: string;
schedule?: ScheduleType[];
}

export async function POST(request: NextRequest) {
try {
const token = request.cookies.get("token")?.value;
if (!token) {
return NextResponse.json(
{ message: "Cookie not found" },
{ status: 401 },
);
}
const body = await request.json();
const {
id,
type,
sequence,
url,
style,
title,
imgUrl,
subText01,
subText02,
dateStart,
dateEnd,
schedule,
} = body;
const client = await clientPromise;
const db = client.db("linkle");
const collection: Collection<UserDocument> = db.collection("userdata");

const decoded = jwt.verify(
token,
process.env.JWT_SECRET as string,
) as JwtPayload;
const userId = decoded.userId;

const newData: NewData = {
id,
type,
sequence,
url,
style,
title,
imgUrl,
subText01,
subText02,
dateStart,
dateEnd,
schedule,
};

const result = await collection.updateOne(
{ userId: userId, "data.id": id },
{
$set: {
"data.$": newData,
},
},
);
return NextResponse.json(
{ message: "Data successfully updated", result },
{ status: 200 },
);
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return NextResponse.json(
{ message: "Failed to authenticate", error: errorMessage },
{ status: 500 },
);
}
}