Skip to content

Commit

Permalink
modified: src/module/muscle/muscle.controller.ts
Browse files Browse the repository at this point in the history
	modified:   src/module/muscle/muscle.service.ts
  • Loading branch information
dleo9307 committed Jan 4, 2021
1 parent 03056d1 commit 34400c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/module/muscle/muscle.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Post } from '@nestjs/common';
import { Body, Controller, Delete, Get, Post, Param } from '@nestjs/common';
import { MuscleService } from './muscle.service';
import { MuscleCreateInput } from './dto/muscle.dto';

Expand All @@ -16,6 +16,11 @@ export class MuscleController {
return this.muscleService.create(muscleCreateInput);
}

@Delete(':id')
async deleteById(@Param('id') id: number) {
return this.muscleService.deleteById(id);
}

@Delete()
async deleteAll() {
return this.muscleService.deleteAll();
Expand Down
13 changes: 12 additions & 1 deletion src/module/muscle/muscle.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { Sequelize } from 'sequelize-typescript';
import { Muscle } from './muscle.entity';
import { MuscleCreateInput } from './dto/muscle.dto';
Expand All @@ -15,11 +15,22 @@ export class MuscleService {
return this.muscleRepository.findAll();
}

async getOrNotFound(id: number): Promise<Muscle> {
const muscle: Muscle = await this.muscleRepository.findByPk(id);
if(!muscle) throw new BadRequestException('Not found muscle');
return muscle;
}

async create(input: MuscleCreateInput): Promise<Muscle> {
return this.muscleRepository.create(input);
}

async deleteAll(): Promise<void> {
return this.muscleRepository.truncate();
}

async deleteById(id: number): Promise<void> {
const muscle: Muscle = await this.getOrNotFound(id);
muscle.destroy();
}
}

0 comments on commit 34400c8

Please sign in to comment.