-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from CirclesUBI/news-activity-log
News activity log
- Loading branch information
Showing
10 changed files
with
366 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Op } from 'sequelize'; | ||
|
||
import News from '../models/news'; | ||
import { respondWithSuccess } from '../helpers/responses'; | ||
|
||
function prepareNewsResult(response) { | ||
return { | ||
iconId: response.iconId, | ||
message: { | ||
en: response.message_en, | ||
}, | ||
date: response.date, | ||
isActive: response.isActive, | ||
}; | ||
} | ||
|
||
async function resolveBatch(req, res, next) { | ||
const { isActive, limit, offset } = req.query; | ||
|
||
News.findAll({ | ||
where: { | ||
isActive: isActive, | ||
}, | ||
order: [['date', 'DESC']], | ||
limit: limit, | ||
offset: offset, | ||
}) | ||
.then((response) => { | ||
respondWithSuccess(res, response.map(prepareNewsResult)); | ||
}) | ||
.catch((err) => { | ||
next(err); | ||
}); | ||
} | ||
|
||
async function findByDate(req, res, next) { | ||
const { isActive, afterDate, limit, offset } = req.query; | ||
|
||
News.findAll({ | ||
where: { | ||
isActive: isActive, | ||
date: { [Op.gte]: new Date(afterDate) }, | ||
}, | ||
order: [['date', 'DESC']], | ||
limit: limit, | ||
offset: offset, | ||
}) | ||
.then((response) => { | ||
respondWithSuccess(res, response.map(prepareNewsResult)); | ||
}) | ||
.catch((err) => { | ||
next(err); | ||
}); | ||
} | ||
|
||
export default { | ||
findNews: async (req, res, next) => { | ||
if (req.query.afterDate) { | ||
return await findByDate(req, res, next); | ||
} | ||
return await resolveBatch(req, res, next); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('news', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
createdAt: { | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
type: Sequelize.DATE, | ||
}, | ||
message_en: { | ||
type: Sequelize.TEXT, | ||
}, | ||
date: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
validate: { | ||
notEmpty: true, | ||
}, | ||
defaultValue: Sequelize.NOW, | ||
}, | ||
iconId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}, | ||
isActive: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: true, | ||
}, | ||
}); | ||
}, | ||
|
||
async down(queryInterface) { | ||
await queryInterface.dropTable('news'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Sequelize from 'sequelize'; | ||
|
||
import db from '../database'; | ||
|
||
const News = db.define('news', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
message_en: { | ||
type: Sequelize.TEXT, | ||
}, | ||
date: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
validate: { | ||
notEmpty: true, | ||
}, | ||
defaultValue: Sequelize.NOW, | ||
}, | ||
iconId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}, | ||
isActive: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: true, | ||
}, | ||
}); | ||
|
||
export default News; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import express from 'express'; | ||
|
||
import newsController from '../controllers/news'; | ||
import newsValidation from '../validations/news'; | ||
import validate from '../helpers/validate'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', validate(newsValidation.findNews), newsController.findNews); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Joi } from 'celebrate'; | ||
|
||
export default { | ||
findNews: { | ||
query: Joi.object({ | ||
isActive: Joi.boolean().default(true), | ||
afterDate: Joi.date(), | ||
limit: Joi.number().integer().default(10), | ||
offset: Joi.number().integer().default(0), | ||
}), | ||
}, | ||
}; |
Oops, something went wrong.