aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/search.service.ts
blob: 6c16efd5471d66d139ed9a7be19fe0550e5654b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
    }
  }
}