Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #12 from microservices-demo/adduser
Browse files Browse the repository at this point in the history
Adduser
  • Loading branch information
jasonrichardsmith committed Aug 26, 2016
2 parents 8b7ca83 + 86bacce commit 1687b53
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 94 deletions.
10 changes: 5 additions & 5 deletions api/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
tagsUrl: "http://catalogue/tags",
cartsUrl: "http://cart/carts",
ordersUrl: "http://orders",
customersUrl: "http://accounts/customers",
addressUrl: "http://accounts/addresses",
cardsUrl: "http://accounts/cards",
loginUrl: "http://login/login",
registerUrl: "http://login/register"
customersUrl: "http://user/customers",
addressUrl: "http://user/addresses",
cardsUrl: "http://user/cards",
loginUrl: "http://user/login",
registerUrl: "http://user/register",
};
}());
73 changes: 0 additions & 73 deletions api/login/index.js

This file was deleted.

101 changes: 95 additions & 6 deletions api/accounts/index.js → api/user/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
(function (){
'use strict';

var express = require("express")
var async = require("async")
, express = require("express")
, request = require("request")
, endpoints = require("../endpoints")
, helpers = require("../../helpers")
, app = express()
, cookie_name = "logged_in"

// Register - TO BE USED FOR TESTING ONLY (for now)
app.get("/register", function(req, res, next) {
helpers.simpleHttpRequest(registerUrl + "?username=" + req.query.username + "&password=" + req.query.password, res, next);
});

app.get("/accounts/:id", function (req, res, next) {
app.get("/customers/:id", function (req, res, next) {
helpers.simpleHttpRequest(endpoints.customersUrl + "/" + req.params.id, res, next);
});
app.get("/cards/:id", function (req, res, next) {
helpers.simpleHttpRequest(endpoints.cardsUrl + "/" + req.params.id, res, next);
});

app.get("/customers", function (req, res, next) {
helpers.simpleHttpRequest(endpoints.customersUrl, res, next);
});
app.get("/addresses", function (req, res, next) {
helpers.simpleHttpRequest(endpoints.addressUrl, res, next);
});
app.get("/cards", function (req, res, next) {
helpers.simpleHttpRequest(endpoints.cardsUrl, res, next);
});
// Create Customer - TO BE USED FOR TESTING ONLY (for now)
app.post("/customers", function(req, res, next) {
var options = {
Expand All @@ -35,6 +45,25 @@
}.bind({res: res}));
});

// Create Customer - TO BE USED FOR TESTING ONLY (for now)
app.post("/register", function(req, res, next) {
var options = {
uri: endpoints.registerUrl,
method: 'POST',
json: true,
body: req.body
};

console.log("Posting Customer: " + JSON.stringify(req.body));

request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

// Create Address - TO BE USED FOR TESTING ONLY (for now)
app.post("/addresses", function(req, res, next) {
var options = {
Expand Down Expand Up @@ -114,5 +143,65 @@
}.bind({res: res}));
});

app.get("/login", function (req, res, next) {
console.log("Received login request");

async.waterfall([
function (callback) {
var options = {
headers: {
'Authorization': req.get('Authorization')
},
uri: endpoints.loginUrl
};
request(options, function (error, response, body) {
if (error) {
callback(error);
return;
}
if (response.statusCode == 200 && body != null && body != "") {
console.log(body);
var customerId = JSON.parse(body).user.id;
console.log(customerId);
callback(null, customerId);
return;
}
console.log(response.statusCode);
callback(true);
});
},
function (custId, callback) {
var sessionId = req.session.id;
console.log("Merging carts for customer id: " + custId + " and session id: " + sessionId);

var options = {
uri: endpoints.cartsUrl + "/" + custId + "/merge" + "?sessionId=" + sessionId,
method: 'GET'
};
request(options, function (error, response, body) {
if (error) {
callback(error);
return;
}
console.log('Carts merged.');
callback(null, custId);
});
}
],
function (err, custId) {
if (err) {
console.log("Error with log in: " + err);
res.status(401);
res.end();
return;
}
res.status(200);
res.cookie(cookie_name, custId, {maxAge: 3600000}).send('Cookie is set');
console.log("Sent cookies.");
res.end();
return;
});
});

module.exports = app;
}());
2 changes: 1 addition & 1 deletion public/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function addToCart(id) {
function username(id, callback) {
console.log("Requesting user account information " + id);
$.ajax({
url: "accounts/" + id,
url: "customers/" + id,
type: "GET",
success: function (data, textStatus, jqXHR) {
callback(JSON.parse(data).firstName + " " + JSON.parse(data).lastName);
Expand Down
6 changes: 2 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ var request = require("request")
, session = require("express-session")
, config = require("./config")
, helpers = require("./helpers")
, login = require("./api/login")
, cart = require("./api/cart")
, accounts = require("./api/accounts")
, catalogue = require("./api/catalogue")
, orders = require("./api/orders")
, user = require("./api/user")
, app = express()

app.use(express.static("public"));
Expand All @@ -34,11 +33,10 @@ process.argv.forEach(function (val, index, array) {
});

/* Mount API endpoints */
app.use(login);
app.use(cart);
app.use(accounts);
app.use(catalogue);
app.use(orders);
app.use(user);

var server = app.listen(process.env.PORT || 8079, function () {
var port = server.address().port;
Expand Down
10 changes: 5 additions & 5 deletions test/api/endpoints_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@

describe("customersUrl", function() {
it("points to the proper endpoint", function() {
expect(endpoints.customersUrl).to.equal("http://accounts/customers");
expect(endpoints.customersUrl).to.equal("http://user/customers");
});
});

describe("addressUrl", function() {
it("points to the proper endpoint", function() {
expect(endpoints.addressUrl).to.equal("http://accounts/addresses");
expect(endpoints.addressUrl).to.equal("http://user/addresses");
});
});

describe("cardsUrl", function() {
it("points to the proper endpoint", function() {
expect(endpoints.cardsUrl).to.equal("http://accounts/cards");
expect(endpoints.cardsUrl).to.equal("http://user/cards");
});
});

describe("loginUrl", function() {
it("points to the proper endpoint", function() {
expect(endpoints.loginUrl).to.equal("http://login/login");
expect(endpoints.loginUrl).to.equal("http://user/login");
});
});

describe("registerUrl", function() {
it("points to the proper endpoint", function() {
expect(endpoints.registerUrl).to.equal("http://login/register");
expect(endpoints.registerUrl).to.equal("http://user/register");
});
});
});
Expand Down

0 comments on commit 1687b53

Please sign in to comment.