Skip to content
This repository was archived by the owner on Nov 11, 2018. It is now read-only.

Commit 5229d86

Browse files
author
Gery Hirschfeld
committed
feat(services): Add services
1 parent 7ccc911 commit 5229d86

27 files changed

+416
-143
lines changed

src/App.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Core elements to get the server started
22
import {
3-
DB,
43
Environment,
54
Server,
65
Logger,
@@ -18,8 +17,11 @@ import * as GraphQLHTTP from 'express-graphql';
1817
import { Exception } from './exceptions';
1918
import { Schema } from './schemas';
2019
import { RootValue } from './RootValue';
21-
import { Context, RepositoriesContext, DataLoadersContext } from './context';
22-
import { AuthorRepository, BookRepository } from './repositories';
20+
import {
21+
Context,
22+
DataLoadersContext,
23+
ServicesContext
24+
} from './context';
2325
import { oauth } from './middlewares';
2426

2527

@@ -29,8 +31,6 @@ export class App {
2931

3032
private log = Logger('app:main');
3133
private express: express.Application;
32-
private repositoriesContext: RepositoriesContext;
33-
private dataLoadersContext: DataLoadersContext;
3434

3535
static getInstance(): App {
3636
if (!App.instance) {
@@ -41,8 +41,6 @@ export class App {
4141

4242
constructor() {
4343
this.express = Server.init();
44-
this.buildRepositoriesContext();
45-
this.buildDataLoadersContext();
4644
}
4745

4846
public main(): void {
@@ -73,9 +71,13 @@ export class App {
7371
this.log.debug('Setup GraphQLHTTP');
7472
// Creates a GraphQLHTTP per request
7573
GraphQLHTTP({
76-
schema: Schema.getInstance().get(),
74+
schema: Schema.getInstance(),
7775
rootValue: new RootValue(),
78-
context: new Context(req, res, this.repositoriesContext, this.dataLoadersContext),
76+
context: new Context(
77+
req, res,
78+
DataLoadersContext.getInstance(),
79+
ServicesContext.getInstance()
80+
),
7981
graphiql: Environment.getConfig().server.graphiql,
8082
formatError: exception => ({
8183
key: Exception.getKey(exception.message),
@@ -91,16 +93,4 @@ export class App {
9193
this.log.debug('Server was started on environment %s', Environment.getName());
9294
}
9395

94-
private buildRepositoriesContext(): void {
95-
this.repositoriesContext = new RepositoriesContext()
96-
.setAuthorRepository(new AuthorRepository(DB))
97-
.setBookRepository(new BookRepository(DB));
98-
}
99-
100-
private buildDataLoadersContext(): void {
101-
this.dataLoadersContext = new DataLoadersContext()
102-
.setAuthorDataLoader(this.repositoriesContext.AuthorRepository)
103-
.setBookDataLoader(this.repositoriesContext.BookRepository);
104-
}
105-
10696
}

src/context/DataloadersContext.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ const log = Logger('app:context:DataLoadersContext');
88

99
export class DataLoadersContext {
1010

11+
static instance: DataLoadersContext;
12+
1113
private authorDataLaoder: DataLoader<number, any>;
1214
private bookDataLaoder: DataLoader<number, any>;
1315

16+
static getInstance(): DataLoadersContext {
17+
if (!DataLoadersContext.instance) {
18+
DataLoadersContext.instance = new DataLoadersContext();
19+
}
20+
return DataLoadersContext.instance;
21+
}
22+
1423
public get AuthorDataLoader(): DataLoader<number, any> {
1524
return this.authorDataLaoder;
1625
}
@@ -20,13 +29,13 @@ export class DataLoadersContext {
2029
}
2130

2231
public setAuthorDataLoader(authorRepository: AuthorRepository): DataLoadersContext {
23-
this.authorDataLaoder = new DataLoader((ids: number[]) => authorRepository.findAuthorsByIds(ids));
32+
this.authorDataLaoder = new DataLoader((ids: number[]) => authorRepository.findByIds(ids));
2433
log.debug('setAuthorDataLoader');
2534
return this;
2635
}
2736

2837
public setBookDataLoader(bookRepository: BookRepository): DataLoadersContext {
29-
this.bookDataLaoder = new DataLoader((ids: number[]) => bookRepository.findBooksByIds(ids));
38+
this.bookDataLaoder = new DataLoader((ids: number[]) => bookRepository.findByIds(ids));
3039
log.debug('setBookDataLoader');
3140
return this;
3241
}

src/context/RepositoriesContext.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/context/ServicesContext.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
AuthorService,
3+
BookService
4+
} from '../services';
5+
6+
import { Logger } from '../core/logger';
7+
const log = Logger('app:context:ServicesContext');
8+
9+
10+
export class ServicesContext {
11+
12+
static instance: ServicesContext;
13+
14+
private authorService: AuthorService;
15+
private bookService: BookService;
16+
17+
static getInstance(): ServicesContext {
18+
if (!ServicesContext.instance) {
19+
ServicesContext.instance = new ServicesContext();
20+
}
21+
return ServicesContext.instance;
22+
}
23+
24+
public get AuthorService(): AuthorService {
25+
return this.authorService;
26+
}
27+
28+
public get BookService(): BookService {
29+
return this.bookService;
30+
}
31+
32+
public setAuthorService(authorService: AuthorService): ServicesContext {
33+
this.authorService = authorService;
34+
log.debug('setAuthorService');
35+
return this;
36+
}
37+
38+
public setBookService(bookService: BookService): ServicesContext {
39+
this.bookService = bookService;
40+
log.debug('setBookService');
41+
return this;
42+
}
43+
44+
}

src/context/context.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Express from 'express';
22

3-
import { RepositoriesContext } from './RepositoriesContext';
43
import { DataLoadersContext } from './DataloadersContext';
4+
import { ServicesContext } from './ServicesContext';
55

66

77
export class Context<A> {
@@ -16,8 +16,8 @@ export class Context<A> {
1616
constructor(
1717
private request: Express.Request,
1818
private repsonse: Express.Response,
19-
private repositories: RepositoriesContext,
20-
private dataLoaders: DataLoadersContext
19+
private dataLoaders: DataLoadersContext,
20+
private services: ServicesContext
2121
) { }
2222

2323
public get Args(): A {
@@ -32,14 +32,14 @@ export class Context<A> {
3232
return this.request;
3333
}
3434

35-
public get Repositories(): RepositoriesContext {
36-
return this.repositories;
37-
}
38-
3935
public get DataLoaders(): DataLoadersContext {
4036
return this.dataLoaders;
4137
}
4238

39+
public get Services(): ServicesContext {
40+
return this.services;
41+
}
42+
4343
public getLanguage(): string[] {
4444
return this.request.acceptsLanguages();
4545
}

src/context/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
11
export * from './Context';
2-
export * from './RepositoriesContext';
32
export * from './DataloadersContext';
3+
export * from './ServicesContext';
4+
5+
import { DB } from '../core/Database';
6+
import {
7+
AuthorRepository,
8+
BookRepository
9+
} from '../repositories';
10+
11+
12+
/**
13+
* Dataloaders
14+
*/
15+
import { DataLoadersContext } from './DataloadersContext';
16+
DataLoadersContext.getInstance()
17+
.setAuthorDataLoader(new AuthorRepository(DB))
18+
.setBookDataLoader(new BookRepository(DB));
19+
20+
21+
/**
22+
* Services
23+
*/
24+
import { ServicesContext } from './ServicesContext';
25+
26+
import {
27+
BookService,
28+
AuthorService
29+
} from '../services';
30+
31+
ServicesContext.getInstance()
32+
.setBookService(new BookService(new BookRepository(DB)))
33+
.setAuthorService(new AuthorService(new AuthorRepository(DB)));

src/core/Utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { NotFoundException } from '../exceptions';
33

44
export class Utils {
55

6+
static isEmpty<T>(list: T[]): boolean {
7+
return !Utils.hasResults(list);
8+
};
9+
610
static hasResults<T>(list: T[]): boolean {
711
return (typeof list === 'object' && !!list && list.length) ? list.length > 0 : false;
812
};

src/core/anotations/service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// import { DI } from '../DI';
2+
3+
// export const service = <T extends { new (...args: any[]): {} }>(constructor: T) => {
4+
5+
// console.log(constructor.name);
6+
// console.log(constructor);
7+
8+
// DI.getInstance().register(constructor.name, constructor);
9+
10+
// return class extends constructor {
11+
12+
// };
13+
// };

src/exceptions/ValidationException.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Exception } from './Exception';
33

44
export class ValidationException extends Exception {
55

6-
constructor(message: string) {
6+
constructor(message?: string) {
77
super(message);
88
this.name = 'ValidationException';
99
this.key = 'e_validation';

0 commit comments

Comments
 (0)