add post microservicio ( schema, controller, service, api gateway)

This commit is contained in:
Mariela 2022-07-01 14:53:38 -06:00
parent e6dac76303
commit 06dc11e297
21 changed files with 331 additions and 21 deletions

View File

@ -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);
}
}

View File

@ -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",

View File

@ -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<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
allPosts() {
const pattern = { cmd: 'findAllPosts' };
const payload = {};
return this.clientPostApp
.send<string>(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<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
}

View File

@ -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",

View File

@ -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",

View File

@ -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 {}

View File

@ -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();

View File

@ -0,0 +1 @@
export class CreatePostCommentDto {}

View File

@ -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;
}

View File

@ -0,0 +1 @@
export class PostComment {}

View File

@ -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>(PostCommentsController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -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);
}
}

View File

@ -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 {}

View File

@ -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>(PostCommentsService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -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`;
}
}

View File

@ -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);
}
}

View File

@ -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 {}

View File

@ -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<PostDocument>,
) { }
async create(post: PostDocument): Promise<Post> {
return this.postModel.create(post);
}
async findAll(): Promise<Post[]> {
return this.postModel
.find()
.setOptions({ sanitizeFilter: true })
.exec();
}
async findOne(id: string): Promise<Post> {
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();
}
}

View File

@ -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);

View File

@ -1,5 +1,3 @@
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import { Document, ObjectId } from 'mongoose';

View File

@ -20,7 +20,6 @@ export class UsersService {
.exec();
}
async findOne(id: string): Promise<User> {
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();
}