-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds ShoppingList and related models. See #5
- Loading branch information
Showing
14 changed files
with
169 additions
and
14 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import { Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Field, ID, ObjectType } from 'type-graphql'; | ||
|
||
import { FridgeIngredient } from './FridgeIngredient'; | ||
import { | ||
FridgeIngredientsConnection, | ||
FridgeIngredientsConnectionEdge, | ||
} from './FridgeIngredientsConnection'; | ||
|
||
import { FridgeIngredient } from './FridgeIngredient'; | ||
|
||
/** | ||
* @NOTE: Having a separate fridge that is not part of the user allows us to have a separate | ||
* resolver. | ||
*/ | ||
@Entity() | ||
@ObjectType() | ||
export class Fridge { | ||
@Field(type => ID) | ||
@Field(() => ID) | ||
@PrimaryGeneratedColumn() | ||
public readonly id!: number; | ||
|
||
@Field(type => FridgeIngredientsConnection) | ||
@OneToMany(type => FridgeIngredient, fridgeIngredients => fridgeIngredients.fridge) | ||
@Field(() => FridgeIngredientsConnection) | ||
@OneToMany(() => FridgeIngredient, fridgeIngredients => fridgeIngredients.fridge) | ||
public ingredients!: Promise<FridgeIngredient[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Field, ID, ObjectType } from 'type-graphql'; | ||
|
||
import { ShoppingListItem } from './ShoppingListItem'; | ||
import { ShoppingListItemsConnection } from './ShoppingListItemsConnection'; | ||
|
||
/** | ||
* @NOTE: Having a separate shopping list that is not a property of the user allows us to have a | ||
* separate resolver. | ||
*/ | ||
@Entity() | ||
@ObjectType() | ||
export class ShoppingList { | ||
@Field(() => ID) | ||
@PrimaryGeneratedColumn() | ||
public readonly id!: number; | ||
|
||
@Field(() => ShoppingListItemsConnection) | ||
@OneToMany(() => ShoppingListItem, shoppingListItem => shoppingListItem.shoppingList) | ||
public items!: Promise<ShoppingListItem[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Field, ObjectType } from 'type-graphql'; | ||
|
||
import { Ingredient } from '../ingredient/Ingredient'; | ||
import { ShoppingList } from './ShoppingList'; | ||
import { UNITS } from '../fridge/Units'; | ||
|
||
@Entity() | ||
@ObjectType() | ||
export class ShoppingListItem { | ||
@PrimaryGeneratedColumn() | ||
public readonly id!: number; | ||
|
||
@ManyToOne(() => ShoppingList, shoppingList => shoppingList.items) | ||
public shoppingList!: Promise<ShoppingList>; | ||
|
||
@Field(() => Ingredient) | ||
@ManyToOne(() => Ingredient, ingredient => ingredient.fridgeIngredients) | ||
public item!: Promise<Ingredient>; | ||
|
||
@Field() | ||
@Column({ enum: UNITS }) | ||
public unit!: string; | ||
|
||
@Field() | ||
@Column() | ||
public amount!: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Connection, Edge } from '../graphql/connections'; | ||
import { Field, ObjectType } from 'type-graphql'; | ||
|
||
import { ShoppingListItem } from './ShoppingListItem'; | ||
|
||
@ObjectType() | ||
export class ShoppingListItemsConnectionEdge extends Edge<ShoppingListItem> { | ||
@Field(() => ShoppingListItem) | ||
public node!: ShoppingListItem; | ||
} | ||
|
||
@ObjectType() | ||
export class ShoppingListItemsConnection extends Connection<ShoppingListItem> { | ||
@Field(() => ShoppingListItemsConnectionEdge) | ||
public edges!: Edge<ShoppingListItem>[]; | ||
} | ||
|
||
export function createShoppingListItemsEdge( | ||
node: ShoppingListItem, | ||
): ShoppingListItemsConnectionEdge { | ||
const edge = new ShoppingListItemsConnectionEdge(); | ||
edge.node = node; | ||
edge.cursor = [ShoppingListItemsConnectionEdge.name, node.id].join('_'); | ||
return edge; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
Args, | ||
ArgsType, | ||
Authorized, | ||
Ctx, | ||
Field, | ||
Mutation, | ||
ObjectType, | ||
Resolver, | ||
} from 'type-graphql'; | ||
import { IsIn, IsNumber, MinLength } from 'class-validator'; | ||
|
||
import { InjectRepository } from 'typeorm-typedi-extensions'; | ||
import { Repository } from 'typeorm'; | ||
import { Service } from 'typedi'; | ||
import { ShoppingListItem } from './ShoppingListItem'; | ||
import { ShoppingListItemsConnectionEdge } from './ShoppingListItemsConnection'; | ||
import { UNITS } from '../fridge/Units'; | ||
import { User } from '../user/User'; | ||
|
||
@ArgsType() | ||
class AddItemArgs { | ||
@Field() | ||
@MinLength(1) | ||
public readonly name!: string; | ||
|
||
@Field() | ||
@IsIn(UNITS) | ||
public readonly unit!: string; | ||
|
||
@Field() | ||
@IsNumber() | ||
public readonly amount!: number; | ||
} | ||
|
||
@ObjectType() | ||
class AddItemResponse { | ||
@Field() | ||
public shoppingListItemsConnectionEdge!: ShoppingListItemsConnectionEdge; | ||
} | ||
|
||
@Service() | ||
@Resolver() | ||
export class ShoppingListResolver { | ||
@InjectRepository(() => ShoppingListItem) | ||
private readonly shoppingListItemRepo!: Repository<ShoppingListItem>; | ||
|
||
@Mutation(() => AddItemResponse) | ||
@Authorized() | ||
public async addItem( | ||
@Args() addItem: AddItemArgs, | ||
@Ctx('user') user: User, | ||
): Promise<AddItemResponse> { | ||
// let ShoppingListItem; | ||
throw new Error('Not implemented'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters