Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
Add API endpoint for saving status change log
Browse files Browse the repository at this point in the history
  • Loading branch information
kabirbaidhya committed Jun 28, 2017
1 parent 5c65ef2 commit 8613884
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
14 changes: 14 additions & 0 deletions src/controllers/statusLog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import HttpStatus from 'http-status-codes';
import * as statusLogService from '../services/statusLog';

/**
Expand Down Expand Up @@ -27,3 +28,16 @@ export function getLatestStatus(req, res, next) {
.catch(err => next(err));
}

/**
* Save the status change log into the database.
*
* @export
* @param {any} req
* @param {any} res
* @param {any} next
*/
export function save(req, res, next) {
statusLogService.save(req.body)
.then(data => res.status(HttpStatus.CREATED).json(data))
.catch(err => next(err));
}
63 changes: 29 additions & 34 deletions src/models/StatusLog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import camelize from 'camelize';
import Status from './Status';
import Service from './Service';
import logger from '../utils/logger';
import { getClient } from '../utils/db';

Expand All @@ -14,47 +15,41 @@ class StatusLog extends db.Model {
return true;
}

service() {
return this.belongsTo(Service);
}

status() {
return this.belongsTo(Status);
}

static fetchAllLogs() {
logger().info('Fetching all status logs');

return db.knex
.select([
'status_logs.*',
'services.name as service_name',
'services.url as url',
'statuses.name as status'
])
.from('status_logs')
.innerJoin('services', 'status_logs.service_id', 'services.id')
.innerJoin('statuses', 'status_logs.status_id', 'statuses.id')
.orderBy('created_at', 'DESC')
.then(data => {
logger().debug('Retrieved status logs: ', data);

return camelize(data);
});
return StatusLog
.collection()
.query(qb => qb.orderBy('created_at', 'DESC'))
.fetch({
debug: true,
withRelated: ['status', 'service']
})
.then(collection => collection.toJSON());
}

static fetchLatestStatuses() {
logger().info('Fetching the latest status');

return db.knex
.select([
'status_logs.*',
'services.name as service_name',
'services.url as url',
'statuses.name as status'
])
.from('status_logs')
.innerJoin('services', 'status_logs.service_id', 'services.id')
.innerJoin('statuses', 'status_logs.status_id', 'statuses.id')
.groupBy('status_logs.service_id')
.orderBy('created_at', 'DESC')
.then(data => {
logger().debug('Retrieved the latest status logs: ', data);

return camelize(data);
});
return StatusLog
.collection()
.query(qb =>
qb.groupBy('service_id')
.orderBy('created_at', 'DESC')
)
.fetch({
debug: true,
withRelated: ['status', 'service']
})
.then(collection => collection.toJSON());
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Router } from 'express';
// Controllers
import * as homeController from './controllers/home';
import * as serviceController from './controllers/service';
import * as statusLogController from './controllers/statusLog';
// Validators
import { validateStatusLog } from './validators/statusLog';

const router = Router();

Expand All @@ -13,6 +16,7 @@ router.get('/status', statusLogController.getLatestStatus);

// Status Change logs
router.get('/status/logs', statusLogController.getAll);
router.post('/status/logs', validateStatusLog, statusLogController.save);

// Services
router.get('/services', serviceController.getAll);
Expand Down
16 changes: 16 additions & 0 deletions src/services/statusLog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logger from '../utils/logger';
import StatusLog from '../models/StatusLog';

/**
Expand All @@ -19,3 +20,18 @@ export function fetchAll() {
export function fetchLatestStatuses() {
return StatusLog.fetchLatestStatuses();
}

/**
* Persist a status change log into the database.
*
* @param {Object} data
* @return {Object}
*/
export async function save(data) {
logger().info('Saving status change log');
logger().debug('Data: ', data);

let model = await StatusLog.forge(data).save();

return model.toJSON();
}
4 changes: 3 additions & 1 deletion src/utils/db.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import knex from 'knex';
import bookshelf from 'bookshelf';
import bookshelfCamelcase from 'bookshelf-camelcase';

import * as config from '../config/config';

/**
Expand All @@ -18,7 +20,7 @@ export function getClient() {
const dbConfig = config.get().db;

db = bookshelf(knex(dbConfig));
db.plugin(['bookshelf-camelcase']);
db.plugin(bookshelfCamelcase);

return db;
}

0 comments on commit 8613884

Please sign in to comment.