Permalink
Browse files

added user cocktail relation with cocktail aggregation

  • Loading branch information...
nelson-lopez committed Nov 9, 2019
1 parent 7861fa9 commit 1d336b1f80c5674e47b7f1ad1a85bc893ccaadeb
@@ -1,4 +1,12 @@
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import {
Controller,
Post,
Body,
ValidationPipe,
Param,
ParseIntPipe,
Patch,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@@ -11,6 +19,14 @@ export class AuthController {
return this.authService.signUp(authCredentialsDto);
}

@Patch(':id')
addCocktail(
@Param('id', ParseIntPipe) id: number,
@Body('cocktailId', ParseIntPipe) cocktailId: number,
) {
return this.authService.addCocktail(id, cocktailId);
}

@Post('/signin')
signIn(
@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto,
@@ -4,6 +4,7 @@ import { UserRepository } from './user.repository';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { JwtPayload } from './jwt-payload.interface';
import { JwtService } from '@nestjs/jwt';
import { User } from './user.entity';

@Injectable()
export class AuthService {
@@ -32,4 +33,8 @@ export class AuthService {
const accessToken = await this.jwtService.sign(payload);
return { accessToken };
}

addCocktail(id: number, cockTailId: number): Promise<User> {
return this.userRepository.addCocktail(id, cockTailId);
}
}
@@ -4,8 +4,11 @@ import {
BaseEntity,
PrimaryGeneratedColumn,
Column,
ManyToMany,
JoinTable,
} from 'typeorm';
import * as bcrypt from 'bcrypt';
import { Cocktail } from 'src/cocktail/cocktail.entity';

@Entity()
@Unique(['username'])
@@ -18,6 +21,9 @@ export class User extends BaseEntity {
password: string;
@Column()
salt: string;
@ManyToMany(type => Cocktail)
@JoinTable()
cocktails: Cocktail;

async validatePassword(password: string): Promise<boolean> {
const hash = await bcrypt.hash(password, this.salt);
@@ -1,11 +1,12 @@
import { EntityRepository, Repository } from 'typeorm';
import { EntityRepository, Repository, getCustomRepository } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { User } from './user.entity';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import {
ConflictException,
InternalServerErrorException,
} from '@nestjs/common';
import { CocktailRepository } from 'src/cocktail/cocktail.repository';

@EntityRepository(User)
export class UserRepository extends Repository<User> {
@@ -32,6 +33,15 @@ export class UserRepository extends Repository<User> {
}
}

async addCocktail(id: number, cocktailId: number): Promise<User> {
const user = await this.findOne(id);
const cocktailRepository = getCustomRepository(CocktailRepository);
const cocktail = await cocktailRepository.findOne(cocktailId);
user.cocktails = cocktail;
user.save();
return user;
}

/// validate user password

async validatUserPassword(

0 comments on commit 1d336b1

Please sign in to comment.