-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 }; |
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 }; |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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 }; |