Newer
Older
ServerGenerator / src / ExampleOutput / Model.ts
@peter peter on 12 Sep 2020 763 bytes Added create operation.
export interface Graph {
    nodes: GraphNode[];
    nextId: number;
}

export interface GraphNodeInput {
    name: string;
    description: string;
}

export interface GraphNode extends GraphNodeInput {
    id: number;
}

export interface GraphNodePayload extends GraphNode {
    ref: string;
}

export function toPayload(node: GraphNode): GraphNodePayload {
    return {
        ...node,
        ref: `/nodes/${node.id}`,
    }
}

export function findSingle(graph: Graph, id: number) {
    return graph.nodes.find(n => n.id === id);
}

export function append(collection: Graph, item: GraphNodeInput): GraphNode {
    const newItem: GraphNode = {...item, id: collection.nextId};
    collection.nextId++;
    collection.nodes.push(newItem);
    return newItem;
}