Skip to content

Commit

Permalink
Added API endpoints for user module
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaicaaan committed Dec 17, 2017
1 parent 78ecd53 commit c1178b1
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-starter-ts",
"version": "1.0.0",
"version": "0.0",
"description": "This repository gives the developer an Express Starter Typescript to kick off there development fast",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 14 additions & 17 deletions src/app.ts
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}`);
});
3 changes: 1 addition & 2 deletions src/controllers/index.ts
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';
8 changes: 1 addition & 7 deletions src/controllers/intro/intro.controller.ts
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!';
}
}
22 changes: 22 additions & 0 deletions src/controllers/user/deleteUser.controller.ts
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`;
}
}
19 changes: 19 additions & 0 deletions src/controllers/user/getUser.controller.ts
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;
}
}
3 changes: 3 additions & 0 deletions src/controllers/user/index.ts
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';
25 changes: 25 additions & 0 deletions src/controllers/user/postUser.controller.ts
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;
}
}
14 changes: 0 additions & 14 deletions src/controllers/user/user.controller.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User extends BaseEntity {
export class User {

@PrimaryGeneratedColumn()
id: number;
Expand All @@ -12,7 +12,7 @@ export class User extends BaseEntity {
@Column('text')
password: string;

@Column('boolean')
@Column('boolean', { default: true })
enabled: boolean;

@Column('timestamp')
Expand Down
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user.service';
21 changes: 21 additions & 0 deletions src/services/user.service.ts
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);
}
}

0 comments on commit c1178b1

Please sign in to comment.