-
-
Notifications
You must be signed in to change notification settings - Fork 675
/
recipe.service.ts
47 lines (36 loc) · 1010 Bytes
/
recipe.service.ts
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
40
41
42
43
44
45
46
47
import { Inject, Service } from "typedi";
import { type RecipeInput } from "./recipe.input";
import { Recipe } from "./recipe.type";
@Service()
export class RecipeService {
private autoIncrementValue: number;
constructor(
@Inject("SAMPLE_RECIPES")
private readonly items: Recipe[],
) {
this.autoIncrementValue = this.items.length;
}
async getAll() {
return this.items;
}
async getOne(id: string) {
return this.items.find(it => it.id === id);
}
async add(data: RecipeInput) {
const recipe = this.createRecipe(data);
this.items.push(recipe);
return recipe;
}
async findIndex(recipe: Recipe) {
return this.items.findIndex(it => it.id === recipe.id);
}
private createRecipe(recipeData: Partial<Recipe>): Recipe {
const recipe = Object.assign(new Recipe(), recipeData);
recipe.id = this.getId();
return recipe;
}
private getId(): string {
this.autoIncrementValue += 1;
return this.autoIncrementValue.toString();
}
}