Refactor event handling and user management: Replace direct database calls with service layer methods for events, user profiles, and preferences, enhancing code organization and maintainability. Update API routes to utilize new services for event registration, feedback, and user statistics, ensuring a consistent approach across the application.

This commit is contained in:
Julien Froidefond
2025-12-12 16:19:13 +01:00
parent fd095246a3
commit 494ac3f503
34 changed files with 1795 additions and 1096 deletions

View File

@@ -1,73 +1,19 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import bcrypt from "bcryptjs";
import { userService } from "@/services/users/user.service";
import { ValidationError, ConflictError } from "@/services/errors";
export async function POST(request: Request) {
try {
const body = await request.json();
const { email, username, password, bio, characterClass, avatar } = body;
if (!email || !username || !password) {
return NextResponse.json(
{ error: "Email, nom d'utilisateur et mot de passe sont requis" },
{ status: 400 }
);
}
if (password.length < 6) {
return NextResponse.json(
{ error: "Le mot de passe doit contenir au moins 6 caractères" },
{ status: 400 }
);
}
// Valider characterClass si fourni
const validCharacterClasses = [
"WARRIOR",
"MAGE",
"ROGUE",
"RANGER",
"PALADIN",
"ENGINEER",
"MERCHANT",
"SCHOLAR",
"BERSERKER",
"NECROMANCER",
];
if (characterClass && !validCharacterClasses.includes(characterClass)) {
return NextResponse.json(
{ error: "Classe de personnage invalide" },
{ status: 400 }
);
}
// Vérifier si l'email existe déjà
const existingUser = await prisma.user.findFirst({
where: {
OR: [{ email }, { username }],
},
});
if (existingUser) {
return NextResponse.json(
{ error: "Cet email ou nom d'utilisateur est déjà utilisé" },
{ status: 400 }
);
}
// Hasher le mot de passe
const hashedPassword = await bcrypt.hash(password, 10);
// Créer l'utilisateur
const user = await prisma.user.create({
data: {
email,
username,
password: hashedPassword,
bio: bio || null,
characterClass: characterClass || null,
avatar: avatar || null,
},
const user = await userService.validateAndCreateUser({
email,
username,
password,
bio,
characterClass,
avatar,
});
return NextResponse.json(
@@ -76,6 +22,11 @@ export async function POST(request: Request) {
);
} catch (error) {
console.error("Registration error:", error);
if (error instanceof ValidationError || error instanceof ConflictError) {
return NextResponse.json({ error: error.message }, { status: 400 });
}
return NextResponse.json(
{ error: "Une erreur est survenue lors de l'inscription" },
{ status: 500 }