Skip to content

Commit

Permalink
change indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkaterina committed Mar 17, 2024
1 parent 42b3c33 commit 954c4c8
Show file tree
Hide file tree
Showing 5 changed files with 650 additions and 650 deletions.
260 changes: 130 additions & 130 deletions functions/db-controllers/food-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,143 +5,143 @@ const Food = require('../models/food');
const User = require('../models/user');

const getFoods = async (req, res, next) => {
const userId = req.userData.userId;

let userWithFood;
try {
userWithFood = await User.findById(userId).populate('foods');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not find food. Try again later.', 500
);
return next(error);
}

if (!userWithFood) {
console.error('Could not find user by id');
return next(
new HttpError('Could not find food. Try again later.', 404)
);
}

res.json({
foods: userWithFood.foods.map(food =>
food.toObject({ getters: true })
)
});
const userId = req.userData.userId;

let userWithFood;
try {
userWithFood = await User.findById(userId).populate('foods');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not find food. Try again later.', 500
);
return next(error);
}

if (!userWithFood) {
console.error('Could not find user by id');
return next(
new HttpError('Could not find food. Try again later.', 404)
);
}

res.json({
foods: userWithFood.foods.map(food =>
food.toObject({ getters: true })
)
});
};

const createFood = async (req, res, next) => {

const { food } = req.body;

const createdFood = new Food({
food,
creator: req.userData.userId
});

let user;
try {
user = await User.findById(req.userData.userId);
} catch (err) {
console.error(err);
const error = new HttpError('Could not add food to favorites. Try again later.', 500);
return next(error);
}

if (!user) {
console.error('Could not find user by id');
const error = new HttpError('Could not add food to favorites. Try again later.', 404);
return next(error);
}

let existingFood
try {
existingFood = await Food.findOne({ "food.food.label": food.food.label, "creator": req.userData.userId})
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add food to favorites. Try again later.',
500
);
return next(error);
}

if (existingFood) {
const error = new HttpError(
'Food exists already.',
422
);
return next(error);
}

try {
const sess = await mongoose.startSession();
sess.startTransaction();
await createdFood.save({ session: sess });
user.foods.push(createdFood);
await user.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add food to favorites. Try again later.',
500
);
return next(error);
}

res.status(201).json({ food: createdFood });
const { food } = req.body;

const createdFood = new Food({
food,
creator: req.userData.userId
});

let user;
try {
user = await User.findById(req.userData.userId);
} catch (err) {
console.error(err);
const error = new HttpError('Could not add food to favorites. Try again later.', 500);
return next(error);
}

if (!user) {
console.error('Could not find user by id');
const error = new HttpError('Could not add food to favorites. Try again later.', 404);
return next(error);
}

let existingFood
try {
existingFood = await Food.findOne({ "food.food.label": food.food.label, "creator": req.userData.userId})
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add food to favorites. Try again later.',
500
);
return next(error);
}

if (existingFood) {
const error = new HttpError(
'Food exists already.',
422
);
return next(error);
}

try {
const sess = await mongoose.startSession();
sess.startTransaction();
await createdFood.save({ session: sess });
user.foods.push(createdFood);
await user.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add food to favorites. Try again later.',
500
);
return next(error);
}

res.status(201).json({ food: createdFood });
};

const deleteFood = async (req, res, next) => {
const foodId = req.params.pid;

let food;
try {
food = await Food.findById(foodId).populate('creator');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete food. Try again later.',
500
);
return next(error);
}

if (!food) {
console.error('Could not find food by id');
const error = new HttpError('Could not delete food. Try again later.', 404);
return next(error);
}

if (food.creator.id !== req.userData.userId) {
console.error('Not authorized')
const error = new HttpError(
'You are not allowed to delete this food.',
401
);
return next(error);
}

try {
const sess = await mongoose.startSession();
sess.startTransaction();
await food.deleteOne({ session: sess });
food.creator.foods.pull(food);
await food.creator.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete food. Try again later.',
500
);
return next(error);
}

res.status(200).json({ message: 'Deleted food.' });
const foodId = req.params.pid;

let food;
try {
food = await Food.findById(foodId).populate('creator');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete food. Try again later.',
500
);
return next(error);
}

if (!food) {
console.error('Could not find food by id');
const error = new HttpError('Could not delete food. Try again later.', 404);
return next(error);
}

if (food.creator.id !== req.userData.userId) {
console.error('Not authorized')
const error = new HttpError(
'You are not allowed to delete this food.',
401
);
return next(error);
}

try {
const sess = await mongoose.startSession();
sess.startTransaction();
await food.deleteOne({ session: sess });
food.creator.foods.pull(food);
await food.creator.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete food. Try again later.',
500
);
return next(error);
}

res.status(200).json({ message: 'Deleted food.' });
};

exports.getFoods = getFoods;
Expand Down
Loading

0 comments on commit 954c4c8

Please sign in to comment.