item editing
parent
8f181bdc94
commit
0665a79dd5
|
@ -0,0 +1,16 @@
|
|||
import { Field, ID, InputType } from '@nestjs/graphql';
|
||||
|
||||
@InputType()
|
||||
export class EditItemInput {
|
||||
@Field((type) => ID)
|
||||
id: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
name?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
notes?: string;
|
||||
|
||||
@Field((type) => ID, { nullable: true })
|
||||
parent?: string;
|
||||
}
|
|
@ -7,6 +7,7 @@ import {
|
|||
ResolveField,
|
||||
Resolver,
|
||||
} from '@nestjs/graphql';
|
||||
import { EditItemInput } from './dto/edit-item.input';
|
||||
import { NewItemInput } from './dto/new-item.input';
|
||||
import { EANService } from './ean/ean.service';
|
||||
import { ItemModel, PaginatedItems } from './items.model';
|
||||
|
@ -58,6 +59,11 @@ export class ItemsResolver {
|
|||
return this.itemsService.createItem(itemData);
|
||||
}
|
||||
|
||||
@Mutation((returns) => ItemModel)
|
||||
async editItem(@Args('input') input: EditItemInput) {
|
||||
return this.itemsService.editItem(input);
|
||||
}
|
||||
|
||||
@Mutation((returns) => ItemModel)
|
||||
async deleteItem(@Args('id', { type: () => ID }) itemId: string) {
|
||||
return this.itemsService.deleteItem(itemId);
|
||||
|
|
|
@ -3,6 +3,7 @@ import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
|
|||
import { EntityManager, Repository, TreeRepository } from 'typeorm';
|
||||
import assert from 'assert';
|
||||
import { NewItemInput } from './dto/new-item.input';
|
||||
import { EditItemInput } from './dto/edit-item.input';
|
||||
import { EANService } from './ean/ean.service';
|
||||
import { Item } from './items.entity';
|
||||
import { ItemModel, PaginatedItems } from './items.model';
|
||||
|
@ -118,6 +119,23 @@ export class ItemsService {
|
|||
return item;
|
||||
}
|
||||
|
||||
async editItem(input: EditItemInput) {
|
||||
const item = await this.itemRepository.findOneOrFail(
|
||||
this.eans.toID(input.id),
|
||||
);
|
||||
const edited: Item = {
|
||||
...item,
|
||||
...input,
|
||||
// input.id can be an EAN-13
|
||||
id: item.id,
|
||||
parent: input.parent
|
||||
? await this.itemRepository.findOneOrFail(this.eans.toID(input.parent))
|
||||
: undefined,
|
||||
};
|
||||
await this.itemRepository.save(edited);
|
||||
return edited;
|
||||
}
|
||||
|
||||
async deleteItem(id: string) {
|
||||
const item = await this.itemRepository.findOne(this.eans.toID(id));
|
||||
if (!item) {
|
||||
|
|
Loading…
Reference in New Issue