Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions server/src/controllers/perkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const perkSchema = Joi.object({
merchant: Joi.string().allow('')

});
const objectTitleSchema = Joi.object({
title: Joi.string().required()
});



Expand Down Expand Up @@ -57,7 +60,7 @@ export async function getAllPerks(req, res, next) {
export async function createPerk(req, res, next) {
try {
// validate request body against schema
const { value, error } = perkSchema.validate(req.body);
const { value, error } = perkSchema.validate(req.body);
if (error) return res.status(400).json({ message: error.message });
// ...value spreads the validated fields
const doc = await Perk.create({ ...value});
Expand All @@ -70,10 +73,18 @@ export async function createPerk(req, res, next) {
// TODO
// Update an existing perk by ID and validate only the fields that are being updated
export async function updatePerk(req, res, next) {

try {
const { id } = req.params;
const { title } = req.body;
if (!title) return res.status(400).json({ message: 'Title field is required' });
const { value, error } = perkSchema.validate(req.body);
if (error) return res.status(400).json({ message: error.message });
const doc = await Perk.findByIdAndUpdate(id, { ...value });
if (!doc) return res.status(404).json({ message: 'Perk not found' });
res.json({ perk: doc });
} catch (err) { next(err);
}
}


// Delete a perk by ID
export async function deletePerk(req, res, next) {
try {
Expand Down