Skip to content

Commit

Permalink
Merge bc11f1d into ab55f1a
Browse files Browse the repository at this point in the history
  • Loading branch information
tolulope-od authored Feb 14, 2019
2 parents ab55f1a + bc11f1d commit 53a8ce3
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eslint:
enabled: true
config_file: .eslintrc
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import bodyParser from 'body-parser';
import mealsRoute from './server/routes/mealsRoute';
import mealsRoute from './server/routes/meal.route';

// Set up the app with express
const app = express();
Expand Down
43 changes: 40 additions & 3 deletions server/controllers/meal.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,47 @@ const MealController = {
getSingleMeal(req, res) {
const { id } = req.params;
const foundMeal = MealsService.getAMeal(id);
if (Object.entries(foundMeal).length !== 0) {
return res.json({
status: 'success',
data: foundMeal,
}).status(200);
}
return res.json({
status: 'success',
data: foundMeal,
}).status(201);
status: 'Error',
message: 'No meal with that id found',
}).status(400);
},

deleteSingleMeal(req, res) {
const { id } = req.params;
const deletedMeal = MealsService.deleteAMeal(id);
if (Object.entries(deletedMeal).length !== 0) {
return res.json({
status: 'success',
data: deletedMeal,
}).status(200);
}
return res.json({
status: 'Error',
message: 'No meal with that id found',
}).status(400);
},

editAMeal(req, res) {
const { id } = req.params;
const newMeal = req.body;
const editedMeal = MealsService.editAMeal(id, newMeal);
if (Object.entries(editedMeal).length !== 0) {
return res.json({
status: 'success',
data: editedMeal,
}).status(200);
}
return res.json({
status: 'Error',
message: 'No meal with that id found',
}).status(400);
},
};

Expand Down
12 changes: 12 additions & 0 deletions server/routes/meal.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from 'express';
import MealController from '../controllers/meal.controller';

const router = Router();

router.get('/', MealController.fetAllMeals);
router.post('/', MealController.addAMeal);
router.get('/:id', MealController.getSingleMeal);
router.delete('/:id', MealController.deleteSingleMeal);
router.put('/:id', MealController.editAMeal);

export default router;
19 changes: 0 additions & 19 deletions server/routes/mealsRoute.js

This file was deleted.

28 changes: 23 additions & 5 deletions server/services/meal.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,31 @@ const MealsService = {

getAMeal(id) {
// -1 because we have out data in an array, which starts at 0
return this.fetchAllMeals()[id - 1];
return this.fetchAllMeals()[id - 1] || {};
},

delete(id) {
const meals = [...this.fetchAllMeals()];
meals.splice(meals.findIndex(meal => meal.id === id), 1);
return meals;
deleteAMeal(id) {
// eslint-disable-next-line eqeqeq
const toDelete = dummyData.meals.findIndex(meal => meal.id == id);
if (toDelete > -1) {
dummyData.meals.splice(toDelete, 1);
return dummyData;
}
return {};
},

editAMeal(id, newMeal) {
// eslint-disable-next-line eqeqeq
const index = dummyData.meals.findIndex(meal => meal.id == id);
const toEdit = dummyData.meals[index];
if (toEdit) {
toEdit.name = newMeal.name;
toEdit.price = newMeal.price;
toEdit.size = newMeal.size;

return toEdit;
}
return {};
},
};

Expand Down
82 changes: 61 additions & 21 deletions test/MealTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,46 @@ describe('Access Meals Service', () => {
price: '500',
});
});

it('Should return an empty object when an invalid id is specified', () => {
const meal = MealsService.getAMeal(6);

expect(meal).to.deep.equal({
});
});
});
describe('Delete one meal', () => {
it('Should delete one meal option based on the id and return new meals list', () => {
const meal = MealsService.delete(1);
const meal = MealsService.deleteAMeal(1);

expect(meal).to.deep.equal([
{
id: 2,
name: 'Fried Rice',
size: 'Small',
price: '400',
},
{
id: 3,
name: 'Coconut Rice',
size: 'Medium',
price: '800',
},
{
id: 4,
name: 'Basmati Rice & Beef',
size: 'Large',
price: '1500',
},
]);
expect(meal).to.deep.equal({
meals: [
{
id: 2,
name: 'Fried Rice',
size: 'Small',
price: '400',
},
{
id: 3,
name: 'Coconut Rice',
size: 'Medium',
price: '800',
},
{
id: 4,
name: 'Basmati Rice & Beef',
size: 'Large',
price: '1500',
},
],
});
});

it('Should return an empty object if a meal with an invalid id is called', () => {
const meal = MealsService.deleteAMeal(6);

expect(meal).to.deep.equal({});
});
});
describe('Add one meal', () => {
Expand All @@ -91,4 +106,29 @@ describe('Access Meals Service', () => {
);
});
});

describe('Edit an existing meal', () => {
it('Should edit an existing meal option based on the id', () => {
const newMeal = { name: 'Yamarita', size: 'Large', price: '700' };
const meal = MealsService.editAMeal(2, newMeal);

expect(meal).to.deep.equal(
{
id: 2,
name: 'Yamarita',
size: 'Large',
price: '700',
},
);
});

it('Should return an empty object if an invalid id is specified', () => {
const newMeal = { name: 'Yamarita', size: 'Large', price: '700' };
const meal = MealsService.editAMeal(6, newMeal);

expect(meal).to.deep.equal(
{},
);
});
});
});

0 comments on commit 53a8ce3

Please sign in to comment.