<?php
/*=========================================================================================
    App Name  : PPID PTUN Kupang
    File Name : BaseRepository
    --------------------------------------------------------------------------------------
    Author    : Fuad Auliya Rohman
    Email     : fuad.dev16@gmail.com
==========================================================================================*/

namespace App\Repositories;

use App\Contracts\CrudRepositoryContract;

class BaseRepository implements CrudRepositoryContract
{
    /**
     * Find an item by ID
     *
     * @param int|mixed $id
     * @return Model|null
     */
    public function findById($id)
    {
        return $this->mainModel->find($id);
    }

    /**
     * find an item by parameter
     *
     * @param string $parameter
     * @param mixed $value
     * @return Model|null
     */
    public function findBy(string $parameter, $value)
    {
        return $this->mainModel->firstWhere($parameter, $value);
    }

    /**
     * Return all items
     *
     * @return Collection
     */
    public function all()
    {
        return $this->mainModel->all();
    }

    /**
     * Create an item
     *
     * @param array|mixed $data
     * @return Model|mixed
     */
    public function create($data)
    {
        return $this->mainModel->create($data);
    }

    /**
     * Update a model
     *
     * @param int|mixed $id
     * @param array $data
     * @return Boolean|mixed
     */
    public function update($id, array $data)
    {
        return $this->mainModel->where('id', $id)->update($data);
    }

    /**
     * Delete a model
     *
     * @param int|mixed $id
     * @return Boolean
     */
    public function delete($id)
    {
        $data = $this->findById($id);
        return $data->delete();
    }

    /**
     * Delete multiple items
     *
     * @param array $id
     * @return Boolean|mixed
     */
    public function destroy(array $id)
    {
        return $this->mainModel->destroy($id);
    }
}
