All files / src/client/mock MockWriteModelApi.ts

14.81% Statements 4/27
0% Branches 0/9
0% Functions 0/17
17.39% Lines 4/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64              1x 1x     1x     1x                                                                                                  
// Package: com.lightningkite.lightningdb.mock
// Generated by Khrysalis - this file will be overwritten.
import { Condition } from '../../shared/Condition'
import { HasId } from '../../shared/HasId'
import { MassModification } from '../../shared/MassModification'
import { Modification } from '../../shared/Modification'
import { UUIDFor } from '../../shared/UUIDFor'
import { WriteModelApi } from '../WriteModelApi'
import { ItemNotFound } from './ItemNotFound'
import { MockTable } from './MockTable'
import { forEach } from 'iter-tools-es'
import { Observable, of, throwError } from 'rxjs'
 
//! Declares com.lightningkite.lightningdb.mock.MockWriteModelApi
export class MockWriteModelApi<Model extends HasId> extends WriteModelApi<Model> {
    public constructor(public readonly table: MockTable<Model>) {
        super();
    }
    
    
    public post(value: Model): Observable<Model> {
        return of(this.table.addItem(value));
    }
    
    public postBulk(values: Array<Model>): Observable<Array<Model>> {
        return of(values.map((it: Model): Model => this.table.addItem(it)));
    }
    
    public put(value: Model): Observable<Model> {
        return of(this.table.replaceItem(value));
    }
    
    public putBulk(values: Array<Model>): Observable<Array<Model>> {
        return of(values.map((it: Model): Model => this.table.replaceItem(it)));
    }
    
    public patch(id: UUIDFor<Model>, modification: Modification<Model>): Observable<Model> {
        return ((): (Observable<Model> | null) => {
            const temp6 = (this.table.data.get(id) ?? null);
            Iif(temp6 === null) { return null }
            return ((item: Model): Observable<Model> => {
                const modified = modification.invoke(item);
                this.table.replaceItem(modified);
                return of(modified);
            })(temp6)
        })() ?? throwError(new ItemNotFound(`404 item with key ${id} not found`));
    }
    
    public patchBulk(modification: MassModification<Model>): Observable<Array<Model>> {
        return of(this.table
            .asList().filter((it: Model): boolean => modification.condition.invoke(it)).map((it: Model): Model => this.table.replaceItem(modification.modification.invoke(it))));
    }
    
    public _delete(id: UUIDFor<Model>): Observable<void> {
        return of(this.table.deleteItemById(id));
    }
    
    public deleteBulk(condition: Condition<Model>): Observable<void> {
        return of(this.table
                .asList().filter((it: Model): boolean => condition.invoke(it)).forEach((it: Model): void => {
                this.table.deleteItem(it);
        }));
    }
}