<?php

namespace App\Services;

use App\Repositories\{{modelName}};

class {{modelName}}Service
{
    private ${{modelNameSingularLowerCase}}Repository;

    public function __construct({{modelName}} ${{modelNameSingularLowerCase}}Repository)
    {
        $this->{{modelNameSingularLowerCase}}Repository = ${{modelNameSingularLowerCase}}Repository;
    }

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

        return ${{modelNameSingularLowerCase}};
    }

    public function delete($id)
    {
        ${{modelNameSingularLowerCase}} = $this->{{modelNameSingularLowerCase}}Repository->findOrFail($id);
        ${{modelNameSingularLowerCase}}->delete();
    }

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

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

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

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