From d53a2eec860bacf5ceab3847d1b9d1553426d984 Mon Sep 17 00:00:00 2001 From: benmcmahon100 Date: Wed, 13 Apr 2016 00:05:13 +0100 Subject: [PATCH] BillBoard now Flyer. Changes to API and view logic Fix to replace of null error. Changed Flyer to flyer --- client/main.js | 19 ++++++--- common/models/{BillBoard.json => Flyer.json} | 2 +- server/boot/randomAPIs.js | 44 -------------------- server/model-config.json | 2 +- 4 files changed, 15 insertions(+), 52 deletions(-) rename common/models/{BillBoard.json => Flyer.json} (96%) diff --git a/client/main.js b/client/main.js index db28dec2c1..0b17f18749 100644 --- a/client/main.js +++ b/client/main.js @@ -134,7 +134,7 @@ main = (function(main, global) { localStorage.setItem(item, input); return input; } else { - let data = localStorage.getItem(item); + let data = typeof localStorage.getItem(item) !== 'undefined' && localStorage.getItem(item) !== null ? localStorage.getItem(item) : ""; try { data = JSON.parse(data); } catch (e) { @@ -637,9 +637,11 @@ $(document).ready(function() { function getCurrentBillBoard(cb) { $.ajax({ - url: '/billboard', + url: '/api/Flyers/findOne?' + + 'filter=%7B%22order%22%3A%20%20%22id%20DESC%22%7D', method: 'GET', - dataType: 'JSON' + dataType: 'JSON', + data: {'order': 'id DESC'} }).done((resp) => { cb(resp); }); @@ -657,9 +659,14 @@ $(document).ready(function() { }); } - function handleNewBillBoard(message) { - if (main.localStorageIO('lastBillBoardSeen') !== message.data) { - $('#billContent').text(message.data); + function handleNewBillBoard(resp) { + const data = typeof main.localStorageIO('lastBillBoardSeen') !== "undefined" && main.localStorageIO('lastBillBoardSeen') !== null ? main.localStorageIO('lastBillBoardSeen') : ""; + if ( + data.replace(/\s/gi, '') + !== resp.message.replace(/\s/gi, '') + && resp.active + ) { + $('#billContent').html(resp.message); $('#billBoard').fadeIn(); } } diff --git a/common/models/BillBoard.json b/common/models/Flyer.json similarity index 96% rename from common/models/BillBoard.json rename to common/models/Flyer.json index 6647dab585..35766a5fdb 100644 --- a/common/models/BillBoard.json +++ b/common/models/Flyer.json @@ -1,5 +1,5 @@ { - "name": "BillBoard", + "name": "Flyer", "base": "PersistedModel", "idInjection": true, "trackChanges": false, diff --git a/server/boot/randomAPIs.js b/server/boot/randomAPIs.js index 39dccaf24b..3fd3f953bd 100644 --- a/server/boot/randomAPIs.js +++ b/server/boot/randomAPIs.js @@ -7,7 +7,6 @@ import secrets from '../../config/secrets'; module.exports = function(app) { const router = app.loopback.Router(); const User = app.models.User; - const BillBoard = app.models.BillBoard; router.get('/api/github', githubCalls); router.get('/api/blogger', bloggerCalls); router.get('/api/trello', trelloCalls); @@ -39,7 +38,6 @@ module.exports = function(app) { '/the-fastest-web-page-on-the-internet', theFastestWebPageOnTheInternet ); - router.get('/billBoard', billBoard); app.use(router); @@ -84,48 +82,6 @@ module.exports = function(app) { }); } - function billBoard(req, res) { - if ( - req.user - && typeof req.user.currentChallenge.challengeId !== 'undefined' - ) { - BillBoard.findOne({}, function(err, data) { - if (err) { - res.send({ - err: { - type: 'Error', - message: 'Database Error' - }, - data: null - }); - } else { - if (data.active) { - res.send({ - err: null, - data: data.message - }); - } else { - res.send({ - err: { - type: 'warning', - message: 'Bill Board is not active' - }, - data: null - }); - } - } - }); - } else { - res.send({ - err: { - type: 'warning', - message: 'User Not Signed In' - }, - data: null - }); - } - } - function theFastestWebPageOnTheInternet(req, res) { res.render('resources/the-fastest-web-page-on-the-internet', { title: 'This is the fastest web page on the internet' diff --git a/server/model-config.json b/server/model-config.json index 30e527c45d..60800cf81b 100644 --- a/server/model-config.json +++ b/server/model-config.json @@ -67,7 +67,7 @@ "dataSource": "db", "public": true }, - "BillBoard": { + "flyer": { "dataSource": "db", "public": true }