Add bio field to user model and update related components: Enhance leaderboard and profile features by including a bio field in user data. Update API routes, UI components, and validation logic to support bio input and display, improving user profiles and leaderboard entries.
This commit is contained in:
@@ -14,16 +14,30 @@ export async function GET() {
|
||||
score: true,
|
||||
level: true,
|
||||
avatar: true,
|
||||
bio: true,
|
||||
},
|
||||
});
|
||||
|
||||
const leaderboard = users.map((user: { id: string; username: string; score: number; level: number; avatar: string | null }, index: number) => ({
|
||||
rank: index + 1,
|
||||
username: user.username,
|
||||
score: user.score,
|
||||
level: user.level,
|
||||
avatar: user.avatar,
|
||||
}));
|
||||
const leaderboard = users.map(
|
||||
(
|
||||
user: {
|
||||
id: string;
|
||||
username: string;
|
||||
score: number;
|
||||
level: number;
|
||||
avatar: string | null;
|
||||
bio: string | null;
|
||||
},
|
||||
index: number
|
||||
) => ({
|
||||
rank: index + 1,
|
||||
username: user.username,
|
||||
score: user.score,
|
||||
level: user.level,
|
||||
avatar: user.avatar,
|
||||
bio: user.bio,
|
||||
})
|
||||
);
|
||||
|
||||
return NextResponse.json(leaderboard);
|
||||
} catch (error) {
|
||||
@@ -34,4 +48,3 @@ export async function GET() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ export async function GET() {
|
||||
email: true,
|
||||
username: true,
|
||||
avatar: true,
|
||||
bio: true,
|
||||
hp: true,
|
||||
maxHp: true,
|
||||
xp: true,
|
||||
@@ -50,7 +51,7 @@ export async function PUT(request: Request) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { username, avatar } = body;
|
||||
const { username, avatar, bio } = body;
|
||||
|
||||
// Validation
|
||||
if (username !== undefined) {
|
||||
@@ -84,14 +85,33 @@ export async function PUT(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Validation bio
|
||||
if (bio !== undefined) {
|
||||
if (typeof bio !== "string") {
|
||||
return NextResponse.json(
|
||||
{ error: "La bio doit être une chaîne de caractères" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (bio.length > 500) {
|
||||
return NextResponse.json(
|
||||
{ error: "La bio ne peut pas dépasser 500 caractères" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour l'utilisateur
|
||||
const updateData: { username?: string; avatar?: string | null } = {};
|
||||
const updateData: { username?: string; avatar?: string | null; bio?: string | null } = {};
|
||||
if (username !== undefined) {
|
||||
updateData.username = username.trim();
|
||||
}
|
||||
if (avatar !== undefined) {
|
||||
updateData.avatar = avatar || null;
|
||||
}
|
||||
if (bio !== undefined) {
|
||||
updateData.bio = bio.trim() || null;
|
||||
}
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: { id: session.user.id },
|
||||
@@ -101,6 +121,7 @@ export async function PUT(request: Request) {
|
||||
email: true,
|
||||
username: true,
|
||||
avatar: true,
|
||||
bio: true,
|
||||
hp: true,
|
||||
maxHp: true,
|
||||
xp: true,
|
||||
|
||||
Reference in New Issue
Block a user