post creation and adding ribbon functionality
This commit is contained in:
@@ -10,5 +10,6 @@ module.exports = {
|
|||||||
auth: require('./auth.controller'),
|
auth: require('./auth.controller'),
|
||||||
charge: require('./charge.controller'),
|
charge: require('./charge.controller'),
|
||||||
project: require('./project.controller'),
|
project: require('./project.controller'),
|
||||||
image: require('./image.controller')
|
image: require('./image.controller'),
|
||||||
|
post: require('./post.controller')
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
const db = require('../../models'),
|
||||||
|
errorMaker = require('../helpers/error.maker'),
|
||||||
|
textCleaner = require('../helpers/text_cleaner');
|
||||||
|
|
||||||
|
const { Post, sequelize } = db;
|
||||||
|
|
||||||
|
exports.createStatusUpdate = async (req, res, next) => {
|
||||||
|
const { text } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const organization_id = req.user.id;
|
||||||
|
console.log(organization_id);
|
||||||
|
|
||||||
|
const post = await Post.create({ text, organization_id });
|
||||||
|
|
||||||
|
return res.status(201).json(post);
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// query params: {postId}
|
||||||
|
exports.addRibbon = async (req, res, next) => {
|
||||||
|
const { update_id } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const posts = await sequelize.query(
|
||||||
|
`
|
||||||
|
SELECT num_ribbons
|
||||||
|
FROM posts
|
||||||
|
WHERE id = :update_id`,
|
||||||
|
{ type: db.Sequelize.QueryTypes.SELECT, replacements: { update_id } }
|
||||||
|
);
|
||||||
|
|
||||||
|
const newNumRibbons = posts[0].num_ribbons + 1;
|
||||||
|
|
||||||
|
const update = await sequelize.query(
|
||||||
|
`
|
||||||
|
UPDATE posts
|
||||||
|
SET num_ribbons = :newNumRibbons
|
||||||
|
WHERE id = :update_id`,
|
||||||
|
{
|
||||||
|
type: db.Sequelize.QueryTypes.UPDATE,
|
||||||
|
replacements: { newNumRibbons, update_id }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ message: 'Successfully added ribbon to post!' });
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -8,7 +8,8 @@ const {
|
|||||||
grant,
|
grant,
|
||||||
stripe,
|
stripe,
|
||||||
organization,
|
organization,
|
||||||
charge
|
charge,
|
||||||
|
post
|
||||||
} = require('../controllers');
|
} = require('../controllers');
|
||||||
|
|
||||||
// POST donor signup
|
// POST donor signup
|
||||||
@@ -54,6 +55,12 @@ router.get('/dashboard', checkAuth(roles.DONOR), donor.getDashboardData);
|
|||||||
|
|
||||||
router.get('/charges', checkAuth(roles.DONOR), charge.getAllChargesbyDonor);
|
router.get('/charges', checkAuth(roles.DONOR), charge.getAllChargesbyDonor);
|
||||||
|
|
||||||
|
router.put(
|
||||||
|
'/statusupdate/ribbon/:update_id',
|
||||||
|
checkAuth(roles.DONOR),
|
||||||
|
post.addRibbon
|
||||||
|
);
|
||||||
|
|
||||||
// // Retrieve a single Donor by Id
|
// // Retrieve a single Donor by Id
|
||||||
// router.get('/:donor_id', donor.findById);
|
// router.get('/:donor_id', donor.findById);
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ const {
|
|||||||
region,
|
region,
|
||||||
stripe,
|
stripe,
|
||||||
project,
|
project,
|
||||||
image
|
image,
|
||||||
|
post
|
||||||
} = require('../controllers');
|
} = require('../controllers');
|
||||||
|
|
||||||
// Charity signup route
|
// Charity signup route
|
||||||
@@ -34,6 +35,12 @@ router.get(
|
|||||||
|
|
||||||
router.post('/project', checkAuth(roles.ORGANIZATION), project.createProject);
|
router.post('/project', checkAuth(roles.ORGANIZATION), project.createProject);
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
'/statusupdate',
|
||||||
|
checkAuth(roles.ORGANIZATION),
|
||||||
|
post.createStatusUpdate
|
||||||
|
);
|
||||||
|
|
||||||
// // Add cause
|
// // Add cause
|
||||||
// router.post('/cause', checkAuth(roles.ORGANIZATION), cause.createCause);
|
// router.post('/cause', checkAuth(roles.ORGANIZATION), cause.createCause);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
const Organization = require('./Organization');
|
||||||
|
|
||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
const Post = sequelize.define('Post', {
|
||||||
|
id: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
primaryKey: true,
|
||||||
|
autoIncrement: true
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
type: DataTypes.STRING(350),
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
organization_id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
allowNull: false,
|
||||||
|
references: {
|
||||||
|
model: Organization(sequelize, DataTypes),
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
num_ribbons: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
defaultValue: 0
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Post;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user