-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b484cc
commit 8ab77f7
Showing
15 changed files
with
173 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/models/dtos/create-response.input.ts → src/models/dtos/create-reply.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { CreateReplyInput } from './create-reply.input'; | ||
import { InputType, Field, Int, PartialType } from '@nestjs/graphql'; | ||
|
||
@InputType() | ||
export class UpdateReplyInput extends PartialType(CreateReplyInput) {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Reply } from 'src/entities/reply.entity'; | ||
import { ReplyService } from '../providers/reply.service'; | ||
import { ReplyResolver } from '../resolvers/reply.resolver'; | ||
import { PostCommentModule } from './post-comment.module'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Reply]), PostCommentModule], | ||
providers: [ReplyResolver, ReplyService], | ||
}) | ||
export class ReplyModule {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Reply } from 'src/entities/reply.entity'; | ||
import { User } from 'src/entities/user.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { CreateReplyInput } from '../models/dtos/create-reply.input'; | ||
import { UpdateReplyInput } from '../models/dtos/update-reply.input'; | ||
import { PostCommentService } from './post-comment.service'; | ||
|
||
@Injectable() | ||
export class ReplyService { | ||
constructor( | ||
@InjectRepository(Reply) | ||
private readonly replyRepository: Repository<Reply>, | ||
private readonly postCommentService: PostCommentService, | ||
) {} | ||
|
||
async create(request: Request, createReplyInput: CreateReplyInput) { | ||
const user: User = request['user']; | ||
const postComment = await this.postCommentService.findById(createReplyInput.commentId); | ||
const reply = new Reply(); | ||
|
||
reply.reply = createReplyInput.reply; | ||
reply.user = user; | ||
reply.comment = postComment; | ||
|
||
return await this.replyRepository.save(reply); | ||
} | ||
|
||
async findByPostComment(commentId: string) { | ||
const queryBuilder = this.replyRepository | ||
.createQueryBuilder('r') | ||
.select([ | ||
'r.id', | ||
'r.reply', | ||
'r.createdAt', | ||
'r.updatedAt', | ||
'u.id', | ||
'u.username', | ||
'u.profilePicture', | ||
'pc.id', | ||
]) | ||
.leftJoin('r.user', 'u') | ||
.leftJoin('r.comment', 'pc') | ||
.where('pc.id = :commentId', { commentId }); | ||
|
||
return await queryBuilder.getMany(); | ||
} | ||
|
||
findOne(id: number) { | ||
return `This action returns a #${id} reply`; | ||
} | ||
|
||
async update(request: Request, replyId: string, updateReplyInput: UpdateReplyInput) { | ||
const user: User = request['user']; | ||
const reply = await this.replyRepository.findOne({ where: { id: replyId }, relations: { user: true } }); | ||
|
||
if (reply.user.id !== user.id) { | ||
throw new ForbiddenException('You are not allowed to perform this action.'); | ||
} | ||
|
||
reply.reply = updateReplyInput.reply; | ||
|
||
return await this.replyRepository.save(reply); | ||
} | ||
|
||
async remove(request: Request, replyId: string) { | ||
const user: User = request['user']; | ||
const reply = await this.replyRepository.findOne({ where: { id: replyId }, relations: { user: true } }); | ||
|
||
if (!reply) { | ||
throw new NotFoundException('Reply not found. Maybe you might have already deleted it?'); | ||
} | ||
|
||
if (reply.user.id !== user.id) { | ||
throw new ForbiddenException('You are not allowed to perform this action.'); | ||
} | ||
|
||
return await this.replyRepository.remove(reply).then(() => { | ||
return "This reply has been removed." | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { UseGuards } from '@nestjs/common'; | ||
import { Args, Context, Int, Mutation, Query, Resolver } from '@nestjs/graphql'; | ||
import { JwtAuthGuard } from 'src/auth/auth.guard'; | ||
import { Reply } from '../entities/reply.entity'; | ||
import { CreateReplyInput } from '../models/dtos/create-reply.input'; | ||
import { UpdateReplyInput } from '../models/dtos/update-reply.input'; | ||
import { ReplyService } from '../providers/reply.service'; | ||
|
||
@Resolver(() => Reply) | ||
export class ReplyResolver { | ||
constructor(private readonly replyService: ReplyService) {} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Mutation(() => Reply) | ||
createReply( | ||
@Context() context: { req: Request }, | ||
@Args('createReplyInput') createReplyInput: CreateReplyInput, | ||
) { | ||
return this.replyService.create(context.req, createReplyInput); | ||
} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Query(() => [Reply]) | ||
findRepliesByPostComment(@Args('commentId') commentId: string) { | ||
return this.replyService.findByPostComment(commentId); | ||
} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Query(() => Reply) | ||
findReplyById(@Args('id', { type: () => Int }) id: number) { | ||
return this.replyService.findOne(id); | ||
} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Mutation(() => Reply) | ||
updateReply( | ||
@Context() context: { req: Request }, | ||
@Args('replyId') replyId: string, | ||
@Args('updateReplyInput') updateReplyInput: UpdateReplyInput, | ||
) { | ||
return this.replyService.update(context.req, replyId, updateReplyInput); | ||
} | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Mutation(() => String) | ||
removeReply(@Context() context: { req: Request }, @Args('replyId') replyId: string) { | ||
return this.replyService.remove(context.req, replyId); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.