From 5c193884bd5e3e55af92a492eea449dc94bae3a1 Mon Sep 17 00:00:00 2001 From: Santiago Olayo Date: Sun, 23 Jun 2024 17:02:19 -0500 Subject: [PATCH] feat: add post endpoint --- src/app.module.ts | 2 ++ src/main.ts | 6 ++++-- src/posts/post.entity.ts | 33 +++++++++++++++++++++++++++++++++ src/posts/posts.controller.ts | 16 ++++++++++++++++ src/posts/posts.module.ts | 9 +++++++++ src/posts/posts.service.ts | 10 ++++++++++ 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/posts/post.entity.ts create mode 100644 src/posts/posts.controller.ts create mode 100644 src/posts/posts.module.ts create mode 100644 src/posts/posts.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 3bc9ada..e3e5120 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,6 +6,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { SecretsManagerModule } from './secrets-manager/secrets-manager.module'; import { SecretsManagerService } from './secrets-manager/secrets-manager.service'; import { DatabaseCredentials } from './secrets-manager/secret-value-interfaces/database-credentials.interface'; +import { PostsModule } from './posts/posts.module'; @Module({ @@ -30,6 +31,7 @@ import { DatabaseCredentials } from './secrets-manager/secret-value-interfaces/d }; }, }), + PostsModule, ], controllers: [AppController], providers: [AppService], diff --git a/src/main.ts b/src/main.ts index c6fc654..93555fb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,16 +5,18 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.setGlobalPrefix('api'); + const config = new DocumentBuilder() .setTitle('Auth Microservice') .setDescription('The Auth API') .setVersion('1.0') - .addTag('nestjs') + .addTag('NestJS') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('docs', app, document); - + await app.listen(process.env.PORT ?? 3000); } bootstrap(); diff --git a/src/posts/post.entity.ts b/src/posts/post.entity.ts new file mode 100644 index 0000000..9887f04 --- /dev/null +++ b/src/posts/post.entity.ts @@ -0,0 +1,33 @@ +import { + Entity, + Column, + PrimaryGeneratedColumn, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; + +@Entity({ + name: 'posts', +}) +export class Post { + @PrimaryGeneratedColumn() + id: number; + + @Column() + title: string; + + @Column({ + type: 'text', + name: 'image_url', + }) + imageUrl: string; + + @CreateDateColumn({ name: 'created_at' }) + createdAt: string; + + @UpdateDateColumn({ name: 'updated_at' }) + updatedAt: string; + + @Column({ default: true, name: 'is_active' }) + isActive: boolean; +} diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts new file mode 100644 index 0000000..a988770 --- /dev/null +++ b/src/posts/posts.controller.ts @@ -0,0 +1,16 @@ +import { Controller, Get } from '@nestjs/common'; +import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { PostsService } from './posts.service'; +import { Post } from './post.entity'; + +@Controller('/v1/posts') +export class PostsController { + constructor(private readonly postService: PostsService) {} + + @Get('') + @ApiOperation({ summary: 'List posts' }) + @ApiResponse({ status: 200, description: 'Post found' }) + async getPosts(): Promise { + return this.postService.getPosts(); + } +} diff --git a/src/posts/posts.module.ts b/src/posts/posts.module.ts new file mode 100644 index 0000000..b010511 --- /dev/null +++ b/src/posts/posts.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { PostsService } from './posts.service'; +import { PostsController } from './posts.controller'; + +@Module({ + providers: [PostsService], + controllers: [PostsController] +}) +export class PostsModule {} diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts new file mode 100644 index 0000000..98b7237 --- /dev/null +++ b/src/posts/posts.service.ts @@ -0,0 +1,10 @@ +import { Injectable } from '@nestjs/common'; +import { Post } from './post.entity'; + +@Injectable() +export class PostsService { + + getPosts(): Promise { + return Promise.resolve([]); + } +}