backend feladat
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user