<?php

namespace App\Services;

use App\Repositories\Address;

class AddressService
{
    private $addressRepository;

    public function __construct(Address $addressRepository)
    {
        $this->addressRepository = $addressRepository;
    }

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

        return $address;
    }

    public function delete($id)
    {
        $address = $this->addressRepository->findOrFail($id);
        $address->delete();
    }

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

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

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

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