Created
March 20, 2018 14:37
-
-
Save enko/26eff8ca7e7528a708d384a6eb39f597 to your computer and use it in GitHub Desktop.
Basic API example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Connection } from "typeorm"; | |
export abstract class BaseApiController<T> { | |
protected classType: new () => T; | |
public static connection: Connection; | |
constructor(classType: new () => T) { | |
this.classType = classType; | |
} | |
public async find(): Promise<T[]> { | |
return await BaseApiController.connection.manager.find(this.classType); | |
} | |
public async save(entity: T): Promise<T> { | |
console.dir(entity); | |
return await BaseApiController.connection.manager.save(entity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
BaseApiController, | |
} from '../controllers'; | |
import { | |
User, | |
} from '../entity'; | |
import { | |
JsonController, | |
Get, | |
Post, | |
Body, | |
} from 'routing-controllers'; | |
@JsonController('/user') | |
export class UserController extends BaseApiController<User> { | |
constructor() { | |
super(User) | |
} | |
@Get() | |
public async find(): Promise<User[]> { | |
return super.find(); | |
} | |
@Post() | |
public async save( | |
@Body() user: User, | |
): Promise<User> { | |
return super.save(user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment