TrackController now also deletes based on a entityfromparam

ref #13
This commit is contained in:
Nicolai Ort 2020-12-05 12:21:47 +01:00
parent 179add80f4
commit 0e3cf07b91

View File

@ -1,7 +1,7 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions';
import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors"; import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors";
import { CreateTrack } from '../models/creation/CreateTrack'; import { CreateTrack } from '../models/creation/CreateTrack';
import { Track } from '../models/entities/Track'; import { Track } from '../models/entities/Track';
@ -74,14 +74,10 @@ export class TrackController {
@ResponseSchema(ResponseTrack) @ResponseSchema(ResponseTrack)
@ResponseSchema(TrackNotFoundError, { statusCode: 404 }) @ResponseSchema(TrackNotFoundError, { statusCode: 404 })
@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(@EntityFromParam('id') track: Track) {
let track = await this.trackRepository.findOne({ id: id }); if (!track) { throw new TrackNotFoundError(); }
if (!track) {
throw new TrackNotFoundError();
}
await this.trackRepository.delete(track); await this.trackRepository.delete(track);
return new ResponseTrack(track); return new ResponseTrack(track);
} }
} }