Now using typeorm-routing-controllers-extensions for cleaner controllers
ref #4
This commit is contained in:
@@ -7,29 +7,34 @@ import {
|
||||
Put,
|
||||
Delete,
|
||||
} from "routing-controllers";
|
||||
import { OpenAPI } from "routing-controllers-openapi";
|
||||
import { getConnection, Repository } from "typeorm";
|
||||
import { getConnectionManager, Repository } from "typeorm";
|
||||
import {
|
||||
EntityFromParam,
|
||||
EntityFromBody,
|
||||
} from "typeorm-routing-controllers-extensions";
|
||||
import { Track } from "../models/Track";
|
||||
|
||||
@JsonController("/track")
|
||||
export class TrackController {
|
||||
private repo(): Repository<Track> {
|
||||
return getConnection().getRepository(Track);
|
||||
private trackRepository: Repository<Track>;
|
||||
|
||||
constructor() {
|
||||
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
||||
}
|
||||
|
||||
@Get("/track")
|
||||
async getAll() {
|
||||
return await this.repo().find();
|
||||
return await this.trackRepository.find();
|
||||
}
|
||||
|
||||
@Get("/track/:id")
|
||||
async getOne(@Param("id") id: number) {
|
||||
return await this.repo().findOne({ id: id });
|
||||
async getOne(@EntityFromParam("id") track: Track) {
|
||||
return track;
|
||||
}
|
||||
|
||||
@Post("/track")
|
||||
post(@Body() user: any) {
|
||||
return "Saving user...";
|
||||
post(@EntityFromBody() track: Track) {
|
||||
return this.trackRepository.save(track);
|
||||
}
|
||||
|
||||
@Put("/track/:id")
|
||||
@@ -38,7 +43,7 @@ export class TrackController {
|
||||
}
|
||||
|
||||
@Delete("/track/:id")
|
||||
remove(@Param("id") id: number) {
|
||||
return "Removing user...";
|
||||
remove(@EntityFromParam("id") track: Track) {
|
||||
return this.trackRepository.delete(track);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user