/home/mip/mip/app/Modules/Client/Repositories/Products/ProductRepository.php
<?php

namespace QxCMS\Modules\Client\Repositories\Products;
use Illuminate\Support\Str;
use QxCMS\Modules\AbstractRepository;
use QxCMS\Modules\Client\Models\Products\Product;
use QxCMS\Modules\Client\Models\Settings\UserLogs\UserLogs as Log;
use Storage;
use Illuminate\Http\Request;

class ProductRepository extends AbstractRepository implements ProductRepositoryInterface
{
    protected $model;
    protected $log;

    function __construct(Product $model, Log $log)
    {
        $this->model = $model;
        $this->log = $log;
    }

    public function getAllProduct(){
        $allProduct = $this->model->all();
        return $allProduct;
    }
    
    public function create(Request $request)
    {
        $user = auth()->user();
        $model = $this->model->fill($request->except(['image']));

        if( $request->hasFile('image') ) {
            $ext = $request->file('image')->getClientOriginalExtension();
            $path = $request->file('image')->storeAs($user->client->root_dir.'/products', Str::slug($request['name']).'.'.$ext, $user->client->storage_type);
            $model->image = basename($path);
        }
        $model->save();
        $this->log->saveLog(['action' => 'Create', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);
        return $model;
    }

    public function update($id, Request $request)
    {
        $model = $this->findById($id);
        $user = auth()->user();
        $model->fill($request->all());
        
        if( $request->hasFile('image') ) {
            Storage::disk($user->client->storage_type)->delete($user->client->root_dir.'/products/'.$model['image']);
            $ext = $request->file('image')->getClientOriginalExtension();
            $path = $request->file('image')->storeAs($user->client->root_dir.'/products', Str::slug($request['name']).'.'.$ext, $user->client->storage_type);
            $model->image = basename($path);
            $this->log->saveLog(['action' => 'Update', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);
        }   
        if(count($model->getDirty()) > 0) {
            $model->save();
            session()->flash('success', 'Successfully edited.');
            $this->log->saveLog(['action' => 'Update', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);
        }
        return $model;
    }

    public function delete($id)
    {
        $model = $this->findById($id);
        $user = auth()->user();
        if($model->image <> ''){
            Storage::disk($user->client->storage_type)->delete($user->client->root_dir.'/products/'.$model['image']);
        }
        $model->delete();
        $this->log->saveLog(['action' => 'Delete', 'module_id' => $this->getModuleId(), 'user_id' => $user->id, 'data_id' => $model->id]);
        return $this->getAjaxResponse('success', 'Successfully deleted.');
    }
}