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
import { | |
Controller, Get, Post, Put, Delete, Param, Body, NotFoundException, UseGuards, Request | |
} from '@nestjs/common'; | |
import { AuthGuard } from '@nestjs/passport'; | |
import { PostsService } from './posts.service'; | |
import { Post as PostEntity } from './post.entity'; | |
import { PostDto } from './dto/post.dto'; | |
@Controller('posts') |
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
import { Injectable, Inject } from '@nestjs/common'; | |
import { Post } from './post.entity'; | |
import { PostDto } from './dto/post.dto'; | |
import { User } from '../users/user.entity'; | |
import { POST_REPOSITORY } from '../../core/constants'; | |
@Injectable() | |
export class PostsService { | |
constructor( |
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
import { Module } from '@nestjs/common'; | |
import { PostsService } from './posts.service'; | |
import { PostsController } from './posts.controller'; | |
import { postsProviders } from './posts.providers'; | |
@Module({ | |
providers: [PostsService, ...postsProviders], | |
controllers: [PostsController], | |
}) |
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
import { Post } from './post.entity'; | |
import { POST_REPOSITORY } from '../../core/constants'; | |
export const postsProviders = [{ | |
provide: POST_REPOSITORY, | |
useValue: Post, | |
}]; |
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
import { IsNotEmpty, MinLength } from 'class-validator'; | |
export class PostDto { | |
@IsNotEmpty() | |
@MinLength(4) | |
readonly title: string; | |
@IsNotEmpty() | |
readonly body: 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
import { Table, Column, Model, DataType, ForeignKey, BelongsTo } from 'sequelize-typescript'; | |
import { User } from '../users/user.entity'; | |
@Table | |
export class Post extends Model<Post> { | |
@Column({ | |
type: DataType.STRING, | |
allowNull: false, | |
}) |
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
import { Controller, Body, Post, UseGuards, Request } from '@nestjs/common'; | |
import { AuthGuard } from '@nestjs/passport'; | |
import { AuthService } from './auth.service'; | |
import { UserDto } from '../users/dto/user.dto'; | |
import { DoesUserExist } from '../../core/guards/doesUserExist.guard'; | |
@Controller('auth') | |
export class AuthController { | |
constructor(private authService: AuthService) { } |
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
import { CanActivate, ExecutionContext, Injectable, ForbiddenException } from '@nestjs/common'; | |
import { Observable } from 'rxjs'; | |
import { UsersService } from '../../modules/users/users.service'; | |
@Injectable() | |
export class DoesUserExist implements CanActivate { | |
constructor(private readonly userService: UsersService) {} | |
canActivate( |
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
import { IsNotEmpty, MinLength, IsEmail, IsEnum } from 'class-validator'; | |
enum Gender { | |
MALE = 'male', | |
FEMALE = 'female', | |
} | |
export class UserDto { | |
@IsNotEmpty() | |
readonly name: 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
import { NestFactory } from '@nestjs/core'; | |
import { AppModule } from './app.module'; | |
import { ValidateInputPipe } from './core/pipes/validate.pipe'; | |
async function bootstrap() { | |
const app = await NestFactory.create(AppModule); | |
// global endpoints prefix | |
app.setGlobalPrefix('api/v1'); | |
// handle all user input validation globally |