-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from damascus-mx/Tests/Ts
PR: Use Typescript
- Loading branch information
Showing
16 changed files
with
1,125 additions
and
1,487 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
Large diffs are not rendered by default.
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Required libs | ||
import express from 'express'; | ||
import compression from 'compression'; | ||
import * as bodyParser from 'body-parser'; | ||
import morgan from 'morgan'; | ||
import * as rfs from 'rotating-file-stream'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
// Custom libs | ||
|
||
// Const | ||
const app = express(); | ||
const API_ROUTE = '/api/v1'; | ||
|
||
// Routes import | ||
import { UserRoutes } from '../routes/user.routes'; | ||
|
||
// Gzip compression | ||
app.use(compression()); | ||
|
||
// Parser to JSON | ||
app.use(bodyParser.urlencoded({extended: false})); | ||
app.use(bodyParser.json()); | ||
|
||
// Config logger | ||
const logDirectory = path.join(__dirname, 'log'); | ||
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory); | ||
const accessLogStream = rfs.default( | ||
'access.log', { | ||
interval: '5d', | ||
path: logDirectory | ||
} | ||
); | ||
app.use(morgan("combined", { stream: accessLogStream })); | ||
|
||
// CORS | ||
// TODO - Change public policy to AWS Cloudfront/VPC | ||
app.use((req, res, next) => { | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-Width, Content-Type, Accept, Access-Control-Allow-Request-Method'); | ||
res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT, OPTIONS'); | ||
res.header('Allow', 'GET, POST, DELETE, PUT, OPTIONS'); | ||
|
||
next(); | ||
}); | ||
|
||
// Routes | ||
app.use(API_ROUTE, UserRoutes); | ||
|
||
export default app; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Modules | ||
import cluster from 'cluster'; | ||
import { Pool } from 'pg'; | ||
import dotenv from 'dotenv'; | ||
// Custom Modules | ||
import app from './app'; | ||
import { PoolInstance } from '../infrastructure/pool'; | ||
|
||
// Start dotenv | ||
dotenv.config(); | ||
|
||
// Start pool | ||
const pool: Pool = PoolInstance.getInstance(); | ||
|
||
const PORT = process.env.PORT || 5000; | ||
|
||
// Start API cluster | ||
if ( cluster.isMaster ) { | ||
const CPU_COUNT = require('os').cpus().length; | ||
for (let i = 0; i < CPU_COUNT; i += 1) { | ||
cluster.fork(); | ||
} | ||
|
||
cluster.on('exit', function(worker) { | ||
console.log(`Cluster ${worker.id} died.`); | ||
cluster.fork(); | ||
}); | ||
} else { | ||
// pool.query('SELECT NOW()').then(res => console.log(res.rows[0])).catch(e => console.log(e.message)); | ||
app.listen( PORT, () => { console.log(`Server running on port ${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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const GENERIC_ERROR = '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Repository } from "../core/repository"; | ||
import { UserModel } from "../domain/models/user.model"; | ||
import { UserRepository } from "../infrastructure/repositories/user.repository"; | ||
import { Pool } from "pg"; | ||
import { PoolInstance } from "../infrastructure/pool"; | ||
import { GENERIC_ERROR } from "../common/config/app.config"; | ||
|
||
export class UserController { | ||
private _Pool: Pool; | ||
|
||
constructor(private _userRepository: Repository<UserModel>) { | ||
this._Pool = PoolInstance.getInstance(); | ||
} | ||
|
||
public async CreateUser(req: any, res: any) { | ||
const payload = req.body; | ||
|
||
|
||
res.status(500).send({message: 'Method under construction.'}); | ||
} | ||
|
||
public async GetUser(req: any, res: any) { | ||
try { | ||
const repo = new UserRepository(); | ||
const users = await repo.GetById(req.params.id); | ||
users ? res.status(200).send({users: users}) : res.status(404).send({message: 'User not found'}); | ||
} catch (error) { | ||
res.status(400).send({message: GENERIC_ERROR, error: error.message}); | ||
} | ||
} | ||
|
||
public async GetUsers(req: any, res: any) { | ||
try { | ||
const repo = new UserRepository(); | ||
const users = await repo.GetAll(); | ||
users && users.length > 0 ? res.status(200).send({users: users}) : res.status(404).send({message: 'User not found'}); | ||
} catch (error) { | ||
res.status(400).send({message: GENERIC_ERROR, error: error.message}); | ||
} | ||
} | ||
|
||
|
||
} |
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,7 @@ | ||
export interface Repository<T> { | ||
Create(model: T): Promise<void>; | ||
Update(Id: any, payload: any): Promise<void>; | ||
Delete(Id: any): Promise<void>; | ||
GetById(Id: any): Promise<T>; | ||
GetAll(): Promise<T[]>; | ||
} |
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,51 @@ | ||
export interface UserModel { | ||
id: number, | ||
username: string, | ||
password: string, | ||
email: string, | ||
name: string, | ||
surname: string, | ||
image?: string, | ||
cover?: string, | ||
bio?: string, | ||
total_followers: number, | ||
phone?: number, | ||
location?: string, | ||
city?: string, | ||
country: string, | ||
theme_hex?: string, | ||
iat: Date, | ||
role: string, | ||
private: boolean, | ||
verified: boolean, | ||
confirmed: boolean, | ||
active: boolean, | ||
last_modification: Date | ||
} | ||
|
||
export function toModel(objectModel: any): UserModel { | ||
return { | ||
id: Number(objectModel.id), | ||
username: objectModel.username, | ||
password: objectModel.password, | ||
email: objectModel.email, | ||
name: objectModel.name, | ||
surname: objectModel.surname, | ||
image: objectModel.image, | ||
cover: objectModel.cover, | ||
bio: objectModel.bio, | ||
total_followers: Number(objectModel.total_followers), | ||
phone: Number(objectModel.phone), | ||
location: objectModel.location, | ||
city: objectModel.city, | ||
country: objectModel.country, | ||
theme_hex: objectModel.theme_hex, | ||
iat: new Date(objectModel.iat), | ||
role: objectModel.role, | ||
private: objectModel.private, | ||
verified: objectModel.verified, | ||
confirmed: objectModel.confirmed, | ||
active: objectModel.active, | ||
last_modification: new Date(objectModel.last_modification) | ||
}; | ||
} |
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,20 @@ | ||
import { Pool } from 'pg'; | ||
|
||
export abstract class PoolInstance { | ||
private static _Pool: Pool; | ||
|
||
constructor(){} | ||
|
||
public static getInstance(): Pool { | ||
if ( !PoolInstance._Pool ) { | ||
PoolInstance._Pool = new Pool({ | ||
connectionString: process.env.LOCAL_DB | ||
}); | ||
} | ||
return PoolInstance._Pool; | ||
} | ||
|
||
public query(text: string, params?: any): any { | ||
return PoolInstance._Pool.query(text, params); | ||
}; | ||
} |
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,44 @@ | ||
import { Repository } from "../../core/repository"; | ||
import { UserModel, toModel } from "../../domain/models/user.model"; | ||
import * as AWS from 'aws-sdk'; | ||
import { Pool } from 'pg'; | ||
import { PoolInstance } from "../pool"; | ||
|
||
export class UserRepository implements Repository<UserModel> { | ||
private _Pool: Pool; | ||
|
||
constructor() { | ||
this._Pool = PoolInstance.getInstance(); | ||
} | ||
|
||
Create(model: UserModel): any { | ||
// const rds = new AWS.RDS(); | ||
return this._Pool.query(`CALL CLIENT.CREATE_USER()`) | ||
} | ||
|
||
Update(Id: number, payload: any): any { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
Delete(Id: number): any { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
GetById(Id: number): Promise<UserModel> { | ||
return this._Pool.query(`SELECT * FROM CLIENT.GET_USER(${Id})`) | ||
.then(user => toModel(user.rows[0])) | ||
.catch(e => { throw e }); | ||
} | ||
|
||
GetAll(): Promise<UserModel[]> { | ||
return this._Pool.query('SELECT * FROM CLIENT.USER_BY_FOLLOWERS()') | ||
.then(usersDB => { | ||
usersDB.rows.forEach((item, i) => { | ||
usersDB.rows[i] = toModel(item); | ||
}); | ||
return usersDB.rows; | ||
}) | ||
.catch( e => { throw e }); | ||
|
||
} | ||
} |
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,14 @@ | ||
import { UserController } from '../controllers/user.controller'; | ||
import express from 'express'; | ||
import { UserRepository } from '../infrastructure/repositories/user.repository'; | ||
|
||
const _userRepo = new UserRepository(); | ||
|
||
const api = express.Router(); | ||
const controller = new UserController(_userRepo); | ||
|
||
api.post('/user', controller.CreateUser); | ||
api.get('/user', controller.GetUsers); | ||
api.get('/user/:id', controller.GetUser); | ||
|
||
export const UserRoutes = api; |
Oops, something went wrong.