parent
56ee91e762
commit
b382f0689b
|
@ -1,60 +1,80 @@
|
|||
import {
|
||||
JsonController,
|
||||
Param,
|
||||
Body,
|
||||
Get,
|
||||
Post,
|
||||
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";
|
||||
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined } 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 {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
length: string;
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
length: number;
|
||||
}
|
||||
|
||||
@JsonController("/track")
|
||||
export class TrackNotFoundError extends NotFoundError {
|
||||
constructor() {
|
||||
super('Track not found!');
|
||||
}
|
||||
}
|
||||
|
||||
@JsonController('/tracks')
|
||||
export class TrackController {
|
||||
private trackRepository: Repository<Track>;
|
||||
private trackRepository: Repository<Track>;
|
||||
|
||||
constructor() {
|
||||
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
||||
}
|
||||
constructor() {
|
||||
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ResponseSchema(Track, { isArray: true })
|
||||
getAll() {
|
||||
return this.trackRepository.find();
|
||||
}
|
||||
@Get()
|
||||
@ResponseSchema(Track, { isArray: true })
|
||||
getAll() {
|
||||
return this.trackRepository.find();
|
||||
}
|
||||
|
||||
@Get("/:id")
|
||||
@ResponseSchema(Track)
|
||||
getOne(@Param("id") id: number) {
|
||||
return this.trackRepository.findOne({ id: id });
|
||||
}
|
||||
@Get('/:id')
|
||||
@ResponseSchema(Track)
|
||||
@OnUndefined(TrackNotFoundError)
|
||||
getOne(@Param('id') id: number) {
|
||||
return this.trackRepository.findOne({ id: id });
|
||||
}
|
||||
|
||||
@Post()
|
||||
post(@Body({ validate: true }) track: CreateTrack) {
|
||||
return this.trackRepository.save(track);
|
||||
}
|
||||
@Post()
|
||||
@ResponseSchema(Track)
|
||||
post(
|
||||
@Body({ validate: true })
|
||||
track: CreateTrack
|
||||
) {
|
||||
return this.trackRepository.save(track);
|
||||
}
|
||||
|
||||
@Put("/:id")
|
||||
put(@Param("id") id: number, @EntityFromBody() track: Track) {
|
||||
return this.trackRepository.update({ id: id }, track);
|
||||
}
|
||||
@Put('/:id')
|
||||
@ResponseSchema(Track)
|
||||
async put(@Param('id') id: number, @EntityFromBody() track: Track) {
|
||||
let oldTrack = await this.trackRepository.findOne({ id: id });
|
||||
|
||||
@Delete("/:id")
|
||||
remove(@Param("id") id: number) {
|
||||
return this.trackRepository.delete({ id: id });
|
||||
}
|
||||
if (!oldTrack) {
|
||||
throw new TrackNotFoundError();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue