Skip to content

Commit

Permalink
added basic endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacionr committed Aug 29, 2016
1 parent 1aeabf5 commit 3a47a68
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 44 deletions.
38 changes: 33 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,45 @@ let device_service = new Device.Service();
var router = express.Router();
router.post("/signup", (req, res) => {
let user = { username: req.body.username, password: req.body.password };
user_service.signup(user).then(() => res.json(true)).catch(() => res.json(false));
user_service.signup(user).then(() => res.json({ success: user.username })).catch((reason) => res.json({ error: reason }));
});
router.post('/login', function (req, res) {
res.json({ token: 'youmadeit' });
return user_service.login(req.body.username, req.body.password).then(user => {
let token = session_service.acquire(user.username);
res.json({ token: token });
})
.catch(reason => res.json({ error: reason }));
});
router.post("/lost-password", function (req, res) {
res.json({ success: "An email will be sent to your address." });
});
router.get('/switch', function (req, res) {
res.json({ status: switch_status });
session_service.retrieve(req.query.token).then(user => res.json({ status: switch_status }))
.catch(reason => res.json({ error: reason }));
});
router.post('/switch', function (req, res) {
switch_status = req.body.status;
res.json({ status: switch_status });
session_service.retrieve(req.query.token).then(user => {
switch_status = req.body.status;
res.json({ status: switch_status });
})
.catch(reason => res.json({ error: reason }));
});
router.post("/history", function (req, res) {
session_service.retrieve(req.query.token).then(user => {
res.json({ user: user,
history: [
{ date: new Date(2016, 8, 1, 10, 10), status: { switch: true, presence: true } },
{ date: new Date(2016, 8, 1, 10, 16), status: { switch: true, presence: true } },
{ date: new Date(2016, 8, 1, 10, 30), status: { switch: false, presence: false } },
{ date: new Date(2016, 8, 1, 11, 10), status: { switch: false, presence: false } },
{ date: new Date(2016, 8, 1, 11, 16), status: { switch: true, presence: false } },
{ date: new Date(2016, 8, 1, 15, 22), status: { switch: true, presence: true } },
{ date: new Date(2016, 8, 1, 15, 30), status: { switch: true, presence: true } },
{ date: new Date(2016, 8, 1, 15, 35), status: { switch: false, presence: true } },
{ date: new Date(2016, 8, 1, 15, 55), status: { switch: true, presence: true } },
] });
})
.catch(reason => res.json({ error: reason }));
});
app.use('/', router);
app.listen(port);
5 changes: 5 additions & 0 deletions dist/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
class Model {
}
exports.Model = Model;
;
28 changes: 28 additions & 0 deletions dist/services/session.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
"use strict";
const Datastore = require("nedb");
const shortid = require("shortid");
class Token {
}
exports.Token = Token;
;
class Service {
constructor() {
this.db = new Datastore({ filename: "./sessions.db", autoload: true });
}
acquire(username) {
let token = new Token();
token.__id = shortid.generate();
token.username = username;
this.db.insert(token);
return token.__id;
}
retrieve(token) {
return new Promise((resolve, reject) => {
this.db.findOne({ __id: token }, (err, document) => {
if (err)
return reject(err.message);
if (document) {
return resolve(document.username);
}
return reject("Token not found.");
});
});
}
}
exports.Service = Service;
;
33 changes: 20 additions & 13 deletions dist/services/user.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
"use strict";
const Datastore = require("nedb");
class Model {
}
exports.Model = Model;
;
class Service {
constructor() {
this.db = new Datastore({ filename: "./users.db", autoload: true });
}
signup(user) {
return new Promise((resolve, reject) => this.db.insert(user, (err, document) => {
if (err) {
return reject(err);
}
return resolve();
}));
return new Promise((resolve, reject) => {
this.db.findOne({ username: user.username }, (err, doc) => {
if (err)
return reject(err);
if (doc) {
return reject("The user already exists.");
}
this.db.insert(user, (err, document) => {
if (err) {
return reject(err.message);
}
return resolve(true);
});
});
});
}
;
login(username, password) {
return new Promise((resolve, reject) => this.db.findOne({
username: username, password: password
}, (err, document) => {
if (err) {
return reject(err);
return reject(err.message);
}
else {
return resolve(document);
if (document) {
return resolve(document);
}
return reject("Not found.");
}
}));
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"nedb": "^1.8.0"
"nedb": "^1.8.0",
"shortid": "^2.2.6"
},
"devDependencies": {
"gulp": "^3.9.1",
Expand Down
3 changes: 3 additions & 0 deletions sessions.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"__id":"HkZEj0-j","username":"ignacio","_id":"Iey1FPz1W2a26EJT"}
{"__id":"SkWp9Cbi","username":"ignacio","_id":"uL5rllhEIEVAIjyn"}
{"__id":"B1d8GyGj","username":"ignacio","_id":"TprKNb94X88OnZSQ"}
43 changes: 37 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import express = require("express");
import bodyParser = require("body-parser");

import * as UserModel from "./models/user";

import * as Device from "./services/device";
import * as User from "./services/user";
import * as Session from "./services/session";
Expand All @@ -22,18 +24,47 @@ let device_service = new Device.Service();

var router = express.Router();
router.post("/signup", (req,res) => {
let user: User.Model = { username: req.body.username, password: req.body.password };
user_service.signup(user).then(() => res.json(true)).catch(() => res.json(false));
let user: UserModel.Model = { username: req.body.username, password: req.body.password };
user_service.signup(user).then(() => res.json({success: user.username})).catch((reason) => res.json({error: reason}));
});
router.post('/login', function(req, res) {
res.json({ token: 'youmadeit' });
return user_service.login(req.body.username, req.body.password).then(user =>
{
let token = session_service.acquire(user.username);
res.json({ token: token });
})
.catch(reason => res.json({ error: reason }));
});
router.post("/lost-password", function(req,res) {
res.json({success: "An email will be sent to your address."});
});
router.get('/switch', function(req, res) {
res.json({ status: switch_status });
session_service.retrieve(req.query.token).then(user => res.json({ status: switch_status }))
.catch(reason => res.json({error: reason}));
});
router.post('/switch', function(req, res) {
switch_status = req.body.status;
res.json({ status: switch_status });
session_service.retrieve(req.query.token).then(user => {
switch_status = req.body.status;
res.json({ status: switch_status });
})
.catch(reason => res.json({error: reason}));
});
router.post("/history", function(req,res) {
session_service.retrieve(req.query.token).then(user => {
res.json({user: user,
history: [
{ date: new Date(2016,8,1,10,10), status: {switch: true, presence: true} },
{ date: new Date(2016,8,1,10,16), status: {switch: true, presence: true} },
{ date: new Date(2016,8,1,10,30), status: {switch: false, presence: false} },
{ date: new Date(2016,8,1,11,10), status: {switch: false, presence: false} },
{ date: new Date(2016,8,1,11,16), status: {switch: true, presence: false} },
{ date: new Date(2016,8,1,15,22), status: {switch: true, presence: true} },
{ date: new Date(2016,8,1,15,30), status: {switch: true, presence: true} },
{ date: new Date(2016,8,1,15,35), status: {switch: false, presence: true} },
{ date: new Date(2016,8,1,15,55), status: {switch: true, presence: true} },
]});
})
.catch(reason => res.json({error: reason}));
});

app.use('/', router);
Expand Down
4 changes: 4 additions & 0 deletions src/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class Model {
username: string;
password: string;
};
33 changes: 32 additions & 1 deletion src/services/session.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
import Datastore = require("nedb");
import shortid = require("shortid");

export class Token {
__id : string;
username : string;
expiration : Date;
};

export class Service {

db: Datastore;
constructor() {
this.db = new Datastore({filename: "./sessions.db", autoload: true});
}
acquire(username: string): string {
let token = new Token();
token.__id = shortid.generate();
token.username = username;
this.db.insert(token);
return token.__id;
}
retrieve(token: string): Promise<string> {
return new Promise((resolve,reject) => {
this.db.findOne({__id: token}, (err,document: Token) => {
if (err)
return reject(err.message);
if (document) {
return resolve(document.username);
}
return reject("Token not found.");
});
});
}
};
42 changes: 25 additions & 17 deletions src/services/user.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import Datastore = require("nedb");

export class Model {
username: string;
password: string;
};
import * as UserModel from "../models/user";

export class Service {
db: Datastore;
constructor() {
this.db = new Datastore({filename: "./users.db", autoload: true});
}
signup(user: Model): Promise<any> {
return new Promise((resolve,reject) => this.db.insert(user, (err,document) => {
if (err) {
return reject(err);
}
return resolve();
}));
};
login(username: string, password: string): Promise<Model> {
return new Promise<Model>((resolve,reject) => this.db.findOne({
signup(user: UserModel.Model): Promise<any> {
return new Promise((resolve,reject) => {
this.db.findOne({username: user.username}, (err,doc) => {
if (err) return reject(err);
if (doc) {
return reject("The user already exists.");
}
this.db.insert(user, (err,document) => {
if (err) {
return reject(err.message);
}
return resolve(true);
}
);
});
});
}
login(username: string, password: string): Promise<UserModel.Model> {
return new Promise<UserModel.Model>((resolve,reject) => this.db.findOne({
username: username, password: password
}, (err,document) => {
if (err) {
return reject(err);
return reject(err.message);
}
else {
return resolve(document as Model);
if (document) {
return resolve(document as UserModel.Model);
}
return reject("Not found.");
}
}));
};
Expand Down
3 changes: 2 additions & 1 deletion typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"es6-promise": "registry:npm/es6-promise#3.0.0+20160723033700"
},
"globalDependencies": {
"nedb": "registry:dt/nedb#0.0.0+20160505185910"
"nedb": "registry:dt/nedb#0.0.0+20160505185910",
"shortid": "registry:dt/shortid#0.0.0+20160316155526"
}
}
9 changes: 9 additions & 0 deletions typings/globals/shortid/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/56295f5058cac7ae458540423c50ac2dcf9fc711/shortid/shortid.d.ts
declare module "shortid" {
export function generate(): string;
export function characters(string: string): string;
export function isValid(id: any): boolean;
export function worker(integer: number): void;
export function seed(float: number): void;
}
8 changes: 8 additions & 0 deletions typings/globals/shortid/typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"resolution": "main",
"tree": {
"src": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/56295f5058cac7ae458540423c50ac2dcf9fc711/shortid/shortid.d.ts",
"raw": "registry:dt/shortid#0.0.0+20160316155526",
"typings": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/56295f5058cac7ae458540423c50ac2dcf9fc711/shortid/shortid.d.ts"
}
}
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="globals/nedb/index.d.ts" />
/// <reference path="globals/node/index.d.ts" />
/// <reference path="globals/shortid/index.d.ts" />
/// <reference path="modules/body-parser/index.d.ts" />
/// <reference path="modules/es6-promise/index.d.ts" />
/// <reference path="modules/express/index.d.ts" />
15 changes: 15 additions & 0 deletions users.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{"_id":"1c966sQXzKWWPAyf"}
{"_id":"3u7L6W7f3zPV9yMb"}
{"username":"nicolas","password":"test123","_id":"A0hn3PZxux1OIyc5"}
{"_id":"B973eCDaoOyKL1A1"}
{"_id":"C7zA558rKbDUgXXU"}
{"_id":"CAPhGDJiwkmwKNl3"}
{"_id":"G5FNaevxTEcMH2y7"}
{"_id":"KCbkAzouj49JHIBn"}
{"_id":"MqWTHG88N6MtHqyg"}
{"_id":"O1Gv1VYFCVbxhwOk"}
{"_id":"QR7QVpxnn9vf3wHP"}
{"username":"ignacio","password":"test123","_id":"XhvPJQWc2w026Xzv"}
{"password":"test123","_id":"cEcFIaxF4iYlQxc6"}
{"_id":"d5AjbtdZz3nWvOl0"}
{"_id":"gWH2ZAKqMkz4vu37"}

0 comments on commit 3a47a68

Please sign in to comment.