backend feladat

This commit is contained in:
magdo
2026-02-13 19:14:10 +01:00
parent 6afdef6a30
commit 622d4a3321
877 changed files with 87856 additions and 0 deletions
@@ -0,0 +1,7 @@
export class CreatePostCommand {
constructor(title, content, author) {
this.title = title;
this.content = content;
this.author = author;
}
}
@@ -0,0 +1,19 @@
export class CreatePostCommandHandler {
constructor(postRepository) {
this.postRepository = postRepository;
}
async handle(command) {
if (!command.title || !command.content) {
throw new Error('Title and content are required');
}
const postData = {
title: command.title,
content: command.content,
author: command.author
};
return await this.postRepository.create(postData);
}
}
@@ -0,0 +1,5 @@
export class DeletePostCommand {
constructor(id) {
this.id = id;
}
}
@@ -0,0 +1,13 @@
export class DeletePostCommandHandler {
constructor(postRepository) {
this.postRepository = postRepository;
}
async handle(command) {
const result = await this.postRepository.delete(command.id);
if (!result) {
throw new Error('Post not found');
}
return result;
}
}
@@ -0,0 +1,8 @@
export class UpdatePostCommand {
constructor(id, title, content, author) {
this.id = id;
this.title = title;
this.content = content;
this.author = author;
}
}
@@ -0,0 +1,18 @@
export class UpdatePostCommandHandler {
constructor(postRepository) {
this.postRepository = postRepository;
}
async handle(command) {
const postData = {};
if (command.title) postData.title = command.title;
if (command.content) postData.content = command.content;
if (command.author) postData.author = command.author;
const result = await this.postRepository.update(command.id, postData);
if (!result) {
throw new Error('Post not found');
}
return result;
}
}
@@ -0,0 +1,3 @@
export class GetAllPostsQuery {
constructor() {}
}
@@ -0,0 +1,9 @@
export class GetAllPostsQueryHandler {
constructor(postRepository) {
this.postRepository = postRepository;
}
async handle(query) {
return await this.postRepository.getAll();
}
}
@@ -0,0 +1,5 @@
export class GetPostByIdQuery {
constructor(id) {
this.id = id;
}
}
@@ -0,0 +1,13 @@
export class GetPostByIdQueryHandler {
constructor(postRepository) {
this.postRepository = postRepository;
}
async handle(query) {
const post = await this.postRepository.getById(query.id);
if (!post) {
throw new Error('Post not found');
}
return post;
}
}
@@ -0,0 +1,5 @@
export class GetPostsByUserIdQuery {
constructor(userId) {
this.userId = userId;
}
}
@@ -0,0 +1,9 @@
export class GetPostsByUserIdQueryHandler {
constructor(postRepository) {
this.postRepository = postRepository;
}
async handle(query) {
return await this.postRepository.getByUserId(query.userId);
}
}
@@ -0,0 +1,7 @@
export class CreateUserCommand {
constructor(name, email, age) {
this.name = name;
this.email = email;
this.age = age;
}
}
@@ -0,0 +1,19 @@
export class CreateUserCommandHandler {
constructor(userRepository) {
this.userRepository = userRepository;
}
async handle(command) {
if (!command.name || !command.email) {
throw new Error('Name and email are required');
}
const userData = {
name: command.name,
email: command.email,
age: command.age
};
return await this.userRepository.create(userData);
}
}
@@ -0,0 +1,5 @@
export class DeleteUserCommand {
constructor(id) {
this.id = id;
}
}
@@ -0,0 +1,13 @@
export class DeleteUserCommandHandler {
constructor(userRepository) {
this.userRepository = userRepository;
}
async handle(command) {
const result = await this.userRepository.delete(command.id);
if (!result) {
throw new Error('User not found');
}
return result;
}
}
@@ -0,0 +1,8 @@
export class UpdateUserCommand {
constructor(id, name, email, age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}
}
@@ -0,0 +1,18 @@
export class UpdateUserCommandHandler {
constructor(userRepository) {
this.userRepository = userRepository;
}
async handle(command) {
const userData = {};
if (command.name) userData.name = command.name;
if (command.email) userData.email = command.email;
if (command.age !== undefined) userData.age = command.age;
const result = await this.userRepository.update(command.id, userData);
if (!result) {
throw new Error('User not found');
}
return result;
}
}
@@ -0,0 +1,3 @@
export class GetAllUsersQuery {
constructor() {}
}
@@ -0,0 +1,9 @@
export class GetAllUsersQueryHandler {
constructor(userRepository) {
this.userRepository = userRepository;
}
async handle(query) {
return await this.userRepository.getAll();
}
}
@@ -0,0 +1,5 @@
export class GetUserByIdQuery {
constructor(id) {
this.id = id;
}
}
@@ -0,0 +1,13 @@
export class GetUserByIdQueryHandler {
constructor(userRepository) {
this.userRepository = userRepository;
}
async handle(query) {
const user = await this.userRepository.getById(query.id);
if (!user) {
throw new Error('User not found');
}
return user;
}
}