<?php

namespace App\Services;

use App\Repositories\FilterItem;

class FilterItemService
{
    private $filteritemRepository;

    public function __construct(FilterItem $filteritemRepository)
    {
        $this->filteritemRepository = $filteritemRepository;
    }

    public function save(array $data)
    {
        if (isset($data['id'])) {
            $filteritem = $this->filteritemRepository->findOrFail($data['id']);
            $filteritem->update($data);
        } else {
            $filteritem = $this->filteritemRepository->create($data);
        }

        return $filteritem;
    }

    public function delete($id)
    {
        $filteritem = $this->filteritemRepository->findOrFail($id);
        $filteritem->delete();
    }

    public function get($id)
    {
        return $this->filteritemRepository->findOrFail($id);
    }

    public function getAll()
    {
        return $this->filteritemRepository->all();
    }

    public function with(array $children)
    {
        return $this->filteritemRepository->with($children)->get();
    }

    public function getOneWith($id, array $children)
    {
        return $this->filteritemRepository->with($children)->findOrFail($id);
    }
}