item parents
This commit is contained in:
parent
f500924350
commit
c40d132584
|
@ -7,4 +7,7 @@ export class NewItemInput {
|
||||||
|
|
||||||
@Field({ nullable: true })
|
@Field({ nullable: true })
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
|
@Field((type) => ID, { nullable: true })
|
||||||
|
parent?: number;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,29 @@
|
||||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
import {
|
||||||
|
Column,
|
||||||
|
Entity,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
Tree,
|
||||||
|
TreeChildren,
|
||||||
|
TreeParent,
|
||||||
|
} from 'typeorm';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
|
@Tree('closure-table', {
|
||||||
|
closureTableName: 'item_closure',
|
||||||
|
})
|
||||||
export class Item {
|
export class Item {
|
||||||
@PrimaryGeneratedColumn('increment')
|
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
|
||||||
id: number;
|
id: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
|
@TreeParent()
|
||||||
|
parent?: Item;
|
||||||
|
|
||||||
|
@TreeChildren()
|
||||||
|
children: Item[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,7 @@ export class ItemModel {
|
||||||
|
|
||||||
@Field({ nullable: true })
|
@Field({ nullable: true })
|
||||||
notes?: string;
|
notes?: string;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
parent?: ItemModel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 { NewItemInput } from './dto/new-item.input';
|
||||||
import { ItemModel } from './items.model';
|
import { ItemModel } from './items.model';
|
||||||
import { ItemsService } from './items.service';
|
import { ItemsService } from './items.service';
|
||||||
|
@ -12,6 +20,12 @@ export class ItemsResolver {
|
||||||
return this.itemsService.getItem(id);
|
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)
|
@Mutation((returns) => ItemModel)
|
||||||
async createItem(@Args('itemData') itemData: NewItemInput) {
|
async createItem(@Args('itemData') itemData: NewItemInput) {
|
||||||
return this.itemsService.createItem(itemData);
|
return this.itemsService.createItem(itemData);
|
||||||
|
|
|
@ -1,24 +1,39 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { EntityManager, Repository, TreeRepository } from 'typeorm';
|
||||||
import { NewItemInput } from './dto/new-item.input';
|
import { NewItemInput } from './dto/new-item.input';
|
||||||
import { Item } from './items.entity';
|
import { Item } from './items.entity';
|
||||||
|
import { ItemModel } from './items.model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ItemsService {
|
export class ItemsService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Item)
|
@InjectRepository(Item)
|
||||||
private itemRepository: Repository<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> {
|
async getItem(id: string): Promise<Item | undefined> {
|
||||||
return this.itemRepository.findOne(id);
|
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> {
|
async createItem(input: NewItemInput): Promise<Item> {
|
||||||
const item = new Item();
|
const item = new Item();
|
||||||
item.name = input.name;
|
item.name = input.name;
|
||||||
item.notes = input.notes;
|
item.notes = input.notes;
|
||||||
|
if (input.parent) {
|
||||||
|
const parent = await this.itemRepository.findOneOrFail(input.parent);
|
||||||
|
item.parent = parent;
|
||||||
|
}
|
||||||
return this.itemRepository.save(item);
|
return this.itemRepository.save(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue