Initial commit
This commit is contained in:
51
app/api/user/profile/route.ts
Normal file
51
app/api/user/profile/route.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
import { UserService } from "@/lib/services/user.service";
|
||||
import { ApiResponseHandler } from "@/lib/utils/api-response";
|
||||
import { AuthenticationError, ValidationError } from "@/lib/errors/app-error";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.id) {
|
||||
throw new AuthenticationError();
|
||||
}
|
||||
|
||||
const userProfile = await UserService.getUserById(session.user.id);
|
||||
|
||||
if (!userProfile) {
|
||||
throw new AuthenticationError("用户不存在");
|
||||
}
|
||||
|
||||
return ApiResponseHandler.success(userProfile);
|
||||
} catch (error) {
|
||||
return ApiResponseHandler.error(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.id) {
|
||||
throw new AuthenticationError();
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { name } = body;
|
||||
|
||||
if (!name || typeof name !== "string") {
|
||||
throw new ValidationError("名称不能为空");
|
||||
}
|
||||
|
||||
const updatedProfile = await UserService.updateUser(session.user.id, {
|
||||
name,
|
||||
});
|
||||
|
||||
return ApiResponseHandler.success(updatedProfile);
|
||||
} catch (error) {
|
||||
return ApiResponseHandler.error(error as Error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user