Merge branch 'main' of https://git.odit.services/lfk/backend into main
This commit is contained in:
commit
15552eff54
@ -1,92 +1,91 @@
|
|||||||
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError, Authorized } from 'routing-controllers';
|
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError, Authorized } from 'routing-controllers';
|
||||||
import { getConnectionManager, Repository } from 'typeorm';
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
import { Track } from '../models/Track';
|
import { Track } from '../models/Track';
|
||||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||||
|
import {TrackIdChangeNotAllowedError, TrackNotFoundError} from "../errors/TrackErrors";
|
||||||
class CreateTrack {
|
|
||||||
@IsString()
|
class CreateTrack {
|
||||||
@IsNotEmpty()
|
@IsString()
|
||||||
name: string;
|
@IsNotEmpty()
|
||||||
|
name: string;
|
||||||
@IsInt()
|
|
||||||
@IsPositive()
|
@IsInt()
|
||||||
length: number;
|
@IsPositive()
|
||||||
}
|
length: number;
|
||||||
|
}
|
||||||
export class TrackNotFoundError extends NotFoundError {
|
|
||||||
constructor() {
|
@JsonController('/tracks')
|
||||||
super('Track not found!');
|
@Authorized("TRACKS:read")
|
||||||
}
|
export class TrackController {
|
||||||
}
|
private trackRepository: Repository<Track>;
|
||||||
|
|
||||||
@JsonController('/tracks')
|
/**
|
||||||
@Authorized("TRACKS:read")
|
* Gets the repository of this controller's model/entity.
|
||||||
export class TrackController {
|
*/
|
||||||
private trackRepository: Repository<Track>;
|
constructor() {
|
||||||
|
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
||||||
/**
|
}
|
||||||
* Gets the repository of this controller's model/entity.
|
|
||||||
*/
|
@Get()
|
||||||
constructor() {
|
@ResponseSchema(Track, { isArray: true })
|
||||||
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
@OpenAPI({ description: "Lists all tracks." })
|
||||||
}
|
getAll() {
|
||||||
|
return this.trackRepository.find();
|
||||||
@Get()
|
}
|
||||||
@ResponseSchema(Track, { isArray: true })
|
|
||||||
@OpenAPI({ description: "Lists all tracks." })
|
@Get('/:id')
|
||||||
getAll() {
|
@ResponseSchema(Track)
|
||||||
return this.trackRepository.find();
|
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||||
}
|
@OnUndefined(TrackNotFoundError)
|
||||||
|
@OpenAPI({ description: "Returns a track of a specified id (if it exists)" })
|
||||||
@Get('/:id')
|
getOne(@Param('id') id: number) {
|
||||||
@ResponseSchema(Track)
|
return this.trackRepository.findOne({ id: id });
|
||||||
@OnUndefined(TrackNotFoundError)
|
}
|
||||||
@OpenAPI({ description: "Returns a track of a specified id (if it exists)" })
|
|
||||||
getOne(@Param('id') id: number) {
|
@Post()
|
||||||
return this.trackRepository.findOne({ id: id });
|
@ResponseSchema(Track)
|
||||||
}
|
@OpenAPI({ description: "Create a new track object (id will be generated automagicly)." })
|
||||||
|
post(
|
||||||
@Post()
|
@Body({ validate: true })
|
||||||
@ResponseSchema(Track)
|
track: CreateTrack
|
||||||
@OpenAPI({ description: "Create a new track object (id will be generated automagicly)." })
|
) {
|
||||||
post(
|
return this.trackRepository.save(track);
|
||||||
@Body({ validate: true })
|
}
|
||||||
track: CreateTrack
|
|
||||||
) {
|
@Put('/:id')
|
||||||
return this.trackRepository.save(track);
|
@ResponseSchema(Track)
|
||||||
}
|
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||||
|
@ResponseSchema(TrackIdChangeNotAllowedError, {statusCode: 406})
|
||||||
@Put('/:id')
|
@OpenAPI({description: "Update a track object (id can't be changed)."})
|
||||||
@ResponseSchema(Track)
|
async put(@Param('id') id: number, @EntityFromBody() track: Track) {
|
||||||
@OpenAPI({ description: "Update a track object (id can't be changed)." })
|
let oldTrack = await this.trackRepository.findOne({ id: id });
|
||||||
async put(@Param('id') id: number, @EntityFromBody() track: Track) {
|
|
||||||
let oldTrack = await this.trackRepository.findOne({ id: id });
|
if (!oldTrack) {
|
||||||
|
throw new TrackNotFoundError();
|
||||||
if (!oldTrack) {
|
}
|
||||||
throw new TrackNotFoundError();
|
|
||||||
}
|
if(oldTrack.id != track.id){
|
||||||
|
throw new TrackIdChangeNotAllowedError();
|
||||||
if (oldTrack.id != track.id) {
|
}
|
||||||
throw new NotAcceptableError("The id's don't match!");
|
|
||||||
}
|
await this.trackRepository.update(oldTrack, track);
|
||||||
|
return track;
|
||||||
await this.trackRepository.update(oldTrack, track);
|
}
|
||||||
return track;
|
|
||||||
}
|
@Delete('/:id')
|
||||||
|
@ResponseSchema(Track)
|
||||||
@Delete('/:id')
|
@ResponseSchema(TrackNotFoundError, {statusCode: 404})
|
||||||
@ResponseSchema(Track)
|
@OpenAPI({description: "Delete a specified track (if it exists)."})
|
||||||
@OpenAPI({ description: "Delete a specified track (if it exists)." })
|
async remove(@Param('id') id: number) {
|
||||||
async remove(@Param('id') id: number) {
|
let track = await this.trackRepository.findOne({ id: id });
|
||||||
let track = await this.trackRepository.findOne({ id: id });
|
|
||||||
|
if (!track) {
|
||||||
if (!track) {
|
throw new TrackNotFoundError();
|
||||||
throw new TrackNotFoundError();
|
}
|
||||||
}
|
|
||||||
|
await this.trackRepository.delete(track);
|
||||||
await this.trackRepository.delete(track);
|
return track;
|
||||||
return track;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
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