diff --git a/src/app.module.ts b/src/app.module.ts index e4f71d3eadc6821b7e65fb69825aaada5ee3b856..e0aa57e2278e56f027bcfe0d0db969e5936708c8 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -10,6 +10,7 @@ import { UsersModule } from './users/users.module'; import { MailerModule } from './mailer/mailer.module'; import { TclModule } from './tcl/tcl.module'; import { AdminModule } from './admin/admin.module'; +import { PostsModule } from './posts/posts.module'; @Module({ imports: [ ConfigurationModule, @@ -24,6 +25,7 @@ import { AdminModule } from './admin/admin.module'; MailerModule, TclModule, AdminModule, + PostsModule, ], controllers: [AppController], }) diff --git a/src/main.ts b/src/main.ts index ed8e487ca1cfc569b967338942d322c5c1f7f31c..d4287f5015f44ad7e5e76f6ab93023cd41caf1d7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,7 +6,12 @@ import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new ValidationPipe()); - const options = new DocumentBuilder().setTitle('RAM').setDescription('RAM API description').setVersion('1.0').build(); + const options = new DocumentBuilder() + .setTitle('RAM') + .setDescription('RAM API description') + .setVersion('1.0') + .addBearerAuth({ type: 'http', scheme: 'bearer', bearerFormat: 'JWT' }, 'JWT') + .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('api', app, document); await app.listen(3000); diff --git a/src/posts/posts.controller.spec.ts b/src/posts/posts.controller.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..7784335f5040742d323ac81d6e04f2940f8e9088 --- /dev/null +++ b/src/posts/posts.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PostsController } from './posts.controller'; + +describe('PostsController', () => { + let controller: PostsController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [PostsController], + }).compile(); + + controller = module.get<PostsController>(PostsController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..20d44db2ffa8ea667494c72afca4bf3c1bd97de4 --- /dev/null +++ b/src/posts/posts.controller.ts @@ -0,0 +1,38 @@ +import { Controller, Get, HttpService } from '@nestjs/common'; +import { Observable } from 'rxjs'; +import { Post } from './schemas/post.schema'; +import { map } from 'rxjs/operators'; +import { ApiBearerAuth, ApiQuery } from '@nestjs/swagger'; +import { Query } from '@nestjs/common'; + +@Controller('posts') +export class PostsController { + constructor(private readonly httpService: HttpService) {} + + @Get() + @ApiBearerAuth('JWT') + @ApiQuery({ name: 'include', type: String, required: false }) + @ApiQuery({ name: 'fields', type: String, required: false }) + @ApiQuery({ name: 'formats', type: String, required: false }) + @ApiQuery({ name: 'filter', type: String, required: false }) + @ApiQuery({ name: 'limit', type: String, required: false }) + @ApiQuery({ name: 'page', type: String, required: false }) + @ApiQuery({ name: 'order', type: String, required: false }) + public async findAll(@Query() query): Promise<Observable<{ posts: Post[] }>> { + console.log(query); + return this.httpService + .get(`${process.env.GHOST_HOST_AND_PORT}/ghost/api/v3/content/posts`, { + params: { + key: process.env.GHOST_CONTENT_API_KEY, + include: query.include ? query.include : null, + fields: query.fields ? query.fields : null, + formats: query.formats ? query.formats : null, + filter: query.filter ? query.filter : null, + limit: query.limit ? query.limit : null, + order: query.order ? query.order : null, + page: query.page ? query.page : null, + }, + }) + .pipe(map((response) => response.data)); + } +} diff --git a/src/posts/posts.module.ts b/src/posts/posts.module.ts new file mode 100644 index 0000000000000000000000000000000000000000..de11cb04f87397833dacfd9550c8947acfc9c591 --- /dev/null +++ b/src/posts/posts.module.ts @@ -0,0 +1,8 @@ +import { HttpModule, Module } from '@nestjs/common'; +import { PostsController } from './posts.controller'; + +@Module({ + imports: [HttpModule], + controllers: [PostsController], +}) +export class PostsModule {} diff --git a/src/posts/schemas/post.schema.ts b/src/posts/schemas/post.schema.ts new file mode 100644 index 0000000000000000000000000000000000000000..1711ec1f208295ec71881e4b480f21cd264f2fee --- /dev/null +++ b/src/posts/schemas/post.schema.ts @@ -0,0 +1,29 @@ +export class Post { + id: string; + uuid: string; + title: string; + slug: string; + html: string; + comment_id: string; + feature_image: null; + featured: false; + visibility: string; + email_recipient_filter: string; + created_at: string; + updated_at: string; + published_at: string; + url: string; + excerpt: string; + reading_time: string; + access: boolean; + send_email_when_published: boolean; + og_image: string; + og_title: string; + og_description: string; + twitter_image: string; + twitter_title: string; + twitter_description: string; + meta_title: string; + meta_description: string; + email_subject: string; +} diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 9c8c2c31b19ad0fec488cc11fd3ec1e53976d93a..39a79d0d03de09ee580862e40f1b7126497fcf80 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, Get, Param, Post, Query, Request, UseGuards } from '@nestjs/common'; -import { ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { PasswordChangeDto } from './dto/change-password.dto'; import { EmailChangeDto } from './dto/change-email.dto'; @@ -13,6 +13,7 @@ export class UsersController { constructor(private usersService: UsersService) {} @UseGuards(JwtAuthGuard) + @ApiBearerAuth('JWT') @ApiOperation({ description: 'Get user profile' }) @ApiResponse({ status: 200, description: 'Return user profil' }) @ApiResponse({ status: 401, description: 'User does not have sufficient rights' }) @@ -22,6 +23,7 @@ export class UsersController { } @Post() + @ApiBearerAuth('JWT') @ApiResponse({ status: 201, description: 'User created' }) public async create(@Body() createUserDto: CreateUserDto) { // remove structureId for creation and add structure after @@ -38,6 +40,7 @@ export class UsersController { } @Post('verify/:id') + @ApiBearerAuth('JWT') @ApiParam({ name: 'id', type: String, required: true }) @ApiResponse({ status: 201, description: 'User verified' }) @ApiResponse({ status: 401, description: "This token does'nt exist or is not associate to this user." }) @@ -46,6 +49,7 @@ export class UsersController { } @UseGuards(JwtAuthGuard) + @ApiBearerAuth('JWT') @Post('change-password') @ApiResponse({ status: 201, description: 'Password changed' }) @ApiResponse({ status: 401, description: 'Invalid password' }) @@ -59,6 +63,7 @@ export class UsersController { } @UseGuards(JwtAuthGuard) + @ApiBearerAuth('JWT') @Post('change-email') @ApiResponse({ status: 201, description: 'Email confirmation send' }) @ApiResponse({ status: 401, description: 'Invalid Email' }) @@ -67,6 +72,7 @@ export class UsersController { } @UseGuards(JwtAuthGuard) + @ApiBearerAuth('JWT') @Post('verify-change-email') @ApiResponse({ status: 201, description: 'Email changed' }) @ApiResponse({ status: 401, description: 'Invalid Token' }) @@ -75,12 +81,14 @@ export class UsersController { } @Post('reset-password') + @ApiBearerAuth('JWT') @ApiResponse({ status: 200, description: 'Email sent if account exist' }) public async resetPassword(@Body() passwordReset: PasswordResetDto) { return this.usersService.sendResetPasswordEmail(passwordReset.email); } @Post('reset-password/apply') + @ApiBearerAuth('JWT') @ApiResponse({ status: 200, description: 'Email sent if account exist' }) public async resetPasswordApply(@Body() passwordResetApplyDto: PasswordResetApplyDto) { return this.usersService.validatePasswordResetToken(passwordResetApplyDto.password, passwordResetApplyDto.token);