import { Topping, ChangeToppingDto, } from "../generated/graphql";
import { ToppingList } from "../data/topping-list";
import { v4 as uuidv4 } from 'uuid';
import { EEXIST } from "constants";
export class ToppingResolver {
    static getById = (id: string): Topping => {
        return ToppingList.filter(topping => topping.id === id)[0];
    };

    static getByName = (name: string): Topping => {
        return ToppingList.filter(topping => topping.name === name)[0];
    };

    static list = (): Topping[] => {
        return ToppingList;
    };

    static create = (toppingCreateDto: ChangeToppingDto): Topping => {
        const topping: Topping = {
            id: uuidv4(),
            name: toppingCreateDto.name,
        }
        console.log(`Create Topping ...`, topping)
        return topping;
    };


    static update = (toppingId: string, toppingUpdateDto: ChangeToppingDto): Topping => {
        const topping = ToppingResolver.getById(toppingId);
        if (!topping) {
            throw new Error(
                `No Topping found with id ${toppingId}`
            )
        }
        topping.name = toppingUpdateDto.name; 
        console.log(`Update Topping ...`, topping)
        return topping;
    };

}