Skip to content

Commit

Permalink
Posts and comments cleanup read and write operations | triggered by w…
Browse files Browse the repository at this point in the history
…orks well
  • Loading branch information
devbenho committed Jun 22, 2024
1 parent 658835e commit 698d2b0
Show file tree
Hide file tree
Showing 33 changed files with 860 additions and 320 deletions.
Binary file modified server/db.sqlite3
Binary file not shown.
21 changes: 6 additions & 15 deletions server/src/application/comment/create/create-comment.request.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@

import { UseCaseRequest } from '@application/shared';
import { TriggeredBy } from '@domain/shared/entities/triggered-by';
// import { InvalidParameterException } from '@domain/shared/exceptions';

class CreateCommentRequest extends UseCaseRequest {

readonly postId: string;
readonly content: string;

// Constructor Section
constructor(
triggeredBy: TriggeredBy,
postId: string,
content: string,
) {
constructor(triggeredBy: TriggeredBy, postId: string, content: string) {
super(triggeredBy);
this.postId = postId;
this.content = content;
Expand All @@ -24,15 +17,13 @@ class CreateCommentRequest extends UseCaseRequest {
postId: string,
content: string,
): CreateCommentRequest {
return new CreateCommentRequest(
triggeredBy,
postId,
content,
);
}
return new CreateCommentRequest(triggeredBy, postId, content);
}

// Validate here using EnsureClass
protected validatePayload(): void {
if (!this.postId || !this.content || !this.triggeredBy) {
throw new Error('Invalid request payload provided');
}
}
}

Expand Down
40 changes: 31 additions & 9 deletions server/src/application/comment/create/create-comment.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import { BaseUseCase, UseCase } from '@application/shared';
import { CreateCommentRequest } from './create-comment.request';
import { CommentDetailsResponseDto } from '@contracts/dtos/comments';
@UseCase()
class CreateCommentUseCase extends BaseUseCase<CreateCommentRequest,CommentDetailsResponseDto> {
import { CommentResponseDto } from '@contracts/dtos/comments';
import { Comment, CommentRepository } from '@domain/entities';
import { Long } from 'typeorm';
import { Logger } from '@domain/shared';

constructor() {
super();
@UseCase()
class CreateCommentUseCase extends BaseUseCase<
CreateCommentRequest,
CommentResponseDto
> {
private readonly _commentRepository: CommentRepository;
constructor(commentRepository: CommentRepository) {
super();
this._commentRepository = commentRepository;
}

public async performOperation({ }: CreateCommentRequest): Promise<CommentDetailsResponseDto> {
throw new Error('Method not implemented.');
public async performOperation(
request: CreateCommentRequest,
): Promise<CommentResponseDto> {
const comment: Comment = Comment.create(
null,
request.postId,
null,
request.triggeredBy.toString(),
null,
request.content,
new Date(),
new Date(),
null,
);

const createdComment = await this._commentRepository.create(comment);
return CommentResponseDto.fromEntity(createdComment);
}
}

export { CreateCommentUseCase };
export { CreateCommentUseCase };
13 changes: 6 additions & 7 deletions server/src/application/comment/delete/delete-comment.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { BaseUseCase, UseCase } from '@application/shared';
import { DeleteCommentRequest } from './delete-comment.request';

@UseCase()
class DeleteCommentUseCase extends BaseUseCase<DeleteCommentRequest,bool> {

@UseCase()
class DeleteCommentUseCase extends BaseUseCase<DeleteCommentRequest, boolean> {
constructor() {
super();
super();
}

public async performOperation({ }: DeleteCommentRequest): Promise<bool> {
throw new Error('Method not implemented.');
public async performOperation({}: DeleteCommentRequest): Promise<boolean> {
throw new Error('Method not implemented.');
}
}

export { DeleteCommentUseCase };
export { DeleteCommentUseCase };
17 changes: 10 additions & 7 deletions server/src/application/comment/edit/edit-comment.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { BaseUseCase, UseCase } from '@application/shared';
import { EditCommentRequest } from './edit-comment.request';

@UseCase()
class EditCommentUseCase extends BaseUseCase<EditCommentRequest,CommentDetailsResponseDto> {
import { CommentDetailsResponseDto } from '@contracts/dtos/comments';

@UseCase()
class EditCommentUseCase extends BaseUseCase<
EditCommentRequest,
CommentDetailsResponseDto
> {
constructor() {
super();
super();
}

public async performOperation({ }: EditCommentRequest): Promise<CommentDetailsResponseDto> {
throw new Error('Method not implemented.');
public async performOperation({}: EditCommentRequest): Promise<CommentDetailsResponseDto> {
throw new Error('Method not implemented.');
}
}

export { EditCommentUseCase };
export { EditCommentUseCase };
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

import { UseCaseRequest } from '@application/shared';
import { TriggeredBy } from '@domain/shared/entities/triggered-by';
// import { InvalidParameterException } from '@domain/shared/exceptions';

class GetByPostIdCommentRequest extends UseCaseRequest {

class FindCommentsByPostIdRequest extends UseCaseRequest {
readonly postId: string;
readonly pageSize: number;
readonly pageNumber: number;
Expand All @@ -27,18 +25,17 @@ class GetByPostIdCommentRequest extends UseCaseRequest {
postId: string,
pageSize: number,
pageNumber: number,
): GetByPostIdCommentRequest {
return new GetByPostIdCommentRequest(
): FindCommentsByPostIdRequest {
return new FindCommentsByPostIdRequest(
triggeredBy,
postId,
pageSize,
pageNumber,
);
}
}

// Validate here using EnsureClass
protected validatePayload(): void {
}
protected validatePayload(): void {}
}

export { GetByPostIdCommentRequest };
export { FindCommentsByPostIdRequest };
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { BaseUseCase, UseCase } from '@application/shared';
import { GetByPostIdCommentRequest } from './get-by-post-id-comment.request';

@UseCase()
class GetByPostIdCommentUseCase extends BaseUseCase<GetByPostIdCommentRequest,CommentResponseDto> {
import { FindCommentsByPostIdRequest } from './get-by-post-id-comment.request';
import { CommentResponseDto } from '@contracts/dtos/comments';
import { CommentRepository } from '@domain/entities';
import { Long } from 'typeorm';
import { Logger } from '@domain/shared';

constructor() {
super();
@UseCase()
class FindCommentsByPostIdUseCase extends BaseUseCase<
FindCommentsByPostIdRequest,
CommentResponseDto[]
> {
private readonly _commentRepository: CommentRepository;
constructor(commentRepository: CommentRepository) {
super();
this._commentRepository = commentRepository;
}

public async performOperation({ }: GetByPostIdCommentRequest): Promise<CommentResponseDto> {
throw new Error('Method not implemented.');
public async performOperation(
request: FindCommentsByPostIdRequest,
): Promise<CommentResponseDto[]> {
Logger.info('Get comments by post id');
const comments = await this._commentRepository.findByPostId(request.postId);
Logger.info('Get comments by post id', comments);

return comments.map(comment => CommentResponseDto.fromEntity(comment));
}
}

export { GetByPostIdCommentUseCase };
export { FindCommentsByPostIdUseCase };
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { BaseUseCase, UseCase } from '@application/shared';
import { GetDetailCommentRequest } from './get-detail-comment.request';

@UseCase()
class GetDetailCommentUseCase extends BaseUseCase<GetDetailCommentRequest,CommentDetailsResponseDto> {
import { CommentDetailsResponseDto } from '@contracts/dtos/comments';

@UseCase()
class GetDetailCommentUseCase extends BaseUseCase<
GetDetailCommentRequest,
CommentDetailsResponseDto
> {
constructor() {
super();
super();
}

public async performOperation({ }: GetDetailCommentRequest): Promise<CommentDetailsResponseDto> {
throw new Error('Method not implemented.');
public async performOperation({}: GetDetailCommentRequest): Promise<CommentDetailsResponseDto> {
throw new Error('Method not implemented.');
}
}

export { GetDetailCommentUseCase };
export { GetDetailCommentUseCase };
8 changes: 1 addition & 7 deletions server/src/application/post/create/create-post.use-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { PostRepository } from '@domain/entities/posts/post.repository';
import { PostDetailsResponseDto } from '@contracts/dtos/posts/post-details.response';
import { CreatePostRequest } from './create-post.request';
import { Post } from '@domain/entities';
import { Logger } from '@domain/shared';

@UseCase()
class CreatePostUseCase extends BaseUseCase<
Expand All @@ -12,14 +11,11 @@ class CreatePostUseCase extends BaseUseCase<
> {
private readonly _postRepository: PostRepository;

constructor(
postRepository: PostRepository,
) {
constructor(postRepository: PostRepository) {
super();
this._postRepository = postRepository;
}


public async performOperation(
request: CreatePostRequest,
): Promise<PostDetailsResponseDto> {
Expand All @@ -36,9 +32,7 @@ class CreatePostUseCase extends BaseUseCase<
null,
null,
);
Logger.info('CreatePostUseCase.performOperation', post);
const createdPost = await this._postRepository.createPost(post);
Logger.info('Done', createdPost);
if (createdPost) {
return PostDetailsResponseDto.fromEntity(createdPost);
}
Expand Down
6 changes: 0 additions & 6 deletions server/src/application/post/find-all/find-all-post.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { BaseUseCase, UseCase } from '@application/shared';
import { FindAllPostRequest } from './find-all-post.request';
import { PostResponseDto } from '@contracts/dtos/posts';
import { PostRepository } from '@domain/entities/posts/post.repository';
import { log } from 'console';
import { Logger } from '@domain/shared';

@UseCase()
class FindAllPostUseCase extends BaseUseCase<
Expand All @@ -19,21 +17,17 @@ class FindAllPostUseCase extends BaseUseCase<
public async performOperation(
request: FindAllPostRequest,
): Promise<PostResponseDto[]> {
Logger.info('FindAllPostUseCase.performOperation');
const { pageSize, pageNumber } = request;
const posts = await this._postRepository.findAll(
pageSize || 10,
pageNumber || 1,
);

Logger.info('posts from usecase', posts);
const postDtos = await Promise.all(
posts.map(async post => {
return await PostResponseDto.fromEntity(post);
}),
);
Logger.info('postsDtos from usecase', postDtos);

return postDtos;
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/application/shared/base-usecase.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { performance } from 'node:perf_hooks';

import { UseCaseRequest } from './usecase.request';
import { injectable } from 'inversify';
import { Logger } from '@domain/shared';

@injectable()
abstract class BaseUseCase<IRequest extends UseCaseRequest, IResponse> {
public async execute(request: IRequest): Promise<IResponse> {
try {
Expand All @@ -12,6 +11,7 @@ abstract class BaseUseCase<IRequest extends UseCaseRequest, IResponse> {
const response = await this.performOperation(request);
const endTime = performance.now();
const useCaseExecutionTime = endTime - startTime;
Logger.debug(`Use case execution time: ${useCaseExecutionTime}ms`);
return response;
} catch (error) {
throw error;
Expand Down
16 changes: 10 additions & 6 deletions server/src/contracts/dtos/comments/comment.response.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { Comment } from '@domain/entities';
import { Nullable } from '@domain/shared/types';
import { Comment, User } from '@domain/entities';
import { Nullable } from '@domain/shared';
import { UserResponseDto } from '@dtos/users';

export class CommentResponseDto {
constructor(
public id: string,
public content: string,
public author: UserResponseDto,
public authorId: string,
public author: Nullable<UserResponseDto>,
public postId: string,
public createdAt: Nullable<Date>,
public createdAt: Date,
public updatedAt: Date,
) {}

public static fromEntity(entity: Comment): CommentResponseDto {
return new CommentResponseDto(
entity.id!,
entity.id as string,
entity.content,
UserResponseDto.fromEntity(entity.author as any),
entity.authorId,
UserResponseDto.fromEntity(entity.author as User),
entity.postId,
entity.createdAt,
entity.updatedAt,
);
}
}
16 changes: 8 additions & 8 deletions server/src/domain/entities/comments/comment.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Comment } from '@domain/entities';

interface ICommentRepository {
create(comment: Comment): Promise<Comment>;
findByPostId(postId: string): Promise<Comment[]>;
findById(commentId: string): Promise<Comment | null>;
findByAuthor(authorId: string): Promise<Comment[]>;
delete(commentId: string): Promise<void>;
update(comment: Comment): Promise<Comment>;
abstract class CommentRepository {
abstract create(comment: Comment): Promise<Comment>;
abstract findByPostId(postId: string): Promise<Comment[]>;
abstract findById(commentId: string): Promise<Comment | null>;
abstract findByAuthor(authorId: string): Promise<Comment[]>;
abstract delete(commentId: string): Promise<boolean>;
abstract update(comment: Comment): Promise<Comment>;
}

export { ICommentRepository };
export { CommentRepository };
Loading

0 comments on commit 698d2b0

Please sign in to comment.