From 06dc11e2971a87ba2033ba88a4d81e94409be45c Mon Sep 17 00:00:00 2001 From: Mariela Date: Fri, 1 Jul 2022 14:53:38 -0600 Subject: [PATCH] add post microservicio ( schema, controller, service, api gateway) --- api-gateway/src/app.controller.ts | 35 ++++++++++++++--- api-gateway/src/app.module.ts | 2 +- api-gateway/src/app.service.ts | 38 +++++++++++++++++++ .../package-lock.json | 1 + servicio-foro-comunicaciones/package.json | 1 + .../src/app.module.ts | 24 +++++++++--- servicio-foro-comunicaciones/src/main.ts | 20 +++++++--- .../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 | 35 +++++++++++++++++ .../src/post-comments/post-comments.module.ts | 9 +++++ .../post-comments.service.spec.ts | 18 +++++++++ .../post-comments/post-comments.service.ts | 26 +++++++++++++ .../src/posts/posts.controller.ts | 37 ++++++++++++++++++ .../src/posts/posts.module.ts | 14 +++++++ .../src/posts/posts.service.ts | 36 ++++++++++++++++++ .../src/schemas/post.schema.ts | 24 ++++++++++++ .../src/schemas/reservation.schema.ts | 2 - servicio-usuarios/src/users/users.service.ts | 2 - 21 files changed, 331 insertions(+), 21 deletions(-) create mode 100644 servicio-foro-comunicaciones/src/post-comments/dto/create-post-comment.dto.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/dto/update-post-comment.dto.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/entities/post-comment.entity.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.controller.spec.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.controller.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.module.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.service.spec.ts create mode 100644 servicio-foro-comunicaciones/src/post-comments/post-comments.service.ts create mode 100644 servicio-foro-comunicaciones/src/posts/posts.controller.ts create mode 100644 servicio-foro-comunicaciones/src/posts/posts.module.ts create mode 100644 servicio-foro-comunicaciones/src/posts/posts.service.ts create mode 100644 servicio-foro-comunicaciones/src/schemas/post.schema.ts diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index dfbd7223..da057f30 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -5,7 +5,7 @@ import { AppService } from "./app.service"; export class AppController { constructor(private readonly appService: AppService) { } - //#API userService - create user + // #==== API Users @Post('user/createAdminSystem') createAdminSystem( @Body('dni') dni: string, @@ -53,7 +53,6 @@ export class AppController { // #==== API Communities - //#API orderService - create order @Post('community/createCommunity') createCommunity( @Body('name') name: string, @@ -87,7 +86,6 @@ export class AppController { // #==== API Common Areas - //#API orderService - create order @Post('commonArea/createCommonArea') createCommonArea( @Body('name') name: string, @@ -144,7 +142,7 @@ export class AppController { // #==== API Payment - //#API userService - create user + @Post('payment/createPayment') createPayment( @Body('date_payment') date_payment: Date, @@ -171,8 +169,8 @@ export class AppController { return this.appService.findPayment(paramPaymentDNI); } - // #==== API Payment - //#API userService - create user + // #==== API Reservation + @Post('reservation/createReservation') createReservation( @Body('start_time') start_time: string, @@ -197,4 +195,29 @@ export class AppController { ) { return this.appService.findReservation(paramReservation); } + + + // #==== API Post + + @Post('post/createPost') + createPost( + @Body('post') post: string, + @Body('date_entry') date_entry: Date, + @Body('user_id') user_id: string, + @Body('community_id') community_id: string, + ) { + return this.appService.createPost(post, date_entry, user_id, community_id); + } + + @Get('post/allPosts') + allPosts() { + return this.appService.allPosts(); + } + + @Get('post/find/:id') + findPost( + @Param('id') paramPost: string + ) { + return this.appService.findPost(paramPost); + } } \ No newline at end of file diff --git a/api-gateway/src/app.module.ts b/api-gateway/src/app.module.ts index 95dd8242..b15e44b3 100644 --- a/api-gateway/src/app.module.ts +++ b/api-gateway/src/app.module.ts @@ -67,7 +67,7 @@ import { AppService } from './app.service'; ]), ClientsModule.register([ { - name: "SERVICIO_COMUNICADOS", + name: "SERVICIO_POSTS", transport: Transport.TCP, options: { host: "127.0.0.1", diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 79b66292..b469e280 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -12,6 +12,7 @@ export class AppService { @Inject('SERVICIO_INVITADOS') private readonly clientGuestApp: ClientProxy, @Inject('SERVICIO_PAGOS') private readonly clientPaymentApp: ClientProxy, @Inject('SERVICIO_RESERVACIONES') private readonly clientReservationApp: ClientProxy, + @Inject('SERVICIO_POSTS') private readonly clientPostApp: ClientProxy, @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, ) { } @@ -265,5 +266,42 @@ export class AppService { } + // ====================== POSTS =============================== + + //POST parameter from API + createPost(post: string, date_entry: Date, user_id: string, + community_id: string) { + const pattern = { cmd: 'createPost' }; + const payload = { + post: post, date_entry: date_entry, user_id: user_id, + community_id: community_id + }; + return this.clientPostApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allPosts() { + const pattern = { cmd: 'findAllPosts' }; + const payload = {}; + return this.clientPostApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findPost(paramPostId: string) { + const pattern = { cmd: 'findOnePost' }; + const payload = { id: paramPostId }; + return this.clientPostApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } } \ No newline at end of file diff --git a/servicio-foro-comunicaciones/package-lock.json b/servicio-foro-comunicaciones/package-lock.json index ffa460e8..f6a4aa4d 100644 --- a/servicio-foro-comunicaciones/package-lock.json +++ b/servicio-foro-comunicaciones/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.7", "@nestjs/mongoose": "^9.1.1", "@nestjs/platform-express": "^8.0.0", diff --git a/servicio-foro-comunicaciones/package.json b/servicio-foro-comunicaciones/package.json index f1a639de..94bc68c3 100644 --- a/servicio-foro-comunicaciones/package.json +++ b/servicio-foro-comunicaciones/package.json @@ -23,6 +23,7 @@ "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.7", "@nestjs/mongoose": "^9.1.1", "@nestjs/platform-express": "^8.0.0", diff --git a/servicio-foro-comunicaciones/src/app.module.ts b/servicio-foro-comunicaciones/src/app.module.ts index 86628031..1d4a7a7a 100644 --- a/servicio-foro-comunicaciones/src/app.module.ts +++ b/servicio-foro-comunicaciones/src/app.module.ts @@ -1,10 +1,24 @@ import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; +import { MongooseModule } from '@nestjs/mongoose'; +import { ClientsModule, Transport } from "@nestjs/microservices"; +import { PostsModule } from './posts/posts.module'; +import { PostCommentsModule } from './post-comments/post-comments.module'; @Module({ - imports: [], - controllers: [AppController], - providers: [AppService], + imports: [ ClientsModule.register([ + { + name: "SERVICIO_POSTS", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3007 + } + } + ]), + MongooseModule.forRoot(`mongodb+srv://proyecto_4:proyecto_4@proyecto4.yv4fb.mongodb.net/servicio_posts?retryWrites=true&w=majority`), + PostsModule, + PostCommentsModule], + controllers: [], + providers: [], }) export class AppModule {} diff --git a/servicio-foro-comunicaciones/src/main.ts b/servicio-foro-comunicaciones/src/main.ts index 13cad38c..e6451292 100644 --- a/servicio-foro-comunicaciones/src/main.ts +++ b/servicio-foro-comunicaciones/src/main.ts @@ -1,8 +1,18 @@ -import { NestFactory } from '@nestjs/core'; -import { AppModule } from './app.module'; +import { NestFactory } from "@nestjs/core"; +import { Transport } from "@nestjs/microservices"; +import { AppModule } from "./app.module"; +import { Logger } from "@nestjs/common"; + +const logger = new Logger(); async function bootstrap() { - const app = await NestFactory.create(AppModule); - await app.listen(3000); + const app = await NestFactory.createMicroservice(AppModule, { + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3007 + } + }); + app.listen().then(() => logger.log("Microservice Comunicados is listening" )); } -bootstrap(); +bootstrap(); \ 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 new file mode 100644 index 00000000..74fd1dc3 --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/dto/create-post-comment.dto.ts @@ -0,0 +1 @@ +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 new file mode 100644 index 00000000..be875464 --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/dto/update-post-comment.dto.ts @@ -0,0 +1,6 @@ +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 new file mode 100644 index 00000000..51929fcd --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/entities/post-comment.entity.ts @@ -0,0 +1 @@ +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 new file mode 100644 index 00000000..0db5ca52 --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.spec.ts @@ -0,0 +1,20 @@ +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 new file mode 100644 index 00000000..fa7ea40e --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.controller.ts @@ -0,0 +1,35 @@ +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'; + +@Controller() +export class PostCommentsController { + constructor(private readonly postCommentsService: PostCommentsService) {} + + @MessagePattern('createPostComment') + create(@Payload() createPostCommentDto: CreatePostCommentDto) { + return this.postCommentsService.create(createPostCommentDto); + } + + @MessagePattern('findAllPostComments') + findAll() { + return this.postCommentsService.findAll(); + } + + @MessagePattern('findOnePostComment') + findOne(@Payload() id: number) { + return this.postCommentsService.findOne(id); + } + + @MessagePattern('updatePostComment') + update(@Payload() updatePostCommentDto: UpdatePostCommentDto) { + return this.postCommentsService.update(updatePostCommentDto.id, updatePostCommentDto); + } + + @MessagePattern('removePostComment') + remove(@Payload() id: number) { + 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 new file mode 100644 index 00000000..50fbd6e2 --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { PostCommentsService } from './post-comments.service'; +import { PostCommentsController } from './post-comments.controller'; + +@Module({ + controllers: [PostCommentsController], + providers: [PostCommentsService] +}) +export class PostCommentsModule {} 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 new file mode 100644 index 00000000..a47e2e9a --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.service.spec.ts @@ -0,0 +1,18 @@ +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 new file mode 100644 index 00000000..36e81375 --- /dev/null +++ b/servicio-foro-comunicaciones/src/post-comments/post-comments.service.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@nestjs/common'; +import { CreatePostCommentDto } from './dto/create-post-comment.dto'; +import { UpdatePostCommentDto } from './dto/update-post-comment.dto'; + +@Injectable() +export class PostCommentsService { + create(createPostCommentDto: CreatePostCommentDto) { + return 'This action adds a new postComment'; + } + + findAll() { + return `This action returns all postComments`; + } + + findOne(id: number) { + return `This action returns a #${id} postComment`; + } + + update(id: number, updatePostCommentDto: UpdatePostCommentDto) { + return `This action updates a #${id} postComment`; + } + + remove(id: number) { + return `This action removes a #${id} postComment`; + } +} diff --git a/servicio-foro-comunicaciones/src/posts/posts.controller.ts b/servicio-foro-comunicaciones/src/posts/posts.controller.ts new file mode 100644 index 00000000..6779d5ca --- /dev/null +++ b/servicio-foro-comunicaciones/src/posts/posts.controller.ts @@ -0,0 +1,37 @@ +import { Controller } from '@nestjs/common'; +import { MessagePattern, Payload } from '@nestjs/microservices'; +import { PostsService } from './posts.service'; +import { Post, PostDocument } from '../schemas/post.schema'; + + +@Controller() +export class PostsController { + constructor(private readonly postsService: PostsService) { } + + @MessagePattern({ cmd: 'createPost' }) + create(@Payload() post: PostDocument) { + return this.postsService.create(post); + } + + @MessagePattern({ cmd: 'findAllPosts' }) + findAll() { + return this.postsService.findAll(); + } + + @MessagePattern({ cmd: 'findOnePost' }) + findOne(@Payload() id: string) { + let _id = id['id']; + return this.postsService.findOne(_id); + } + + @MessagePattern({ cmd: 'updatePost' }) + update(@Payload() post: PostDocument) { + return this.postsService.update(post.id, post); + } + + @MessagePattern({ cmd: 'removePost' }) + remove(@Payload() id: string) { + let _id = id['id']; + return this.postsService.remove(_id); + } +} diff --git a/servicio-foro-comunicaciones/src/posts/posts.module.ts b/servicio-foro-comunicaciones/src/posts/posts.module.ts new file mode 100644 index 00000000..d47c06aa --- /dev/null +++ b/servicio-foro-comunicaciones/src/posts/posts.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { PostsService } from './posts.service'; +import { PostsController } from './posts.controller'; +import { Post, PostSchema } from '../schemas/post.schema'; +import { MongooseModule } from '@nestjs/mongoose'; + +@Module({ + imports: [ + MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }]), + ], + controllers: [PostsController], + providers: [PostsService] +}) +export class PostsModule {} diff --git a/servicio-foro-comunicaciones/src/posts/posts.service.ts b/servicio-foro-comunicaciones/src/posts/posts.service.ts new file mode 100644 index 00000000..9442a198 --- /dev/null +++ b/servicio-foro-comunicaciones/src/posts/posts.service.ts @@ -0,0 +1,36 @@ +import { Injectable } from '@nestjs/common'; +import { Post, PostDocument } from '../schemas/post.schema'; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; + +@Injectable() +export class PostsService { + constructor( + @InjectModel(Post.name) private readonly postModel: Model, + ) { } + + async create(post: PostDocument): Promise { + return this.postModel.create(post); + } + + async findAll(): Promise { + return this.postModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); + } + + async findOne(id: string): Promise { + return this.postModel.findOne({ _id: id }).exec(); + } + + async update(id: string, post: PostDocument) { + return this.postModel.findOneAndUpdate({ _id: id }, post, { + new: true, + }); + } + + async remove(id: string) { + return this.postModel.findByIdAndRemove({ _id: id }).exec(); + } +} diff --git a/servicio-foro-comunicaciones/src/schemas/post.schema.ts b/servicio-foro-comunicaciones/src/schemas/post.schema.ts new file mode 100644 index 00000000..d76f724f --- /dev/null +++ b/servicio-foro-comunicaciones/src/schemas/post.schema.ts @@ -0,0 +1,24 @@ +import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import { Document, ObjectId } from 'mongoose'; + + +export type PostDocument = Post & Document; + +@Schema({ collection: 'posts' }) +export class Post { + + @Prop() + post: string; + + @Prop() + date_entry: Date; + + @Prop() + user_id: string + + @Prop() + community_id: string // id de la comunidad +} + + +export const PostSchema = SchemaFactory.createForClass(Post); \ No newline at end of file diff --git a/servicio-reservaciones/src/schemas/reservation.schema.ts b/servicio-reservaciones/src/schemas/reservation.schema.ts index c9b2d918..db3fd227 100644 --- a/servicio-reservaciones/src/schemas/reservation.schema.ts +++ b/servicio-reservaciones/src/schemas/reservation.schema.ts @@ -1,5 +1,3 @@ - - import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; import { Document, ObjectId } from 'mongoose'; diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 459c514c..8ef51075 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -19,7 +19,6 @@ export class UsersService { .setOptions({ sanitizeFilter: true }) .exec(); } - async findOne(id: string): Promise { return this.userModel.findOne({ _id: id }).exec(); @@ -35,7 +34,6 @@ export class UsersService { }); } - async remove(id: string) { return this.userModel.findByIdAndRemove({ _id: id }).exec(); }