Added errors and fixed the create model

ref #4
This commit is contained in:
Nicolai Ort 2020-11-27 18:48:22 +01:00
parent 56ee91e762
commit b382f0689b

View File

@ -1,60 +1,80 @@
import { import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined } from 'routing-controllers';
JsonController, import { getConnectionManager, Repository } from 'typeorm';
Param, import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
Body, import { ResponseSchema } from 'routing-controllers-openapi';
Get, import { Track } from '../models/Track';
Post, import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
Put,
Delete,
} from "routing-controllers";
import { getConnectionManager, Repository } from "typeorm";
import { EntityFromBody } from "typeorm-routing-controllers-extensions";
import { ResponseSchema } from "routing-controllers-openapi";
import { Track } from "../models/Track";
import { IsInt, IsNotEmpty, IsPositive, IsString } from "class-validator";
class CreateTrack { class CreateTrack {
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
name: string; name: string;
@IsInt() @IsInt()
@IsPositive() @IsPositive()
length: string; length: number;
} }
@JsonController("/track") export class TrackNotFoundError extends NotFoundError {
constructor() {
super('Track not found!');
}
}
@JsonController('/tracks')
export class TrackController { export class TrackController {
private trackRepository: Repository<Track>; private trackRepository: Repository<Track>;
constructor() { constructor() {
this.trackRepository = getConnectionManager().get().getRepository(Track); this.trackRepository = getConnectionManager().get().getRepository(Track);
} }
@Get() @Get()
@ResponseSchema(Track, { isArray: true }) @ResponseSchema(Track, { isArray: true })
getAll() { getAll() {
return this.trackRepository.find(); return this.trackRepository.find();
} }
@Get("/:id") @Get('/:id')
@ResponseSchema(Track) @ResponseSchema(Track)
getOne(@Param("id") id: number) { @OnUndefined(TrackNotFoundError)
return this.trackRepository.findOne({ id: id }); getOne(@Param('id') id: number) {
} return this.trackRepository.findOne({ id: id });
}
@Post() @Post()
post(@Body({ validate: true }) track: CreateTrack) { @ResponseSchema(Track)
return this.trackRepository.save(track); post(
} @Body({ validate: true })
track: CreateTrack
) {
return this.trackRepository.save(track);
}
@Put("/:id") @Put('/:id')
put(@Param("id") id: number, @EntityFromBody() track: Track) { @ResponseSchema(Track)
return this.trackRepository.update({ id: id }, track); async put(@Param('id') id: number, @EntityFromBody() track: Track) {
} let oldTrack = await this.trackRepository.findOne({ id: id });
@Delete("/:id") if (!oldTrack) {
remove(@Param("id") id: number) { throw new TrackNotFoundError();
return this.trackRepository.delete({ id: id }); }
}
await this.trackRepository.update(oldTrack, track);
return track;
}
@Delete('/:id')
@ResponseSchema(Track)
@OnUndefined(TrackNotFoundError)
async remove(@Param('id') id: number) {
let track = await this.trackRepository.findOne({ id: id });
if (!track) {
throw new TrackNotFoundError();
}
await this.trackRepository.delete(track);
return track;
}
} }