11import * as Bookshelf from 'bookshelf' ;
2+ import { injectable , inject , named } from 'inversify' ;
3+ import { Types } from '../../constants/Types' ;
4+ import { Model } from '../../constants/Targets' ;
25import { User } from '../models/User' ;
36import { DatabaseException } from '../exceptions/DatabaseException' ;
47import { NotFoundException } from '../exceptions/NotFoundException' ;
@@ -9,8 +12,13 @@ import { NotFoundException } from '../exceptions/NotFoundException';
912 * @export
1013 * @class UserRepository
1114 */
15+ @injectable ( )
1216export class UserRepository {
1317
18+ constructor (
19+ @inject ( Types . Model ) @named ( Model . User ) public UserModel : typeof User
20+ ) { }
21+
1422 /**
1523 * Retrieves all user data out of the database
1624 *
@@ -19,8 +27,8 @@ export class UserRepository {
1927 *
2028 * @memberof UserRepository
2129 */
22- public static async findAll ( ) : Promise < Bookshelf . Collection < User > > {
23- const users = await User . fetchAll ( ) ;
30+ public async findAll ( ) : Promise < Bookshelf . Collection < User > > {
31+ const users = await this . UserModel . fetchAll ( ) ;
2432 return < Bookshelf . Collection < User > > users ;
2533 }
2634
@@ -31,8 +39,8 @@ export class UserRepository {
3139 * @param {number } id of the user
3240 * @returns {Promise<User> }
3341 */
34- public static async findOne ( id : number ) : Promise < User > {
35- return User . fetchById ( id ) ;
42+ public async findOne ( id : number ) : Promise < User > {
43+ return this . UserModel . fetchById ( id ) ;
3644 }
3745
3846 /**
@@ -42,8 +50,8 @@ export class UserRepository {
4250 * @param {number } id of the user
4351 * @returns {Promise<User> }
4452 */
45- public static async findByUserId ( userId : string ) : Promise < User > {
46- return User . fetchByUserId ( userId ) ;
53+ public async findByUserId ( userId : string ) : Promise < User > {
54+ return this . UserModel . fetchByUserId ( userId ) ;
4755 }
4856
4957 /**
@@ -54,11 +62,11 @@ export class UserRepository {
5462 * @param {* } data is the new user
5563 * @returns {Promise<User> }
5664 */
57- public static async create ( data : any ) : Promise < User > {
58- const user = User . forge < User > ( data ) ;
65+ public async create ( data : any ) : Promise < User > {
66+ const user = this . UserModel . forge < User > ( data ) ;
5967 try {
6068 const createdUser = await user . save ( ) ;
61- return await User . fetchById ( createdUser . id ) ;
69+ return await this . UserModel . fetchById ( createdUser . id ) ;
6270 } catch ( error ) {
6371 throw new DatabaseException ( 'Could not create the user!' , error ) ;
6472 }
@@ -72,11 +80,11 @@ export class UserRepository {
7280 * @param {* } data
7381 * @returns {Promise<User> }
7482 */
75- public static async update ( id : number , data : any ) : Promise < User > {
76- const user = User . forge < User > ( { id : id } ) ;
83+ public async update ( id : number , data : any ) : Promise < User > {
84+ const user = this . UserModel . forge < User > ( { id : id } ) ;
7785 try {
7886 const updatedUser = await user . save ( data , { patch : true } ) ;
79- return await User . fetchById ( updatedUser . id ) ;
87+ return await this . UserModel . fetchById ( updatedUser . id ) ;
8088
8189 } catch ( error ) {
8290 throw new DatabaseException ( 'Could not update the user!' , error ) ;
@@ -91,8 +99,8 @@ export class UserRepository {
9199 * @param {number } id
92100 * @returns {Promise<void> }
93101 */
94- public static async destroy ( id : number ) : Promise < void > {
95- let user = User . forge < User > ( { id : id } ) ;
102+ public async destroy ( id : number ) : Promise < void > {
103+ let user = this . UserModel . forge < User > ( { id : id } ) ;
96104 try {
97105 user = await user . fetch ( { require : true } ) ;
98106 } catch ( error ) {
0 commit comments