import 'reflect-metadata' ; import { Arg , Int , Mutation , Query } from 'type-graphql' ; import { Predicate } from '../../dal/Table' ; import { BookStore } from '../model/BookStore' ; import { delay } from '../../common/Delay' ; import { bookStoreTable , TBookStore } from '../../dal/BookStoreTable' ; import { BookStoreInput } from '../model/input/BookStoreInput' ; import { bookTable } from '../../dal/BookTable' ; import { compare } from '../../common/Comparator' ; export class BookStoreSerice { @ Query ( ( ) => [ BookStore ] ) async findBookStores ( @ Arg ( "name" , ( ) => String , { nullable : true } ) name ? ) { await delay ( 1000 ) ; const lowercaseName = name ?. toLocaleLowerCase ( ) ; const predicate = lowercaseName !== undefined && lowercaseName !== "" ? d => d . name . toLowerCase ( ) . indexOf ( lowercaseName ) !== - 1 : undefined ; return bookStoreTable . find ( [ ] , predicate ) . map ( row => new BookStore ( row ) ) . sort ( ( a , b ) => compare ( a . name , b . name ) ) ; } @ Mutation ( ( ) => BookStore ) async mergeBookStore ( @ Arg ( "input" , ( ) => BookStoreInput ) input ) { await delay ( 1000 ) ; bookStoreTable . insert ( input , true ) ; for ( const oldBook of bookTable . findByProp ( "storeId" , input . id ) ) { bookTable . update ( { ... oldBook , storeId : undefined } ) ; } for ( const newBook of bookTable . findByIds ( input . bookIds ) ) { bookTable . update ( { ... newBook , storeId : input . id } ) ; } return new BookStore ( input ) ; } @ Mutation ( ( ) => String , { nullable : true } ) async deleteBookStore ( @ Arg ( "id" , ( ) => String ) id ) { await delay ( 1000 ) ; for ( const oldBook of bookTable . findByProp ( "storeId" , id ) ) { bookTable . update ( { ... oldBook , storeId : undefined } ) ; } return bookStoreTable . delete ( id ) !== 0 ? id : undefined ; } }