-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
111 additions
and
44 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
import { createExpressServer, useContainer } from 'routing-controllers'; | ||
import { Container } from 'typedi'; | ||
import { IntroController, UserController } from './controllers/index'; | ||
|
||
import 'reflect-metadata'; | ||
import { createConnection } from 'typeorm'; | ||
import { createExpressServer, useContainer as routingUseContainer } from 'routing-controllers'; | ||
import { Container } from 'typedi'; | ||
import { createConnection, useContainer as ormUseContainer } from 'typeorm'; | ||
import { DeleteUserController, GetUserController, PostUserController } from './controllers/'; | ||
import { User } from './entities/user'; | ||
|
||
useContainer(Container); | ||
// let's tell orm and the routing controller to use the typeDI | ||
// https://github.com/typestack/typedi/issues/4 | ||
|
||
ormUseContainer(Container); | ||
routingUseContainer(Container); | ||
|
||
const app = createExpressServer({ | ||
controllers: [ | ||
IntroController, | ||
UserController | ||
GetUserController, | ||
PostUserController, | ||
DeleteUserController | ||
] | ||
}); | ||
|
||
// tslint:disable-next-line:arrow-parens | ||
createConnection().then(async (connection) => { | ||
const user = new User(); | ||
user.email = 'test@email.com'; | ||
user.password = 'password123'; | ||
user.enabled = true; | ||
user.created = new Date(); | ||
|
||
let userRepository = connection.getRepository(User); | ||
await userRepository.save(user); | ||
console.log('connection has been setup!'); | ||
}); | ||
|
||
const port = process.env.PORT || 1111; | ||
|
||
app.listen(port, () => { | ||
console.log(`The server is starting at http://localhost:${port}`); | ||
console.log(`The server is listening at http://localhost:${port}`); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './intro/intro.controller'; | ||
export * from './user/user.controller'; | ||
export * from './user'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,12 @@ | ||
import { Get, JsonController } from 'routing-controllers'; | ||
import { Service } from 'typedi'; | ||
import { User } from '../../entities/user'; | ||
|
||
@Service() | ||
@JsonController() | ||
export class IntroController { | ||
|
||
constructor() { | ||
// do nothing | ||
} | ||
|
||
@Get('/') | ||
async execute() { | ||
const users = await User.find(); | ||
return users; | ||
return 'Hello World!'; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { JsonController, Param, Post } from 'routing-controllers'; | ||
import { Service } from 'typedi'; | ||
import { OrmRepository } from 'typeorm-typedi-extensions'; | ||
import { UserService } from '../../services/'; | ||
|
||
@Service() | ||
@JsonController('/users/:id') | ||
export class DeleteUserController { | ||
|
||
constructor( | ||
@OrmRepository() private userService: UserService | ||
) {} | ||
|
||
@Post() | ||
async execute( | ||
@Param('id') userId: number | ||
) { | ||
// void | ||
await this.userService.deleteUserById(userId); | ||
return `User with id ${userId} has been deleted`; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Get, JsonController } from 'routing-controllers'; | ||
import { Service } from 'typedi'; | ||
import { OrmRepository } from 'typeorm-typedi-extensions'; | ||
import { UserService } from '../../services/'; | ||
|
||
@Service() | ||
@JsonController('/users') | ||
export class GetUserController { | ||
|
||
constructor( | ||
@OrmRepository() private userService: UserService | ||
) {} | ||
|
||
@Get() | ||
async execute() { | ||
const users = await this.userService.getUsers(); | ||
return users; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './getUser.controller'; | ||
export * from './postUser.controller'; | ||
export * from './deleteUser.controller'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { JsonController, Post } from 'routing-controllers'; | ||
import { Service } from 'typedi'; | ||
import { OrmRepository } from 'typeorm-typedi-extensions'; | ||
import { User } from '../../entities/user'; | ||
import { UserService } from '../../services/'; | ||
|
||
@Service() | ||
@JsonController('/users') | ||
export class PostUserController { | ||
|
||
constructor( | ||
@OrmRepository() private userService: UserService | ||
) {} | ||
|
||
@Post() | ||
async execute() { | ||
const user = new User(); | ||
user.email = 'jsantos@isbx.com'; | ||
user.password = 'password123'; | ||
user.created = new Date(); | ||
|
||
const createdUser = await this.userService.createUser(user); | ||
return createdUser; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './user.service'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Service } from 'typedi'; | ||
import { EntityRepository, Repository } from 'typeorm'; | ||
import { OrmRepository } from 'typeorm-typedi-extensions'; | ||
import { User } from '../entities/user'; | ||
|
||
@Service() | ||
@EntityRepository(User) | ||
export class UserService extends Repository<User> { | ||
|
||
public createUser(user: User): Promise<User> { | ||
return this.save(user); | ||
} | ||
|
||
public getUsers(): Promise<User[]> { | ||
return this.find(); | ||
} | ||
|
||
public deleteUserById(id?: number, user?: User): Promise<void> { | ||
return this.deleteById(id || user.id); | ||
} | ||
} |