Browse Source

add api endpoint for latest cmt members (#202)

* add api end point for latest cmt memebers

* parse year to return correct committee
pull/208/head
Cian Butler 4 years ago
committed by GitHub
parent
commit
4013f4f446
6 changed files with 66 additions and 1 deletions
  1. +4
    -0
      app.js
  2. +39
    -0
      lib/getCommittee.js
  3. +1
    -0
      package.json
  4. +17
    -0
      routes/committee.js
  5. +1
    -1
      themes/redbrick-theme
  6. +4
    -0
      yarn.lock

+ 4
- 0
app.js View File

@@ -58,6 +58,10 @@ app.use('/about/contact/', contactFormRoute);
const postsRoute = require('./routes/posts');
app.use(baseUrl, postsRoute);

// /api/committee returns committee as json
const committeeRoute = require('./routes/committee');
app.use(baseUrl, committeeRoute);

// /api/regenerate regenerates content without restarting the server
const regenerateRoute = require('./routes/regenerate');
app.use(baseUrl, regenerateRoute);


+ 39
- 0
lib/getCommittee.js View File

@@ -0,0 +1,39 @@
const fs = require('fs');
const path = require('path');
const parseFrontMatter = require('hexo-front-matter').parse;
const yaml = require('js-yaml');
const isUndefined = require('lodash.isundefined');

const configFile = fs.readFileSync('_config.yml', 'utf8');
const config = yaml.safeLoad(configFile).server;

const { readFileAsString } = require('./utils');

const getCommittee = (year) => {
return new Promise((resolve, reject) => {
let committeePage;
if (new Date().getFullYear() === parseInt(year) || isUndefined(year)) {
committeePage = path.join(process.cwd(), 'source', 'about', 'committee', 'index.md');
} else if (new Date().getFullYear() > parseInt(year)) {
committeePage = path.join(process.cwd(), 'source', 'about', 'past-committees', `the-committee-${year}-${parseInt(year) + 1}`, 'index.md');
} else {
return reject(new Error(`There is no committee for ${year}`));
}
readFileAsString(committeePage, (err, page) => {
if (err) {
return reject(err);
};

const frontMatter = parseFrontMatter(page);
const committee = frontMatter.cmt;
for (const i in committee) {
if (committee[i].image) {
committee[i].image = `${config.siteroot}/assets/images/${committee[i].image}`;
}
}
resolve(committee);
});
});
};

module.exports = getCommittee;

+ 1
- 0
package.json View File

@@ -63,6 +63,7 @@
"hexo-server": "^0.2.0",
"hexo-util": "^0.6.0",
"js-yaml": "^3.6.1",
"lodash.isundefined": "^3.0.1",
"moment": "^2.15.1",
"morgan": "~1.7.0",
"nodemailer": "^2.5.0",


+ 17
- 0
routes/committee.js View File

@@ -0,0 +1,17 @@
const express = require('express');
const router = express.Router();
const getCommittee = require('../lib/getCommittee');

/*
* Fetches Array of Current committee
*/
router.get('/committee', ({ query }, res) => {
getCommittee(query.year).then((committee) => {
res.json(committee).end();
})
.catch((err) => {
return res.status(500).json(err).end();
});
});

module.exports = router;

+ 1
- 1
themes/redbrick-theme

@@ -1 +1 @@
Subproject commit 304da269629eb5facf3d9b7e0e55fcb010846c91
Subproject commit ee88883594391b583976cfcc59fe5c74c8323487

+ 4
- 0
yarn.lock View File

@@ -2956,6 +2956,10 @@ lodash.isequal@^4.0.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"

lodash.isundefined@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash.isundefined/-/lodash.isundefined-3.0.1.tgz#23ef3d9535565203a66cefd5b830f848911afb48"

lodash.keys@^3.0.0:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a"


Loading…
Cancel
Save