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

Commit

Permalink
Merge pull request #36 from wbprice/feature/save-and-retrieve-images
Browse files Browse the repository at this point in the history
Save and Retrieve Images
  • Loading branch information
ryayak1460 committed Feb 7, 2015
2 parents 6ab41fb + 90d468f commit 65b21e4
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 7 deletions.
126 changes: 120 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,79 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

'use strict';

var Hapi = require('hapi');
var mongoose = require('mongoose');
var queryOverpass = require('query-overpass');
var _ = require('underscore');

var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || 'localhost';
var database_address = process.env.MONGO_ADDR || 'localhost:27017/localartapi';
var auth = process.env.MONGO_USER + ':' + process.env.MONGO_PASS;
var db;

if (process.env.MONGO_USER) {
mongoose.connect('mongodb://' + auth + '@' + database_address);
} else {
mongoose.connect(database_address);
}

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function(callback) {
console.log('Database connected successfully.');
});

// Create server with host and port;
var server = new Hapi.Server();

server.connection({
host: server_ip_address,
port: server_port,
});

// Add the route
server.views({
engines: {
html: require('handlebars'),
},
path: __dirname + '/templates'
});

// Add the routes

// Reference Route
server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {
reply.view('index');
}
});

// Get specific exhibit
server.route({
method: 'GET',
path: '/templates/{path*}',
handler: {
directory: {
path: './templates',
listing: false,
index: false
}
}
});

// Exhibits Route
server.route({
method: 'GET',
path: '/exhibits/{id?}',
config: {
cors: true
},
handler: function(request, reply) {
'use strict';
var bbox, longitude, latitude, numberOfResults, zoomLevel, query;

// Given a bounding box, return some results.
Expand Down Expand Up @@ -95,15 +145,15 @@ server.route({
url: tags.source,
imageurl: tags.website_1,
fullimage: tags.website,
description: tags.note
+ (function extendedDescription(tags) {
description: tags.note +
(function extendedDescription(tags) {
var i = arguments.length > 1 ?
arguments[1] :
1;
return (function (element) {
return tags.hasOwnProperty(element) ?
tags[element]
+ extendedDescription(tags, i + 1) :
tags[element] +
extendedDescription(tags, i + 1) :
'';
})('note_' + i.toString());
})(tags)
Expand All @@ -121,5 +171,69 @@ server.route({
}
});

// DB stuff
var Schema = mongoose.Schema;
var exhibitSchema = new Schema({
node: String,
thumbnail: String,
image: String
});
var Exhibit = mongoose.model('Exhibit', exhibitSchema);

// New exhibits route
server.route({
method: 'POST',
path: '/post/exhibits',
config: {
cors: true
},
handler: function(request, reply) {
var node, thumbnail, image;

console.log(request.payload);

node = request.payload.node;
thumbnail = request.payload.thumbnail;
image = request.payload.image;

var exhibit = new Exhibit({
node: node,
thumbnail: thumbnail,
image: image
});

exhibit.save(function(err, exhibit) {
if (err) {
return console.error(err);
} else {
reply('Exhibit ' + exhibit.node + ' was saved to mongoDB.');
}
});
}
});

// Images Route
server.route({
method: 'GET',
path: '/images/{id?}',
config: {
cors: true
},
handler: function(request, reply) {
console.log(request.params);
var requestedImage = request.params.id;

Exhibit.find({node: requestedImage},function(err, exhibits) {
if (err) {
return console.error(err);
} else {
reply(exhibits);
}
});

}

});

// Server start
server.start();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"npm": ">= 1.0.0"
},
"dependencies": {
"handlebars": "^2.0.0",
"hapi": "^8.0.0",
"joi": "^5.1.0",
"lout": "^6.1.0",
Expand All @@ -37,6 +38,8 @@
"devDependencies": {
"code": "^1.2.1",
"jshint": "^2.6.0",
"lab": "^5.2.0"
"lab": "^5.2.0",
"mongo": "^0.1.0",
"mongoose": "^3.8.22"
}
}
60 changes: 60 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<html>
<head>
<Title>LocalArt API</Title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css
">
<link rel="stylesheet" type="text/css" href="/templates/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

<div class="pure-g">

<header>
<div class="pure-u-1">
<h1>LocalArt API Documentation</h1>
</div>
</header>

<section id class="pure-u-1">
<h2>Get exhibits</h2>
<p>Returns an object containing all exhibits tagged in Norfolk, VA</p>
<table class="pure-table pure-table-bordered">
<tr>
<td>
URL
</td>
<td>
/exhibits
</td>
</tr>
<tr>
<td>
Method
</td>
<td>
GET
</td>
</tr>
<tr>
<td>
Options
</td>
<td>
<dl>
<dt>bbox</dt>
<dd><p>Limit search area with longitude and latitude ranges to make a bounding box. Follows the order: min Longitude, min Latitude, max Longitude, max Latitude.</p>
<em>/exhibits?bbox=36.75,-76.44,36.98,-76.13</em></dd>
</dl>
</td>
</tr>
</table>
</section>

</div>

</body>

</html>
19 changes: 19 additions & 0 deletions templates/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
* {
color: #1a1a1a;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

.pure-u-1 {
padding: 0 3em;
}

header {
background: #1a1a1a;
}

header h1 {
color: white;
font-weight: 300;
}

0 comments on commit 65b21e4

Please sign in to comment.