-
-
Notifications
You must be signed in to change notification settings - Fork 677
/
recipe.type.ts
33 lines (25 loc) · 883 Bytes
/
recipe.type.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
import { Field, Float, Int, ObjectType } from "type-graphql";
@ObjectType({ description: "Object representing cooking recipe" })
export class Recipe {
@Field()
title: string;
@Field(_type => String, { nullable: true, deprecationReason: "Use 'description' field instead" })
get specification(): string | undefined {
return this.description;
}
@Field({ nullable: true, description: "The recipe description with preparation info" })
description?: string;
@Field(_type => [Int])
ratings: number[];
@Field()
creationDate: Date;
@Field(_type => Int)
ratingsCount: number;
@Field(_type => Float, { nullable: true })
get averageRating(): number | null {
const ratingsCount = this.ratings.length;
if (ratingsCount === 0) return null;
const ratingsSum = this.ratings.reduce((a, b) => a + b, 0);
return ratingsSum / ratingsCount;
}
}