aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/angular-small-apps/apps/recipes/src/app/shared/services
diff options
context:
space:
mode:
Diffstat (limited to 'public/projects/angular-small-apps/apps/recipes/src/app/shared/services')
-rw-r--r--public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.spec.ts16
-rw-r--r--public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.ts39
-rw-r--r--public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.spec.ts16
-rw-r--r--public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.ts46
-rw-r--r--public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.spec.ts16
-rw-r--r--public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.ts39
6 files changed, 172 insertions, 0 deletions
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.spec.ts b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.spec.ts
new file mode 100644
index 0000000..ba1dbd4
--- /dev/null
+++ b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.spec.ts
@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { LocalStorageService } from './local-storage.service';
+
+describe('LocalStorageService', () => {
+ let service: LocalStorageService;
+
+ beforeEach(() => {
+ TestBed.configureTestingModule({});
+ service = TestBed.inject(LocalStorageService);
+ });
+
+ it('should be created', () => {
+ expect(service).toBeTruthy();
+ });
+});
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.ts b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.ts
new file mode 100644
index 0000000..82a72e7
--- /dev/null
+++ b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.ts
@@ -0,0 +1,39 @@
+import { Injectable } from '@angular/core';
+
+@Injectable({
+ providedIn: 'root',
+})
+export class LocalStorageService {
+ constructor() {}
+
+ get(key: string): any {
+ try {
+ const serialItem = localStorage.getItem(key);
+ if (serialItem) {
+ return JSON.parse(serialItem);
+ } else {
+ return undefined;
+ }
+ } catch (e) {
+ console.log(e);
+ return undefined;
+ }
+ }
+
+ set(key: string, value: any) {
+ try {
+ const serialItem = JSON.stringify(value);
+ localStorage.setItem(key, serialItem);
+ } catch (e) {
+ console.log(e);
+ }
+ }
+
+ remove(key: string) {
+ localStorage.removeItem(key);
+ }
+
+ clear() {
+ localStorage.clear;
+ }
+}
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.spec.ts b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.spec.ts
new file mode 100644
index 0000000..e433b4e
--- /dev/null
+++ b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.spec.ts
@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { RecipesService } from './recipes.service';
+
+describe('RecipesService', () => {
+ let service: RecipesService;
+
+ beforeEach(() => {
+ TestBed.configureTestingModule({});
+ service = TestBed.inject(RecipesService);
+ });
+
+ it('should be created', () => {
+ expect(service).toBeTruthy();
+ });
+});
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.ts b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.ts
new file mode 100644
index 0000000..a82e019
--- /dev/null
+++ b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/recipes.service.ts
@@ -0,0 +1,46 @@
+import { HttpClient, HttpHeaders } from '@angular/common/http';
+import { Injectable } from '@angular/core';
+import { Observable } from 'rxjs';
+import { Recipes } from '../recipes';
+
+@Injectable({
+ providedIn: 'root',
+})
+export class RecipesService {
+ private allRecipesAPI =
+ 'https://www.themealdb.com/api/json/v1/1/search.php?f=a';
+ private recipeByIdAPI =
+ 'https://www.themealdb.com/api/json/v1/1/lookup.php?i=';
+ private recipeByNameAPI =
+ 'https://www.themealdb.com/api/json/v1/1/search.php?s=';
+ private recipeByIngredientAPI =
+ 'https://www.themealdb.com/api/json/v1/1/filter.php?i=';
+ private recipeByCategoryAPI =
+ 'https://www.themealdb.com/api/json/v1/1/filter.php?c=';
+
+ httpOptions = {
+ headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
+ };
+
+ constructor(private http: HttpClient) {}
+
+ getAllRecipes(): Observable<Recipes[]> {
+ return this.http.get<Recipes[]>(this.allRecipesAPI);
+ }
+
+ getRecipeById(id: number): Observable<Recipes[]> {
+ return this.http.get<Recipes[]>(this.recipeByIdAPI + id);
+ }
+
+ getRecipeByName(name: string): Observable<Recipes[]> {
+ return this.http.get<Recipes[]>(this.recipeByNameAPI + name);
+ }
+
+ getRecipeByIngredient(ingredient: string): Observable<Recipes[]> {
+ return this.http.get<Recipes[]>(this.recipeByIngredientAPI + ingredient);
+ }
+
+ getRecipeByCategory(category: string): Observable<Recipes[]> {
+ return this.http.get<Recipes[]>(this.recipeByCategoryAPI + category);
+ }
+}
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.spec.ts b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.spec.ts
new file mode 100644
index 0000000..23c42c7
--- /dev/null
+++ b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.spec.ts
@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { SearchService } from './search.service';
+
+describe('SearchService', () => {
+ let service: SearchService;
+
+ beforeEach(() => {
+ TestBed.configureTestingModule({});
+ service = TestBed.inject(SearchService);
+ });
+
+ it('should be created', () => {
+ expect(service).toBeTruthy();
+ });
+});
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.ts b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.ts
new file mode 100644
index 0000000..6c16efd
--- /dev/null
+++ b/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.ts
@@ -0,0 +1,39 @@
+import { Injectable } from '@angular/core';
+import { BehaviorSubject } from 'rxjs';
+import { Recipes } from '../recipes';
+import { RecipesService } from './recipes.service';
+
+@Injectable({
+ providedIn: 'root',
+})
+export class SearchService {
+ private results = new BehaviorSubject<Recipes[]>([]);
+
+ constructor(private recipes: RecipesService) {}
+
+ getResults() {
+ return this.results.asObservable();
+ }
+
+ findResults(query: string, by: string) {
+ switch (by) {
+ case 'name':
+ this.recipes
+ .getRecipeByName(query)
+ .subscribe((recipes: any) => this.results.next(recipes.meals));
+ break;
+ case 'ingredient':
+ this.recipes
+ .getRecipeByIngredient(query)
+ .subscribe((recipes: any) => this.results.next(recipes.meals));
+ break;
+ case 'category':
+ this.recipes
+ .getRecipeByCategory(query)
+ .subscribe((recipes: any) => this.results.next(recipes.meals));
+ break;
+ default:
+ break;
+ }
+ }
+}