negyedik gyakorlat + megoldasok

This commit is contained in:
magdo
2026-03-04 20:02:39 +01:00
parent afc3777ac9
commit 388aa908de
217 changed files with 19791 additions and 0 deletions
@@ -0,0 +1,10 @@
class CreateUserCommand {
constructor(email, name, password, role) {
this.email = email;
this.name = name;
this.password = password;
this.role = role;
}
}
module.exports = CreateUserCommand;
@@ -0,0 +1,41 @@
const UserRepository = require('../../../infrastructure/repositories/UserRepository');
class CreateUserHandler {
constructor() {
this.userRepository = new UserRepository();
}
async handle(command) {
// 1. Validáció
if (!command.email || !command.name || !command.password) {
throw new Error('Email, name és password kötelező');
}
// 2. Email formátum ellenőrzés
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(command.email)) {
throw new Error('Hibás email formátum');
}
// 3. Jelszó hossz ellenőrzés
if (command.password.length < 6) {
throw new Error('Jelszó minimum 6 karakter kell legyen');
}
// 4. Duplikáció ellenőrzés
const existing = await this.userRepository.findByEmail(command.email);
if (existing) {
throw new Error('Email már használatban van');
}
// 5. User létrehozása
return await this.userRepository.create({
email: command.email,
name: command.name,
password: command.password, // Production környezetben hash-elni kell!
role: command.role || 'user'
});
}
}
module.exports = CreateUserHandler;
@@ -0,0 +1,7 @@
class DeleteUserCommand {
constructor(id) {
this.id = id;
}
}
module.exports = DeleteUserCommand;
@@ -0,0 +1,22 @@
const UserRepository = require('../../../infrastructure/repositories/UserRepository');
class DeleteUserHandler {
constructor() {
this.userRepository = new UserRepository();
}
async handle(command) {
const user = await this.userRepository.findById(command.id);
if (!user) {
throw new Error('User nem található');
}
await this.userRepository.delete(command.id);
return {
message: 'User sikeresen törölve',
id: command.id
};
}
}
module.exports = DeleteUserHandler;
@@ -0,0 +1,10 @@
class UpdateUserCommand {
constructor(id, email, name, role) {
this.id = id;
this.email = email;
this.name = name;
this.role = role;
}
}
module.exports = UpdateUserCommand;
@@ -0,0 +1,34 @@
const UserRepository = require('../../../infrastructure/repositories/UserRepository');
class UpdateUserHandler {
constructor() {
this.userRepository = new UserRepository();
}
async handle(command) {
// 1. User létezésének ellenőrzése
const user = await this.userRepository.findById(command.id);
if (!user) {
throw new Error('User nem található');
}
// 2. Email egyediség ellenőrzése (ha változott)
if (command.email && command.email !== user.email) {
const existing = await this.userRepository.findByEmail(command.email);
if (existing) {
throw new Error('Email már használatban van');
}
}
// 3. Frissítendő adatok összeállítása
const updateData = {};
if (command.email) updateData.email = command.email;
if (command.name) updateData.name = command.name;
if (command.role) updateData.role = command.role;
// 4. Frissítés végrehajtása
return await this.userRepository.update(command.id, updateData);
}
}
module.exports = UpdateUserHandler;
@@ -0,0 +1,13 @@
const UserRepository = require('../../../infrastructure/repositories/UserRepository');
class GetAllUsersHandler {
constructor() {
this.userRepository = new UserRepository();
}
async handle(query) {
return await this.userRepository.findAll();
}
}
module.exports = GetAllUsersHandler;
@@ -0,0 +1,5 @@
class GetAllUsersQuery {
constructor() {}
}
module.exports = GetAllUsersQuery;
@@ -0,0 +1,17 @@
const UserRepository = require('../../../infrastructure/repositories/UserRepository');
class GetUserHandler {
constructor() {
this.userRepository = new UserRepository();
}
async handle(query) {
const user = await this.userRepository.findById(query.id);
if (!user) {
throw new Error('User nem található');
}
return user;
}
}
module.exports = GetUserHandler;
@@ -0,0 +1,7 @@
class GetUserQuery {
constructor(id) {
this.id = id;
}
}
module.exports = GetUserQuery;