parent
56ee91e762
commit
b382f0689b
@ -1,17 +1,9 @@
|
|||||||
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()
|
||||||
@ -20,10 +12,16 @@ class CreateTrack {
|
|||||||
|
|
||||||
@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>;
|
||||||
|
|
||||||
@ -37,24 +35,46 @@ export class TrackController {
|
|||||||
return this.trackRepository.find();
|
return this.trackRepository.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/:id")
|
@Get('/:id')
|
||||||
@ResponseSchema(Track)
|
@ResponseSchema(Track)
|
||||||
getOne(@Param("id") id: number) {
|
@OnUndefined(TrackNotFoundError)
|
||||||
|
getOne(@Param('id') id: number) {
|
||||||
return this.trackRepository.findOne({ id: id });
|
return this.trackRepository.findOne({ id: id });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
post(@Body({ validate: true }) track: CreateTrack) {
|
@ResponseSchema(Track)
|
||||||
|
post(
|
||||||
|
@Body({ validate: true })
|
||||||
|
track: CreateTrack
|
||||||
|
) {
|
||||||
return this.trackRepository.save(track);
|
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 });
|
||||||
|
|
||||||
|
if (!oldTrack) {
|
||||||
|
throw new TrackNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("/:id")
|
await this.trackRepository.update(oldTrack, track);
|
||||||
remove(@Param("id") id: number) {
|
return track;
|
||||||
return this.trackRepository.delete({ id: id });
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user