Now using typeorm-routing-controllers-extensions for cleaner controllers
ref #4
This commit is contained in:
parent
7b948f0380
commit
2c29fe29e8
@ -37,15 +37,16 @@
|
|||||||
"routing-controllers": "^0.9.0-alpha.6",
|
"routing-controllers": "^0.9.0-alpha.6",
|
||||||
"routing-controllers-openapi": "^2.1.0",
|
"routing-controllers-openapi": "^2.1.0",
|
||||||
"swagger-ui-express": "^4.1.5",
|
"swagger-ui-express": "^4.1.5",
|
||||||
"typeorm": "^0.2.29"
|
"typeorm": "^0.2.29",
|
||||||
|
"typeorm-routing-controllers-extensions": "^0.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/cors": "^2.8.8",
|
"@types/cors": "^2.8.8",
|
||||||
"@types/dotenv-safe": "^8.1.1",
|
"@types/dotenv-safe": "^8.1.1",
|
||||||
"@types/express": "^4.17.9",
|
"@types/express": "^4.17.9",
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
"@types/node": "^14.14.9",
|
|
||||||
"@types/multer": "^1.4.4",
|
"@types/multer": "^1.4.4",
|
||||||
|
"@types/node": "^14.14.9",
|
||||||
"@types/swagger-ui-express": "^4.1.2",
|
"@types/swagger-ui-express": "^4.1.2",
|
||||||
"dotenv-safe": "^8.2.0",
|
"dotenv-safe": "^8.2.0",
|
||||||
"husky": "^4.3.0",
|
"husky": "^4.3.0",
|
||||||
|
@ -7,29 +7,34 @@ import {
|
|||||||
Put,
|
Put,
|
||||||
Delete,
|
Delete,
|
||||||
} from "routing-controllers";
|
} from "routing-controllers";
|
||||||
import { OpenAPI } from "routing-controllers-openapi";
|
import { getConnectionManager, Repository } from "typeorm";
|
||||||
import { getConnection, Repository } from "typeorm";
|
import {
|
||||||
|
EntityFromParam,
|
||||||
|
EntityFromBody,
|
||||||
|
} from "typeorm-routing-controllers-extensions";
|
||||||
import { Track } from "../models/Track";
|
import { Track } from "../models/Track";
|
||||||
|
|
||||||
@JsonController("/track")
|
@JsonController("/track")
|
||||||
export class TrackController {
|
export class TrackController {
|
||||||
private repo(): Repository<Track> {
|
private trackRepository: Repository<Track>;
|
||||||
return getConnection().getRepository(Track);
|
|
||||||
|
constructor() {
|
||||||
|
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/track")
|
@Get("/track")
|
||||||
async getAll() {
|
async getAll() {
|
||||||
return await this.repo().find();
|
return await this.trackRepository.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/track/:id")
|
@Get("/track/:id")
|
||||||
async getOne(@Param("id") id: number) {
|
async getOne(@EntityFromParam("id") track: Track) {
|
||||||
return await this.repo().findOne({ id: id });
|
return track;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("/track")
|
@Post("/track")
|
||||||
post(@Body() user: any) {
|
post(@EntityFromBody() track: Track) {
|
||||||
return "Saving user...";
|
return this.trackRepository.save(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("/track/:id")
|
@Put("/track/:id")
|
||||||
@ -38,7 +43,7 @@ export class TrackController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Delete("/track/:id")
|
@Delete("/track/:id")
|
||||||
remove(@Param("id") id: number) {
|
remove(@EntityFromParam("id") track: Track) {
|
||||||
return "Removing user...";
|
return this.trackRepository.delete(track);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user