Skip to content

Commit

Permalink
change codebase to TypeScript and beginning of event API (#14)
Browse files Browse the repository at this point in the history
* typescript redo

* create event test

* move to typescript

* remove build folder

* typescript redo

* ignore build folder

* ignore build folder

* update draft pull request

* change route entrypoint

* change route

* add event description

* schema change

* change from class to function

* get event query test

* fix getevent model

* add event test

* change to class
  • Loading branch information
miami78 authored and fatmali committed Dec 19, 2019
1 parent 0d550af commit f125d3f
Show file tree
Hide file tree
Showing 19 changed files with 719 additions and 8,309 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": true,
"mocha":true
},
"extends": ["airbnb-base", "prettier"],
"extends": ["prettier", "plugin:node/recommended"],
"plugins": ["prettier"],
"parserOptions": {
"ecmaVersion": 2018,
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bower_components

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
dist/

# Dependency directories
node_modules/
Expand Down
7 changes: 7 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"watch": "src/**/*.ts",
"execMap": {
"ts": "ts-node"
},
"exec": "ts-node -r tsconfig-paths/register --cacheDirectory tmp/tscache src/server.ts"
}
7,002 changes: 0 additions & 7,002 deletions package-lock.json

This file was deleted.

41 changes: 23 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"description": "This repo serves as the backend for the Developer Circles Eldoret Community website.",
"main": "server.js",
"scripts": {
"start": "nodemon --require @babel/register --require @babel/register ./src/server.js",
"test": "nyc mocha --recursive --require @babel/register --require @babel/polyfill --exit ./src/test/*.test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"start": "nodemon --exec \"ts-node\" ./src/server.ts",
"test": "nyc mocha -r ts-node/register --exit ./src/test/*.test.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"build": "tsc --build tsconfig.json"
},
"repository": {
"type": "git",
Expand All @@ -20,33 +21,37 @@
},
"homepage": "",
"dependencies": {
"@babel/polyfill": "^7.7.0",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"cors": "^2.8.4",
"dotenv-config": "^0.1.1",
"express": "^4.17.1",
"nodemon": "^1.19.4",
"nyc": "^14.1.1"
"express": "^4.17.1"

},
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@babel/register": "^7.7.0",
"@types/pg": "^7.11.2",
"@types/supertest": "^2.0.8",
"@types/chai": "^4.2.7",
"@types/express": "^4.17.2",
"@types/mocha": "^5.2.7",
"@types/node": "^12.12.17",
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"coveralls": "^3.0.7",
"chai": "^4.2.0",
"custom-env": "^1.0.2",
"eslint": "^6.6.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-node": "^4.0.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.1.1",
"mocha": "^6.2.2",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"supertest": "^4.0.2"
"supertest": "^4.0.2",
"ts-node": "^8.5.4",
"typescript": "^3.7.3",
"nyc": "^14.1.1",
"pg": "^7.14.0",
"nodemon": "^1.19.4"
}
}
18 changes: 18 additions & 0 deletions src/DB/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exports.getEvents = {
id: 1,
title: 'Community Challenge',
description:'Join us as we celebrate a year of growth and collaboration',
location: [{
"name": "Sirikwa Hotel",
"street": "Oloo Street",
"city": "Eldoret"
}],
startTime: '2019-08-15 21:05:15.723336+07',
endTime: '2019-08-15 21:05:15.723336+07',
mediaLink: 'https://photos.app.goo.gl/323EcdkmFTzrfLd96',
speakers: [{
"name": "Marvin Kweyu",
"job title": "Django Developer",
"bio": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
}]
};
16 changes: 16 additions & 0 deletions src/DB/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Pool } from "pg";

const pool = new Pool({
//ignore this im getting stuck on destructuring and passing my env variables with Typescript
connectionString: "postgres://postgres:50filthyCENT!@localhost:5432/devceldoret"
});

class Database {
static async query(query:any, value:any, isArray = false) {
const response = await pool.query(query, value);
const result = isArray ? response.rows : response.rows[0];
return result;
}
}

export default Database;
10 changes: 10 additions & 0 deletions src/Utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ErrorHandler extends Error {
status: number;
constructor(message:string, status:number) {
super(message);
this.status = status;
}
}

export default ErrorHandler;

18 changes: 0 additions & 18 deletions src/Utils/helper.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/Utils/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require("dotenv").config({ path: `${__dirname}/.env` });
const pg = require("pg");

const {
APP_ENV,
DB_HOST,
DB_USER,
DB_PASS
} = process.env;

if (process.env.NODE_ENV === "development")

module.exports = new pg.Pool();
18 changes: 18 additions & 0 deletions src/controllers/event-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import EventModel from "../models/events";

class EventController {
static async getEvents(_req: any, res: any, next: any) {
try {
const events = await EventModel.getEvents();
const data = [...events];
res.status(200).json(data);
} catch (error) {
next(error);
}
}

}



export default EventController;
12 changes: 12 additions & 0 deletions src/models/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Database from "../db/index";

class EventModel {
static async getEvents() {
const response = await Database.query('SELECT * FROM events ORDER BY id ASC', '',true).catch((error) => {
throw new Error(error.message);
});
return response;
}
}

export default EventModel;
22 changes: 22 additions & 0 deletions src/routes/event-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Router } from "express";
//import { Pool } from 'pg';
import EventController from "../controllers/event-controller";

const router = Router();
// Added for testing purposes
// const pool = new Pool({
// connectionString: "postgres://postgres:50filthyCENT!@localhost:5432/devceldoret"
// });

// const getEvents = (_request:any, response:any) => {
// pool.query('SELECT * FROM events ORDER BY id ASC', (error, results) => {
// if (error) {
// throw error;
// }
// response.status(200).json(results.rows);
// });
// };

router.get("/", EventController.getEvents);

export default router;
14 changes: 9 additions & 5 deletions src/server.js → src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@babel/polyfill";
require('custom-env').env()
import express from "express";
import bodyParser from "body-parser";
import helper from "./Utils/helper";
import EventRoute from "./routes/event-route";

const app = express();

Expand All @@ -13,6 +13,10 @@ app.get("/", (request, response) => {
response.json({ message: "Server starts successfully!" });
});

// routes
app.use("/api/v1/event", EventRoute);

// CORS
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-type");
Expand All @@ -23,9 +27,9 @@ app.use((req, res, next) => {
next();
});

app.set("port", helper.PORT);
app.listen(helper.PORT, () => {
console.log("app is running on port ", helper.PORT);
app.set("port", process.env.PORT || "3000");
app.listen(process.env.PORT || "3000", () => {
console.log("app is running on port ", process.env.PORT || "3000");
});

export default app;
35 changes: 35 additions & 0 deletions src/test/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from "chai";

import request from "supertest";

const getEvents = require("../DB/db.js");
const Database = require ("../DB/index");

import app from "../server";

const url = '/api/v1/';

describe("DevC", () => {
describe('User can view all events', () => {
it('GET /event', (done) => {
request(app)
.get('/api/v1/event')
.set('Accept', 'application/json')
.expect("Content-Type", /json/)
.then((response) => {
const { data,status } = response.body;
expect(response.status).to.equal(200);
//expect(data.id).to.be.a("number");
// expect(data.title).to.be.a("string");
// expect(data.description).to.be.a("string");
// expect(data.location).to.be.an("array");
// expect(data.startTime).to.be.a("string");
// expect(data.endTime).to.be.a("string");
// expect(data.mediaLink).to.be.a("string");
// expect(data.speakers).to.be.an("array");
done();
})
.catch((error: any) => done(error));
});
});
});
2 changes: 1 addition & 1 deletion src/test/server.test.js → src/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import request from "supertest";

import app from "../server";

describe("Teamwork", () => {
describe("DevC", () => {
// gets json response
describe("GET /", () => {
it("responds with json", done => {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"]
}
Loading

0 comments on commit f125d3f

Please sign in to comment.