Skip to content

Commit

Permalink
refactor: closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrdsilva committed May 4, 2024
1 parent 2b484cc commit 8ab77f7
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 173 deletions.
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AuthModule } from './auth/auth.module';
import { PostCommentModule } from './modules/post-comment.module';
import { StoryModule } from './modules/story.module';
import { ScheduleModule } from '@nestjs/schedule';
import { ResponseModule } from './modules/response.module';
import { ReplyModule } from './modules/reply.module';
import { BlogModule } from './modules/blog.module';

@Module({
Expand All @@ -30,7 +30,7 @@ import { BlogModule } from './modules/blog.module';
PostModule,
PostCommentModule,
StoryModule,
ResponseModule,
ReplyModule,
BlogModule,
],
})
Expand Down
9 changes: 4 additions & 5 deletions src/entities/post-comment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
UpdateDateColumn,
} from 'typeorm';
import { Post } from './post.entity';
import { Response } from './response.entity';
import { Reply } from './reply.entity';
import { User } from './user.entity';
import { Field, ID, Int, ObjectType } from '@nestjs/graphql';

Expand All @@ -28,12 +28,11 @@ export class PostComment {
@JoinColumn({ name: 'post_id' })
post: Post;

@OneToMany(() => Response, (response) => response.comment)
// @Field(() => [Response])
responses: Response[];
@OneToMany(() => Reply, (replies) => replies.comment)
replies: Reply[];

@Field(() => Int)
totalResponses: number;
totalReplies: number;

@ManyToOne(() => User, (user) => user.comments)
@JoinColumn({ name: 'user_id' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import { User } from './user.entity';

@Entity()
@ObjectType()
export class Response {
export class Reply {
@PrimaryGeneratedColumn('uuid')
@Field(() => ID)
id: string;

@Column({ type: 'text' })
@Field(() => String)
response: string;
reply: string;

@ManyToOne(() => User, (user) => user.comments)
@JoinColumn({ name: 'user_id' })
@Field(() => User)
user: User;

@ManyToOne(() => PostComment, (comment) => comment.responses, { onDelete: 'CASCADE' })
@ManyToOne(() => PostComment, (comment) => comment.replies, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'comment_id' })
@Field(() => PostComment)
comment: PostComment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class CreateResponseInput {
@Field(() => String, { description: 'The content of the response' })
response: string;
export class CreateReplyInput {
@Field(() => String, { description: 'The content of the reply' })
reply: string;

@Field(() => String, { description: 'The ID of the comment' })
commentId: string;
Expand Down
5 changes: 5 additions & 0 deletions src/models/dtos/update-reply.input.ts
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) {}
5 changes: 0 additions & 5 deletions src/models/dtos/update-response.input.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/modules/reply.module.ts
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 {}
12 changes: 0 additions & 12 deletions src/modules/response.module.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/providers/post-comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class PostCommentService {
'u.profilePicture',
'p.id',
])
.loadRelationCountAndMap('pc.totalResponses', 'pc.responses')
.loadRelationCountAndMap('pc.totalReplies', 'pc.replies')
.leftJoin('pc.user', 'u')
.leftJoin('pc.post', 'p')
.where('p.id = :postId', { postId })
Expand Down
83 changes: 83 additions & 0 deletions src/providers/reply.service.ts
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."
});
}
}
83 changes: 0 additions & 83 deletions src/providers/response.service.ts

This file was deleted.

49 changes: 49 additions & 0 deletions src/resolvers/reply.resolver.ts
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);
}
}
49 changes: 0 additions & 49 deletions src/resolvers/response.resolver.ts

This file was deleted.

Loading

0 comments on commit 8ab77f7

Please sign in to comment.