Now using typeorm-routing-controllers-extensions for cleaner controllers

ref #4
This commit is contained in:
Nicolai Ort 2020-11-26 13:06:10 +01:00
parent 7b948f0380
commit 2c29fe29e8
2 changed files with 82 additions and 76 deletions

View File

@ -37,15 +37,16 @@
"routing-controllers": "^0.9.0-alpha.6",
"routing-controllers-openapi": "^2.1.0",
"swagger-ui-express": "^4.1.5",
"typeorm": "^0.2.29"
"typeorm": "^0.2.29",
"typeorm-routing-controllers-extensions": "^0.2.0"
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/dotenv-safe": "^8.1.1",
"@types/express": "^4.17.9",
"@types/jsonwebtoken": "^8.5.0",
"@types/node": "^14.14.9",
"@types/multer": "^1.4.4",
"@types/node": "^14.14.9",
"@types/swagger-ui-express": "^4.1.2",
"dotenv-safe": "^8.2.0",
"husky": "^4.3.0",

View File

@ -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);
}
}