Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
devbenho committed Sep 20, 2024
1 parent 742ea52 commit d28189a
Show file tree
Hide file tree
Showing 41 changed files with 762 additions and 472 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/starsships-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions server/src/application/likes/add-like.request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { UseCaseRequest } from '@application/shared';
import { TriggeredBy } from '@domain/shared/entities';
import { InvalidParameterException } from '@domain/shared/exceptions';

class AddCommentRequest extends UseCaseRequest {
readonly triggeredBy: TriggeredBy;
readonly starshipId: string;
readonly text: string;

private constructor(
triggeredBy: TriggeredBy,
starshipId: string,
text: string,
) {
super(triggeredBy);
this.starshipId = starshipId;
this.text = text;
}

public static create(
triggeredBy: TriggeredBy,
starshipId: string,
text: string,
): AddCommentRequest {
return new AddCommentRequest(triggeredBy, starshipId, text);
}

protected validatePayload(): void {
if (this.starshipId == null) {
throw new InvalidParameterException('StarshipId must be provided');
}
if (this.text == null) {
throw new InvalidParameterException('Text must be provided');
}
}
}

export { AddCommentRequest };
50 changes: 50 additions & 0 deletions server/src/application/likes/add-like.use-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {UnauthorizedError} from '@contracts/errors/unauthorized.error';
import {BaseUseCase, UseCase} from '@application/shared';
import {Logger} from '@domain/shared';
import {UserResponseDto} from '@contracts/dtos/users';
import {AddCommentRequest} from './add-comment.request';
import {CommentResponseDto} from '@contracts/dtos/comments/comment.response';
import {CommentModel} from '@infrastructure/comments/comment.model';
import {Comment} from "@domain/entities/comments/comment";
import {Schema, Types} from "mongoose";

@UseCase()
class AddCommentUsecase extends BaseUseCase<AddCommentRequest, CommentResponseDto> {

constructor() {
super();
}

public async performOperation(
request: AddCommentRequest,
): Promise<CommentResponseDto> {

const userId = new Types.ObjectId(request.triggeredBy.who).toString();
const comment: Comment = Comment.Create(null, request.text, userId, new Date(), userId, request.starshipId, null, null, null);
const createdComment = await CommentModel.create(comment);
await createdComment.save();
const populatedComment = await CommentModel.findById(createdComment.id).populate('userId') as any;
if (!populatedComment) {
throw new UnauthorizedError('Comment not found');
}

const result = CommentResponseDto.Create(
populatedComment._id,
populatedComment.userId._id,
{
name: populatedComment.userId.name,
email: populatedComment.userId.email,
scope: populatedComment.userId.scope,
},
populatedComment.text,
populatedComment.starshipId,
populatedComment.createdAt,
populatedComment.updatedAt,
populatedComment.deletedAt
);

return result;
}
}

export {AddCommentUsecase};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { UseCaseRequest } from '@application/shared';
import { TriggeredBy } from '@domain/shared/entities';
import { InvalidParameterException } from '@domain/shared/exceptions';

class GetCommentsByStarshipId extends UseCaseRequest {
readonly triggeredBy: TriggeredBy;
readonly starshipId: string;

private constructor(
triggeredBy: TriggeredBy,
starshipId: string,
) {
super(triggeredBy);
this.starshipId = starshipId;
}

public static create(
triggeredBy: TriggeredBy,
starshipId: string,
): GetCommentsByStarshipId {
return new GetCommentsByStarshipId(triggeredBy, starshipId);
}

protected validatePayload(): void {
if (this.starshipId == null) {
throw new InvalidParameterException('StarshipId must be provided');
}
}
}

export { GetCommentsByStarshipId };
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {UnauthorizedError} from '@contracts/errors/unauthorized.error';
import {BaseUseCase, UseCase} from '@application/shared';
import {Logger} from '@domain/shared';
import {UserResponseDto} from '@contracts/dtos/users';
import {CommentResponseDto} from '@contracts/dtos/comments/comment.response';
import {CommentModel} from '@infrastructure/comments/comment.model';
import {Comment} from "@domain/entities/comments/comment";
import {GetCommentsByStarshipId} from "@application/comments/get-comments-by-starship-id/get-comments-by-starship-id.request";

@UseCase()
class GetCommentsByStarshipIdUseCase extends BaseUseCase<GetCommentsByStarshipId, CommentResponseDto[]> {

constructor() {
super();
}

public async performOperation(
request: GetCommentsByStarshipId,
): Promise<CommentResponseDto[]> {
// get comments by starship id
const comments = await CommentModel.find({ starshipId: request.starshipId }).populate('userId') as any;

const result = comments.map((comment: { _id: string; userId: { _id: string; name: any; email: any; scope: any; }; text: string; starshipId: string; createdAt: Date; updatedAt: Date; deletedAt: Date; }) => {
return CommentResponseDto.Create(
comment._id,
comment.userId._id,
{
name: comment.userId.name,
email: comment.userId.email,
scope: comment.userId.scope,
},
comment.text,
comment.starshipId,
comment.createdAt,
comment.updatedAt,
comment.deletedAt
);
});

return result;
}
}

export { GetCommentsByStarshipIdUseCase };
77 changes: 0 additions & 77 deletions server/src/contracts/dtos/chats/find-all/find-all.response.ts

This file was deleted.

16 changes: 0 additions & 16 deletions server/src/contracts/dtos/comments/comment-details.response.ts

This file was deleted.

27 changes: 0 additions & 27 deletions server/src/contracts/dtos/comments/comment.response.ts

This file was deleted.

2 changes: 0 additions & 2 deletions server/src/contracts/dtos/comments/index.ts

This file was deleted.

56 changes: 56 additions & 0 deletions server/src/contracts/dtos/likes/like.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { UserResponseDto } from "../users";

class CommentResponseDto{
id: string;
userId: string;
user: Pick<UserResponseDto, 'name' | 'email' | 'scope'>;
text: string;
starshipId: string;
createdAt: Date;
updatedAt: Date;
deletedAt: Date;
constructor(
id: string,
userId: string,
user: Pick<UserResponseDto, 'name' | 'email' | 'scope'>,
text: string,
starshipId: string,
createdAt: Date,
updatedAt: Date,
deletedAt: Date
){
this.id = id;
this.userId = userId;
this.user = user;
this.text = text;
this.starshipId = starshipId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.deletedAt = deletedAt;
}


public static Create(
id: string,
userId: string,
user: Pick<UserResponseDto, 'name' | 'email' | 'scope'>,
text: string,
starshipId: string,
createdAt: Date,
updatedAt: Date,
deletedAt: Date
): CommentResponseDto{
return new CommentResponseDto(
id,
userId,
user,
text,
starshipId,
createdAt,
updatedAt,
deletedAt
);
}
}

export { CommentResponseDto };
Loading

0 comments on commit d28189a

Please sign in to comment.