<?php

namespace App\Services;

use App\Repositories\Contact;

class ContactService
{
    private $contactRepository;

    public function __construct(Contact $contactRepository)
    {
        $this->contactRepository = $contactRepository;
    }

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

        return $contact;
    }

    public function delete($id)
    {
        $contact = $this->contactRepository->findOrFail($id);
        $contact->delete();
    }

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

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

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

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