From 2b6524d8b983c45219fd17c2ed69c4f489ba3085 Mon Sep 17 00:00:00 2001 From: Mariela Date: Fri, 1 Jul 2022 16:27:19 -0600 Subject: [PATCH] add comentario de comunicados agregar methodos en api gateway, conexion a bd --- api-gateway/src/app.controller.ts | 25 ++++++++++++ api-gateway/src/app.service.ts | 37 ++++++++++++++++++ .../dto/create-post-comment.dto.ts | 1 - .../dto/update-post-comment.dto.ts | 6 --- .../entities/post-comment.entity.ts | 1 - .../post-comments.controller.spec.ts | 20 ---------- .../post-comments/post-comments.controller.ts | 32 ++++++++------- .../src/post-comments/post-comments.module.ts | 5 +++ .../post-comments.service.spec.ts | 18 --------- .../post-comments/post-comments.service.ts | 39 +++++++++++++------ .../src/schemas/post-comment.schema.ts | 25 ++++++++++++ 11 files changed, 136 insertions(+), 73 deletions(-) delete mode 100644 servicio-foro-comunicaciones/src/post-comments/dto/create-post-comment.dto.ts delete mode 100644 servicio-foro-comunicaciones/src/post-comments/dto/update-post-comment.dto.ts delete mode 100644 servicio-foro-comunicaciones/src/post-comments/entities/post-comment.entity.ts delete mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.controller.spec.ts delete mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.service.spec.ts create mode 100644 servicio-foro-comunicaciones/src/schemas/post-comment.schema.ts diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index da057f30..edbba811 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -220,4 +220,29 @@ export class AppController { ) { return this.appService.findPost(paramPost); } + + + // #==== API Comment + + @Post('post/createComment') + createComment( + @Body('comment') comment: string, + @Body('date_entry') date_entry: Date, + @Body('user_id') user_id: string, + @Body('post_id') post_id: string, + ) { + return this.appService.createComment(comment, date_entry, user_id, post_id); + } + + @Get('post/allComments') + allComments() { + return this.appService.allComments(); + } + + @Get('post/findComment/:id') + findComment( + @Param('id') paramComment: string + ) { + return this.appService.findComment(paramComment); + } } \ No newline at end of file diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index b469e280..ba7b1bb9 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -304,4 +304,41 @@ export class AppService { ); } + // ====================== COMMNENT POSTS =============================== + + //Comment parameter from API + createComment(comment: string, date_entry: Date, user_id: string, + post_id: string) { + const pattern = { cmd: 'createComment' }; + const payload = { + comment: comment, date_entry: date_entry, user_id: user_id, + post_id: post_id + }; + return this.clientPostApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allComments() { + const pattern = { cmd: 'findAllComments' }; + const payload = {}; + return this.clientPostApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findComment(paramCommentId: string) { + const pattern = { cmd: 'findOneComment' }; + const payload = { id: paramCommentId }; + return this.clientPostApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } } \ No newline at end of file diff --git a/servicio-foro-comunicaciones/src/post-comments/dto/create-post-comment.dto.ts b/servicio-foro-comunicaciones/src/post-comments/dto/create-post-comment.dto.ts deleted file mode 100644 index 74fd1dc3..00000000 --- a/servicio-foro-comunicaciones/src/post-comments/dto/create-post-comment.dto.ts +++ /dev/null @@ -1 +0,0 @@ -export class CreatePostCommentDto {} diff --git a/servicio-foro-comunicaciones/src/post-comments/dto/update-post-comment.dto.ts b/servicio-foro-comunicaciones/src/post-comments/dto/update-post-comment.dto.ts deleted file mode 100644 index be875464..00000000 --- a/servicio-foro-comunicaciones/src/post-comments/dto/update-post-comment.dto.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { PartialType } from '@nestjs/mapped-types'; -import { CreatePostCommentDto } from './create-post-comment.dto'; - -export class UpdatePostCommentDto extends PartialType(CreatePostCommentDto) { - id: number; -} diff --git a/servicio-foro-comunicaciones/src/post-comments/entities/post-comment.entity.ts b/servicio-foro-comunicaciones/src/post-comments/entities/post-comment.entity.ts deleted file mode 100644 index 51929fcd..00000000 --- a/servicio-foro-comunicaciones/src/post-comments/entities/post-comment.entity.ts +++ /dev/null @@ -1 +0,0 @@ -export class PostComment {} diff --git a/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.spec.ts b/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.spec.ts deleted file mode 100644 index 0db5ca52..00000000 --- a/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.spec.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { PostCommentsController } from './post-comments.controller'; -import { PostCommentsService } from './post-comments.service'; - -describe('PostCommentsController', () => { - let controller: PostCommentsController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [PostCommentsController], - providers: [PostCommentsService], - }).compile(); - - controller = module.get(PostCommentsController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.ts b/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.ts index fa7ea40e..8d999b8b 100644 --- a/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.ts +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.ts @@ -1,35 +1,37 @@ import { Controller } from '@nestjs/common'; import { MessagePattern, Payload } from '@nestjs/microservices'; import { PostCommentsService } from './post-comments.service'; -import { CreatePostCommentDto } from './dto/create-post-comment.dto'; -import { UpdatePostCommentDto } from './dto/update-post-comment.dto'; +import { Comment, CommentDocument } from '../schemas/post-comment.schema'; + @Controller() export class PostCommentsController { constructor(private readonly postCommentsService: PostCommentsService) {} - @MessagePattern('createPostComment') - create(@Payload() createPostCommentDto: CreatePostCommentDto) { - return this.postCommentsService.create(createPostCommentDto); + @MessagePattern({ cmd: 'createComment' }) + create(@Payload() comment: CommentDocument) { + return this.postCommentsService.create(comment); } - @MessagePattern('findAllPostComments') + @MessagePattern({ cmd: 'findAllComments' }) findAll() { return this.postCommentsService.findAll(); } - @MessagePattern('findOnePostComment') - findOne(@Payload() id: number) { - return this.postCommentsService.findOne(id); + @MessagePattern({cmd: 'findOneComment'}) + findOne(@Payload() id: string) { + let _id = id['id']; + return this.postCommentsService.findOne(_id); } - @MessagePattern('updatePostComment') - update(@Payload() updatePostCommentDto: UpdatePostCommentDto) { - return this.postCommentsService.update(updatePostCommentDto.id, updatePostCommentDto); + @MessagePattern({ cmd: 'updateComment' }) + update(@Payload() comment: CommentDocument) { + return this.postCommentsService.update(comment.id, comment); } - @MessagePattern('removePostComment') - remove(@Payload() id: number) { - return this.postCommentsService.remove(id); + @MessagePattern({ cmd: 'removeComment' }) + remove(@Payload() id: string) { + let _id = id['id']; + return this.postCommentsService.remove(_id); } } diff --git a/servicio-foro-comunicaciones/src/post-comments/post-comments.module.ts b/servicio-foro-comunicaciones/src/post-comments/post-comments.module.ts index 50fbd6e2..70b26350 100644 --- a/servicio-foro-comunicaciones/src/post-comments/post-comments.module.ts +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.module.ts @@ -1,8 +1,13 @@ import { Module } from '@nestjs/common'; import { PostCommentsService } from './post-comments.service'; import { PostCommentsController } from './post-comments.controller'; +import { MongooseModule } from '@nestjs/mongoose'; +import { Comment, CommentSchema } from '../schemas/post-comment.schema'; @Module({ + imports: [ + MongooseModule.forFeature([{ name: Comment.name, schema: CommentSchema }]), + ], controllers: [PostCommentsController], providers: [PostCommentsService] }) diff --git a/servicio-foro-comunicaciones/src/post-comments/post-comments.service.spec.ts b/servicio-foro-comunicaciones/src/post-comments/post-comments.service.spec.ts deleted file mode 100644 index a47e2e9a..00000000 --- a/servicio-foro-comunicaciones/src/post-comments/post-comments.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { PostCommentsService } from './post-comments.service'; - -describe('PostCommentsService', () => { - let service: PostCommentsService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [PostCommentsService], - }).compile(); - - service = module.get(PostCommentsService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/servicio-foro-comunicaciones/src/post-comments/post-comments.service.ts b/servicio-foro-comunicaciones/src/post-comments/post-comments.service.ts index 36e81375..525697d3 100644 --- a/servicio-foro-comunicaciones/src/post-comments/post-comments.service.ts +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.service.ts @@ -1,26 +1,41 @@ import { Injectable } from '@nestjs/common'; -import { CreatePostCommentDto } from './dto/create-post-comment.dto'; -import { UpdatePostCommentDto } from './dto/update-post-comment.dto'; +import { Comment, CommentDocument } from '../schemas/post-comment.schema'; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; + @Injectable() export class PostCommentsService { - create(createPostCommentDto: CreatePostCommentDto) { - return 'This action adds a new postComment'; + constructor( + @InjectModel(Comment.name) private readonly commentModel: Model, + ) {} + + async create(comment: CommentDocument): Promise { + return this.commentModel.create(comment); } - findAll() { - return `This action returns all postComments`; + async findAll(): Promise { + return this.commentModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); + } + + async findOne(id: string): Promise { + return this.commentModel.findOne({ _id: id }).exec(); } - findOne(id: number) { - return `This action returns a #${id} postComment`; + async findOneByDNI(dni: string): Promise { + return this.commentModel.findOne({ dni: dni }).exec(); } - update(id: number, updatePostCommentDto: UpdatePostCommentDto) { - return `This action updates a #${id} postComment`; + async update(id: string, comment: CommentDocument) { + return this.commentModel.findOneAndUpdate({ _id: id }, comment, { + new: true, + }); } - remove(id: number) { - return `This action removes a #${id} postComment`; + async remove(id: string) { + return this.commentModel.findByIdAndRemove({ _id: id }).exec(); } } diff --git a/servicio-foro-comunicaciones/src/schemas/post-comment.schema.ts b/servicio-foro-comunicaciones/src/schemas/post-comment.schema.ts new file mode 100644 index 00000000..8ba0d65f --- /dev/null +++ b/servicio-foro-comunicaciones/src/schemas/post-comment.schema.ts @@ -0,0 +1,25 @@ +import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import { Document, ObjectId } from 'mongoose'; + + +export type CommentDocument = Comment & Document; + +@Schema({ collection: 'comments' }) +export class Comment { + + @Prop() + comment: string; + + @Prop() + date_entry: Date; + + @Prop() + user_id: string + + @Prop() + post_id: string; + +} + + +export const CommentSchema = SchemaFactory.createForClass(Comment); \ No newline at end of file