68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
"use strict";
|
|
|
|
const express = require("express"),
|
|
fs = require("fs"),
|
|
url = require("url"),
|
|
config = require("config"),
|
|
moment = require("moment");
|
|
|
|
let photoDB;
|
|
|
|
require("../db/photos").then(function(db) {
|
|
photoDB = db;
|
|
});
|
|
|
|
const router = express.Router();
|
|
|
|
router.get("/*", function(req, res/*, next*/) {
|
|
let url = decodeURI(req.url).replace(/\?.*$/, "").replace(/^\//, ""),
|
|
query = "SELECT * FROM albums WHERE path=:path";
|
|
// console.log("Looking up album: " + url);
|
|
return photoDB.sequelize.query(query, {
|
|
replacements: {
|
|
path: url
|
|
},
|
|
type: photoDB.Sequelize.QueryTypes.SELECT
|
|
}).then(function(parent) {
|
|
if (parent.length == 0) {
|
|
return res.status(404).send(req.url + " not found");
|
|
}
|
|
|
|
parent = parent[0];
|
|
for (var key in parent) {
|
|
if (parent[key] instanceof Date) {
|
|
parent[key].setHours(0, 0, 0, 0);
|
|
parent[key] = moment(parent[key]);
|
|
}
|
|
}
|
|
|
|
return photoDB.sequelize.query("SELECT * FROM albums WHERE parentId=:parentId ORDER BY LOWER(name)", {
|
|
replacements: {
|
|
parentId: parent.id
|
|
},
|
|
type: photoDB.Sequelize.QueryTypes.SELECT
|
|
}).then(function(children) {
|
|
children.forEach(function(album) {
|
|
for (var key in album) {
|
|
if (album[key] instanceof Date) {
|
|
album[key].setHours(0, 0, 0, 0);
|
|
album[key] = moment(album[key]);
|
|
}
|
|
}
|
|
});
|
|
|
|
let results = {
|
|
album: parent,
|
|
children: children
|
|
};
|
|
return res.status(200).json(results);
|
|
});
|
|
}).catch(function(error) {
|
|
|
|
console.error("Query failed: " + query);
|
|
return Promise.reject(error);
|
|
});
|
|
});
|
|
|
|
module.exports = router;
|