Last active
September 30, 2020 12:44
-
-
Save talesmgodois/cc00e8b1ac6d6a5b1a946a5ee616dcd9 to your computer and use it in GitHub Desktop.
Why you shoud learn Nest.js
This file contains hidden or 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, CanActivate, ExecutionContext } from '@nestjs/common'; | |
import { Observable } from 'rxjs'; | |
@Injectable() | |
export class AuthGuard implements CanActivate { | |
canActivate( | |
context: ExecutionContext, | |
): boolean | Promise<boolean> | Observable<boolean> { | |
const request = context.switchToHttp().getRequest(); | |
return validateRequest(request); | |
} | |
} |
This file contains hidden or 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 { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | |
import { Document } from 'mongoose'; | |
export type CatDocument = Cat & Document; | |
@Schema() | |
export class Cat { | |
@Prop() | |
name: string; | |
@Prop() | |
age: number; | |
@Prop() | |
breed: string; | |
} | |
export const CatSchema = SchemaFactory.createForClass(Cat); |
This file contains hidden or 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 { Model } from 'mongoose'; | |
import { Injectable } from '@nestjs/common'; | |
import { InjectModel } from '@nestjs/mongoose'; | |
import { Cat, CatDocument } from './schemas/cat.schema'; | |
import { CreateCatDto } from './dto/create-cat.dto'; | |
@Injectable() | |
export class CatsService { | |
constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {} | |
async create(createCatDto: CreateCatDto): Promise<Cat> { | |
const createdCat = new this.catModel(createCatDto); | |
return createdCat.save(); | |
} | |
async findAll(): Promise<Cat[]> { | |
return this.catModel.find().exec(); | |
} | |
} |
This file contains hidden or 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 { Get, Controller, Render } from '@nestjs/common'; | |
import { AppService } from './app.service'; | |
@Controller('cats') | |
export class AppController { | |
constructor(private readonly appService: AppService) {} | |
@Get() | |
public async index() { | |
const message = this.appService.getHello(); | |
return { message }; | |
} | |
} |
This file contains hidden or 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
{ | |
"statusCode": 403, | |
"message": "Forbidden resource", | |
"error": "Forbidden" | |
} |
This file contains hidden or 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 { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; | |
@Entity() | |
export class User { | |
@PrimaryGeneratedColumn() | |
id: number; | |
@Column() | |
firstName: string; | |
@Column() | |
lastName: string; | |
@Column({ default: true }) | |
isActive: boolean; | |
} |
This file contains hidden or 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 } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { Repository } from 'typeorm'; | |
import { User } from './user.entity'; | |
@Injectable() | |
export class UsersService { | |
constructor( | |
@InjectRepository(User) | |
private usersRepository: Repository<User>, | |
) {} | |
findAll(): Promise<User[]> { | |
return this.usersRepository.find(); | |
} | |
findOne(id: string): Promise<User> { | |
return this.usersRepository.findOne(id); | |
} | |
async remove(id: string): Promise<void> { | |
await this.usersRepository.delete(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment