-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e066ea6
commit 71abea1
Showing
16 changed files
with
95 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | ||
import { INestApplication } from '@nestjs/common'; | ||
|
||
export const initSwagger = (app: INestApplication) => { | ||
const swaggerConfig = new DocumentBuilder() | ||
.setTitle('MyBlog API') | ||
.setDescription( | ||
'Esta es una API Creada con NestJS con un CRUD básico para un Blog.', | ||
) | ||
.build(); | ||
const document = SwaggerModule.createDocument(app, swaggerConfig); | ||
SwaggerModule.setup('/docs', app, document); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
|
||
/** | ||
* Converts an enum into a String | ||
* @param _enum Enum | ||
* @returns string type | ||
* @gist https://gist.github.com/ruslanguns/d5a6bd9af6bddb77d6b2f2a2fef82748 | ||
*/ | ||
export const EnumToString = (_enum: object) => | ||
export const EnumToString = (_enum: object) => | ||
Object.keys(_enum) | ||
.map(key => _enum[key]) | ||
.filter(value => typeof value === 'string') as string[] | ||
.filter(value => typeof value === 'string') as string[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
import { PartialType, OmitType } from '@nestjs/swagger'; | ||
import { CreatePostDto } from './create-post.dto'; | ||
|
||
|
||
export class EditPostDto extends PartialType( | ||
OmitType( | ||
CreatePostDto, | ||
['slug'] as const | ||
) | ||
) {} | ||
OmitType(CreatePostDto, ['slug'] as const), | ||
) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './create-post.dto' | ||
export * from './edit-post.dto' | ||
export * from './create-post.dto'; | ||
export * from './edit-post.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './post.entity' | ||
export * from './post.entity'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from "typeorm"; | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
CreateDateColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity('posts') | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
@Column({ type: 'text', nullable: false}) | ||
|
||
@Column({ type: 'text', nullable: false }) | ||
slug!: string; | ||
|
||
@Column({ type: 'varchar', length: 150 }) | ||
title!: string; | ||
|
||
@Column({ type: 'varchar', length: 255 }) | ||
excerpt?: string; | ||
|
||
@Column({ type: 'text' }) | ||
content!: string; | ||
|
||
@Column({ type: 'varchar', length: 100, nullable: true }) | ||
category: string; | ||
|
||
@Column({ type: 'simple-array' }) | ||
tags: string[]; | ||
|
||
@Column({ type: 'bool', default: true }) | ||
status: boolean; | ||
@CreateDateColumn({type: "timestamp"}) | ||
|
||
@CreateDateColumn({ type: 'timestamp' }) | ||
createdAt: Date; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './post-category.enum' | ||
export * from './post-category.enum'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export enum PostCategory { | ||
'TECHNOLOGY', | ||
'LIFESTYLE', | ||
'CODING' | ||
} | ||
'CODING', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,50 @@ | ||
import { Controller, Get, Param, Post, Put, Delete, Body, ParseIntPipe } from '@nestjs/common'; | ||
import { | ||
Controller, | ||
Get, | ||
Param, | ||
Post, | ||
Put, | ||
Delete, | ||
Body, | ||
ParseIntPipe, | ||
} from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
import { PostService } from './post.service'; | ||
import { CreatePostDto, EditPostDto } from './dtos'; | ||
|
||
@ApiTags('Posts') | ||
@Controller('post') | ||
export class PostController { | ||
|
||
constructor(private readonly postService: PostService) {} | ||
|
||
@Get() | ||
async getMany() { | ||
const data = await this.postService.getMany(); | ||
return { data } | ||
return { data }; | ||
} | ||
|
||
@Get(':id') | ||
async getById(@Param('id', ParseIntPipe) id: number) { | ||
const data = await this.postService.getById(id) | ||
return { data } | ||
const data = await this.postService.getById(id); | ||
return { data }; | ||
} | ||
|
||
@Post() | ||
async createPost(@Body() dto: CreatePostDto ) { | ||
const data = await this.postService.createOne(dto) | ||
return { message: 'Post created', data} | ||
async createPost(@Body() dto: CreatePostDto) { | ||
const data = await this.postService.createOne(dto); | ||
return { message: 'Post created', data }; | ||
} | ||
|
||
@Put(':id') | ||
async editOne( | ||
@Param('id') id: number, | ||
@Body() dto: EditPostDto | ||
) { | ||
const data = await this.postService.editOne(id, dto) | ||
return { message: 'Post edited', data } | ||
async editOne(@Param('id') id: number, @Body() dto: EditPostDto) { | ||
const data = await this.postService.editOne(id, dto); | ||
return { message: 'Post edited', data }; | ||
} | ||
|
||
@Delete(':id') | ||
async deleteOne(@Param('id') id: number) { | ||
const data = await this.postService.deleteOne(id) | ||
return { message: 'Post deleted', data } | ||
const data = await this.postService.deleteOne(id); | ||
return { message: 'Post deleted', data }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { PostController } from './post.controller'; | ||
import { PostService } from './post.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Post } from './entities'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Post]) | ||
], | ||
imports: [TypeOrmModule.forFeature([Post])], | ||
controllers: [PostController], | ||
providers: [PostService] | ||
providers: [PostService], | ||
}) | ||
export class PostModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters