Merge branch 'main' of https://git.odit.services/lfk/backend into main
This commit is contained in:
commit
15552eff54
@ -4,6 +4,7 @@ import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { Track } from '../models/Track';
|
||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||
import {TrackIdChangeNotAllowedError, TrackNotFoundError} from "../errors/TrackErrors";
|
||||
|
||||
class CreateTrack {
|
||||
@IsString()
|
||||
@ -15,12 +16,6 @@ class CreateTrack {
|
||||
length: number;
|
||||
}
|
||||
|
||||
export class TrackNotFoundError extends NotFoundError {
|
||||
constructor() {
|
||||
super('Track not found!');
|
||||
}
|
||||
}
|
||||
|
||||
@JsonController('/tracks')
|
||||
@Authorized("TRACKS:read")
|
||||
export class TrackController {
|
||||
@ -42,6 +37,7 @@ export class TrackController {
|
||||
|
||||
@Get('/:id')
|
||||
@ResponseSchema(Track)
|
||||
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||
@OnUndefined(TrackNotFoundError)
|
||||
@OpenAPI({ description: "Returns a track of a specified id (if it exists)" })
|
||||
getOne(@Param('id') id: number) {
|
||||
@ -60,7 +56,9 @@ export class TrackController {
|
||||
|
||||
@Put('/:id')
|
||||
@ResponseSchema(Track)
|
||||
@OpenAPI({ description: "Update a track object (id can't be changed)." })
|
||||
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||
@ResponseSchema(TrackIdChangeNotAllowedError, {statusCode: 406})
|
||||
@OpenAPI({description: "Update a track object (id can't be changed)."})
|
||||
async put(@Param('id') id: number, @EntityFromBody() track: Track) {
|
||||
let oldTrack = await this.trackRepository.findOne({ id: id });
|
||||
|
||||
@ -68,8 +66,8 @@ export class TrackController {
|
||||
throw new TrackNotFoundError();
|
||||
}
|
||||
|
||||
if (oldTrack.id != track.id) {
|
||||
throw new NotAcceptableError("The id's don't match!");
|
||||
if(oldTrack.id != track.id){
|
||||
throw new TrackIdChangeNotAllowedError();
|
||||
}
|
||||
|
||||
await this.trackRepository.update(oldTrack, track);
|
||||
@ -78,7 +76,8 @@ export class TrackController {
|
||||
|
||||
@Delete('/:id')
|
||||
@ResponseSchema(Track)
|
||||
@OpenAPI({ description: "Delete a specified track (if it exists)." })
|
||||
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||
@OpenAPI({description: "Delete a specified track (if it exists)."})
|
||||
async remove(@Param('id') id: number) {
|
||||
let track = await this.trackRepository.findOne({ id: id });
|
||||
|
||||
|
18
src/errors/TrackErrors.ts
Normal file
18
src/errors/TrackErrors.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
|
||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||
|
||||
export class TrackNotFoundError extends NotFoundError {
|
||||
@IsString()
|
||||
name = "TrackNotFoundError"
|
||||
|
||||
@IsString()
|
||||
message = "Track not found!"
|
||||
}
|
||||
|
||||
export class TrackIdChangeNotAllowedError extends NotAcceptableError {
|
||||
@IsString()
|
||||
name = "TrackIdChangeNotAllowed"
|
||||
|
||||
@IsString()
|
||||
message = "The id's don't match!! \n And if you wanted to change a track's id: This isn't allowed"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user