Merge branch 'main' into feature/authed-routes
# Conflicts: # src/app.ts # src/controllers/TrackController.ts
This commit is contained in:
commit
e29c0cb7a1
@ -4,4 +4,5 @@ DB_HOST=bla
|
|||||||
DB_PORT=bla
|
DB_PORT=bla
|
||||||
DB_USER=bla
|
DB_USER=bla
|
||||||
DB_PASSWORD=bla
|
DB_PASSWORD=bla
|
||||||
DB_NAME=bla
|
DB_NAME=bla
|
||||||
|
NODE_ENV=production
|
29
src/app.ts
29
src/app.ts
@ -3,24 +3,23 @@ import * as dotenvSafe from "dotenv-safe";
|
|||||||
import { createExpressServer } from "routing-controllers";
|
import { createExpressServer } from "routing-controllers";
|
||||||
import consola from "consola";
|
import consola from "consola";
|
||||||
import loaders from "./loaders/index";
|
import loaders from "./loaders/index";
|
||||||
import authchecker from "./authchecker";
|
|
||||||
import { ErrorHandler } from './middlewares/ErrorHandler';
|
|
||||||
//
|
|
||||||
dotenvSafe.config();
|
dotenvSafe.config();
|
||||||
const PORT = process.env.APP_PORT || 4010;
|
const PORT = process.env.APP_PORT || 4010;
|
||||||
|
|
||||||
const app = createExpressServer({
|
const app = createExpressServer({
|
||||||
authorizationChecker: authchecker,
|
development: process.env.NODE_ENV === "production",
|
||||||
middlewares: [ErrorHandler],
|
cors: true,
|
||||||
development: false,
|
routePrefix: "/api",
|
||||||
controllers: [`${__dirname}/controllers/*.ts`],
|
controllers: [__dirname + "/controllers/*.ts"],
|
||||||
});
|
});
|
||||||
|
|
||||||
(async () => {
|
async function main() {
|
||||||
await loaders(app);
|
await loaders(app);
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
consola.success(
|
consola.success(
|
||||||
`⚡️[server]: Server is running at http://localhost:${PORT}`
|
`⚡️[server]: Server is running at http://localhost:${PORT}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
})();
|
}
|
||||||
|
main();
|
||||||
|
@ -1,64 +1,91 @@
|
|||||||
import {
|
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
|
||||||
JsonController,
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
Param,
|
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||||
Body,
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
Get,
|
import { Track } from '../models/Track';
|
||||||
Post,
|
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||||
Put,
|
|
||||||
Delete,
|
|
||||||
Authorized,
|
|
||||||
} from "routing-controllers";
|
|
||||||
import { getConnectionManager, Repository } from "typeorm";
|
|
||||||
import { EntityFromBody } from "typeorm-routing-controllers-extensions";
|
|
||||||
import { ResponseSchema } from "routing-controllers-openapi";
|
|
||||||
import { Track } from "../models/Track";
|
|
||||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from "class-validator";
|
|
||||||
|
|
||||||
class CreateTrack {
|
class CreateTrack {
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsPositive()
|
@IsPositive()
|
||||||
length: string;
|
length: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonController("/track")
|
export class TrackNotFoundError extends NotFoundError {
|
||||||
// @Authorized("TRACKS:read")
|
constructor() {
|
||||||
|
super('Track not found!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonController('/tracks')
|
||||||
export class TrackController {
|
export class TrackController {
|
||||||
private trackRepository: Repository<Track>;
|
private trackRepository: Repository<Track>;
|
||||||
|
|
||||||
constructor() {
|
/**
|
||||||
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
* Gets the repository of this controller's model/entity.
|
||||||
}
|
*/
|
||||||
|
constructor() {
|
||||||
|
this.trackRepository = getConnectionManager().get().getRepository(Track);
|
||||||
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@ResponseSchema(Track, { isArray: true })
|
@ResponseSchema(Track, { isArray: true })
|
||||||
getAll() {
|
@OpenAPI({description: "Lists all tracks."})
|
||||||
return this.trackRepository.find();
|
getAll() {
|
||||||
}
|
return this.trackRepository.find();
|
||||||
|
}
|
||||||
|
|
||||||
@Get("/:id")
|
@Get('/:id')
|
||||||
@ResponseSchema(Track)
|
@ResponseSchema(Track)
|
||||||
getOne(@Param("id") id: number) {
|
@OnUndefined(TrackNotFoundError)
|
||||||
return this.trackRepository.findOne({ id: id });
|
@OpenAPI({description: "Returns a track of a specified id (if it exists)"})
|
||||||
}
|
getOne(@Param('id') id: number) {
|
||||||
|
return this.trackRepository.findOne({ id: id });
|
||||||
|
}
|
||||||
|
|
||||||
@Authorized()
|
@Post()
|
||||||
@Post()
|
@ResponseSchema(Track)
|
||||||
post(@Body({ validate: true }) track: CreateTrack) {
|
@OpenAPI({description: "Create a new track object (id will be generated automagicly)."})
|
||||||
return this.trackRepository.save(track);
|
post(
|
||||||
}
|
@Body({ validate: true })
|
||||||
|
track: CreateTrack
|
||||||
|
) {
|
||||||
|
return this.trackRepository.save(track);
|
||||||
|
}
|
||||||
|
|
||||||
@Authorized("TRACKS:write")
|
@Put('/:id')
|
||||||
@Put("/:id")
|
@ResponseSchema(Track)
|
||||||
put(@Param("id") id: number, @EntityFromBody() track: Track) {
|
@OpenAPI({description: "Update a track object (id can't be changed)."})
|
||||||
return this.trackRepository.update({ id: id }, track);
|
async put(@Param('id') id: number, @EntityFromBody() track: Track) {
|
||||||
}
|
let oldTrack = await this.trackRepository.findOne({ id: id });
|
||||||
|
|
||||||
@Delete("/:id")
|
if (!oldTrack) {
|
||||||
remove(@Param("id") id: number) {
|
throw new TrackNotFoundError();
|
||||||
return this.trackRepository.delete({ id: id });
|
}
|
||||||
}
|
|
||||||
|
if(oldTrack.id != track.id){
|
||||||
|
throw new NotAcceptableError("The id's don't match!");
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.trackRepository.update(oldTrack, track);
|
||||||
|
return track;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('/:id')
|
||||||
|
@ResponseSchema(Track)
|
||||||
|
@OpenAPI({description: "Delete a specified track (if it exists)."})
|
||||||
|
async remove(@Param('id') id: number) {
|
||||||
|
let track = await this.trackRepository.findOne({ id: id });
|
||||||
|
|
||||||
|
if (!track) {
|
||||||
|
throw new TrackNotFoundError();
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.trackRepository.delete(track);
|
||||||
|
return track;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,6 @@ import bodyParser from 'body-parser';
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
|
|
||||||
export default async (app: Application) => {
|
export default async (app: Application) => {
|
||||||
app.get('/status', (req, res) => res.status(200).end());
|
|
||||||
app.enable('trust proxy');
|
app.enable('trust proxy');
|
||||||
|
|
||||||
app.use(cors());
|
|
||||||
// app.use(bodyParser.urlencoded({ extended: false }));
|
|
||||||
|
|
||||||
// more middlewares
|
|
||||||
return app;
|
return app;
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,9 @@ export default async (app: Application) => {
|
|||||||
});
|
});
|
||||||
const spec = routingControllersToSpec(
|
const spec = routingControllersToSpec(
|
||||||
storage,
|
storage,
|
||||||
{},
|
{
|
||||||
|
routePrefix: "/api"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
components: {
|
components: {
|
||||||
schemas,
|
schemas,
|
||||||
@ -27,11 +29,11 @@ export default async (app: Application) => {
|
|||||||
explorer: true,
|
explorer: true,
|
||||||
};
|
};
|
||||||
app.use(
|
app.use(
|
||||||
"/docs",
|
"/api/docs",
|
||||||
swaggerUiExpress.serve,
|
swaggerUiExpress.serve,
|
||||||
swaggerUiExpress.setup(spec, options)
|
swaggerUiExpress.setup(spec, options)
|
||||||
);
|
);
|
||||||
app.get(["/openapi.json", "/swagger.json"], (req, res) => {
|
app.get(["/api/openapi.json", "/api/swagger.json"], (req, res) => {
|
||||||
res.json(spec);
|
res.json(spec);
|
||||||
});
|
});
|
||||||
return app;
|
return app;
|
||||||
|
@ -7,6 +7,12 @@ import {
|
|||||||
IsString,
|
IsString,
|
||||||
} from "class-validator";
|
} from "class-validator";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @classdesc Defines a track of given length.
|
||||||
|
* @property {number} id - Autogenerated unique id
|
||||||
|
* @property {string} name - The track's name
|
||||||
|
* @property {number} lenth - The track's length in meters
|
||||||
|
*/
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Track {
|
export class Track {
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
@ -22,5 +28,5 @@ export class Track {
|
|||||||
@Column()
|
@Column()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsPositive()
|
@IsPositive()
|
||||||
length: string;
|
length: number;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user