Skip to content

Commit

Permalink
Interfaces, classes and services
Browse files Browse the repository at this point in the history
  • Loading branch information
yisus82 committed Feb 7, 2020
1 parent 3d2e677 commit 4fe89a7
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 38 deletions.
2 changes: 1 addition & 1 deletion recipe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"tslint": "~5.15.0",
"typescript": "~3.5.3"
}
}
}
35 changes: 0 additions & 35 deletions recipe/src/app/app.component.spec.ts

This file was deleted.

22 changes: 22 additions & 0 deletions recipe/src/app/classes/recipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IRecipe } from '../interfaces/recipe';
import { IIngredient } from './../interfaces/ingredient';

export class Recipe implements IRecipe {
id: number;
title: string;
description: string;
serves: string;
imageUrl: string;
ingredients: IIngredient[];
instructions: string[];

constructor({ id, title, description, serves, imageUrl, ingredients, instructions }) {
this.id = id;
this.title = title;
this.description = description;
this.serves = serves;
this.imageUrl = imageUrl;
this.ingredients = ingredients;
this.instructions = instructions;
}
}
4 changes: 4 additions & 0 deletions recipe/src/app/interfaces/ingredient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IIngredient {
amount: string;
name: string;
}
11 changes: 11 additions & 0 deletions recipe/src/app/interfaces/recipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IIngredient } from './ingredient';

export interface IRecipe {
id: number;
title: string;
description: string;
serves: string;
imageUrl: string;
ingredients: IIngredient[];
instructions: string[];
}
67 changes: 67 additions & 0 deletions recipe/src/app/services/recipe.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Recipe } from './../classes/recipe';
import { IIngredient } from './../interfaces/ingredient';
import { Injectable } from '@angular/core';
import * as recipeData from '../../data.json';

@Injectable()
export class RecipeService {
private recipes: Recipe[] = [];

constructor() {
recipeData.recipes.forEach(recipe => {
this.recipes.push(new Recipe(recipe));
});
}

public getRecipes(): Recipe[] {
return this.recipes;
}

public getRecipeById(id: number): Recipe {
return this.recipes.find(recipe => recipe.id === id);
}

public createRecipe(
title: string,
description: string,
serves: string,
imageUrl: string,
ingredients: IIngredient[],
instructions: string[]
) {
const newRecipeData = {
id: this.getNextId(),
title,
description,
serves,
imageUrl,
ingredients: [...ingredients],
instructions: [...instructions]
};

const newRecipe = new Recipe(newRecipeData);

this.recipes.push(newRecipe);
return newRecipe;
}

public updateRecipe(recipe: Recipe): Recipe {
const recipeIndex = this.recipes.findIndex(r => r.id === recipe.id);

this.recipes[recipeIndex] = recipe;
return recipe;
}

public deleteRecipe(id: number): void {
const recipeIndex = this.recipes.findIndex(r => r.id === id);

if (recipeIndex !== -1) {
this.recipes.splice(recipeIndex, 1);
}
}

private getNextId(): number {
const max = Math.max.apply(null, this.recipes.map(recipe => recipe.id));
return max + 1;
}
}
Binary file added recipe/src/assets/cookies.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recipe/src/assets/food-background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recipe/src/assets/nachos.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added recipe/src/assets/salad.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions recipe/src/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"recipes": [
{
"id": 1,
"title": "Best Salad in the World",
"description": "This is a recipe for the best tasting salad in the world. It has lots of carrots and lettuce!",
"serves": "2 people",
"imageUrl": "assets/salad.jpg",
"ingredients": [
{
"amount": "1 head",
"name": "lettuce"
},
{
"amount": "10",
"name": "carrots"
},
{
"amount": "2 tablespoons",
"name": "ranch dressing"
}
],
"instructions": [
"remove lettuce leaves",
"add lettuce leaves to a large bowl",
"then add the carrots the bowl",
"add the ranch dressing",
"then mix ingredients together in the bowl"
]
},
{
"id": 2,
"title": "Chocolate Chip Cookies",
"description": "This was a recipe handed down to me by my grandmother Ruth who use to make the best chocolate chip cookies!",
"serves": "4 people",
"imageUrl": "assets/cookies.jpg",
"ingredients": [
{
"amount": "1 bag",
"name": "chocolate chips"
},
{
"amount": "2",
"name": "eggs"
},
{
"amount": "1 cup",
"name": "flour"
},
{
"amount": "1 cup",
"name": "sugar"
}
],
"instructions": [
"get a large bowl",
"crack eggs into the bowl",
"add the rest of the ingredients to the bowl",
"mix ingredients with a whisk",
"then evenly place small clumps of cookie dough on a baking sheet",
"place baking sheet in oven for 12 minutes at 350 degrees F"
]
},
{
"id": 3,
"title": "Cinco de Nachos",
"description": "I discovered this recipe when I went on a trip to Mexico. A nice lady taught me how to make world famous nachos.",
"serves": "2 adults or 4 kids",
"imageUrl": "assets/nachos.jpg",
"ingredients": [
{
"amount": "1 bag",
"name": "tortilla chips"
},
{
"amount": "1 pound",
"name": "shredded cheese"
},
{
"amount": "1 can",
"name": "refried beans"
}
],
"instructions": [
"spread tortilla chips on a large plate",
"warm up beans in a pot on the stove",
"sprinkle the shredded cheese on top of the chips",
"place plate in the oven for 10 minutes at 350 degrees F",
"finally remove plate from oven and spread refried beans on top of the chips"
]
}
]
}
6 changes: 4 additions & 2 deletions recipe/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"lib": [
"es2018",
"dom"
]
],
"resolveJsonModule": true,
"esModuleInterop": true
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
}

0 comments on commit 4fe89a7

Please sign in to comment.