<?php

namespace App\Services;

use App\Repositories\Summary;

class SummaryService
{
    private $summaryRepository;

    public function __construct(Summary $summaryRepository)
    {
        $this->summaryRepository = $summaryRepository;
    }

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

        return $summary;
    }

    public function delete($id)
    {
        $summary = $this->summaryRepository->findOrFail($id);
        $summary->delete();
    }

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

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

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

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