item parents

master
Lauren Liberda 2021-07-08 23:58:07 +02:00
parent f500924350
commit c40d132584
5 changed files with 58 additions and 7 deletions

View File

@ -7,4 +7,7 @@ export class NewItemInput {
@Field({ nullable: true })
notes?: string;
@Field((type) => ID, { nullable: true })
parent?: number;
}

View File

@ -1,13 +1,29 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import {
Column,
Entity,
PrimaryGeneratedColumn,
Tree,
TreeChildren,
TreeParent,
} from 'typeorm';
@Entity()
@Tree('closure-table', {
closureTableName: 'item_closure',
})
export class Item {
@PrimaryGeneratedColumn('increment')
id: number;
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
id: string;
@Column()
name: string;
@Column({ nullable: true })
notes?: string;
@TreeParent()
parent?: Item;
@TreeChildren()
children: Item[];
}

View File

@ -12,4 +12,7 @@ export class ItemModel {
@Field({ nullable: true })
notes?: string;
@Field({ nullable: true })
parent?: ItemModel;
}

View File

@ -1,4 +1,12 @@
import { Args, ID, Mutation, Query, Resolver } from '@nestjs/graphql';
import {
Args,
ID,
Mutation,
Parent,
Query,
ResolveField,
Resolver,
} from '@nestjs/graphql';
import { NewItemInput } from './dto/new-item.input';
import { ItemModel } from './items.model';
import { ItemsService } from './items.service';
@ -12,6 +20,12 @@ export class ItemsResolver {
return this.itemsService.getItem(id);
}
@ResolveField()
async parent(@Parent() item: ItemModel) {
if (item.parent) return item.parent;
return this.itemsService.getItemParent(item);
}
@Mutation((returns) => ItemModel)
async createItem(@Args('itemData') itemData: NewItemInput) {
return this.itemsService.createItem(itemData);

View File

@ -1,24 +1,39 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
import { EntityManager, Repository, TreeRepository } from 'typeorm';
import { NewItemInput } from './dto/new-item.input';
import { Item } from './items.entity';
import { ItemModel } from './items.model';
@Injectable()
export class ItemsService {
constructor(
@InjectRepository(Item)
private itemRepository: Repository<Item>,
) {}
@InjectEntityManager()
private entityManager: EntityManager,
) {
this.treeRepository = this.entityManager.getTreeRepository(Item);
}
treeRepository: TreeRepository<Item>;
async getItem(id: string): Promise<Item | undefined> {
return this.itemRepository.findOne(id);
}
async getItemParent(item: ItemModel) {
const itemE = await this.itemRepository.findOneOrFail(item.id);
return (await this.treeRepository.findAncestorsTree(itemE)).parent;
}
async createItem(input: NewItemInput): Promise<Item> {
const item = new Item();
item.name = input.name;
item.notes = input.notes;
if (input.parent) {
const parent = await this.itemRepository.findOneOrFail(input.parent);
item.parent = parent;
}
return this.itemRepository.save(item);
}
}