BusinessRepository.php 2.46 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 11:06
 */

namespace App\Repositories\Backend\Business;



use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Modules\Models\Business\Business;
use App\Events\Backend\Access\Business\BusinessCreated;
use App\Events\Backend\Access\Business\BusinessUpdated;
use App\Modules\Repositories\Business\BaseBusinessRepository;

class BusinessRepository extends BaseBusinessRepository
{
    public function getForDataTable()
    {
        return $this->query()->get();
    }


    public function create($input)
    {


        $data = $input['data'];
        $business = $this->createUserStub($data);

        DB::transaction(function () use ($business, $data) {
            if ($business->save()) {
                //Attach new roles
                event(new BusinessCreated($business));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.business.create_error'));
        });

    }

    public function update(Business $business, $input)
    {


        DB::transaction(function () use ($business, $input) {
            if ($business->update($input)) {
                event(new BusinessUpdated($business));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.business.update_error'));
        });
    }


    public function delete(Spot $spot,$input)
    {
        
        DB::transaction(function () use ($spot, $input) {
            if ($spot->delete($input)) {
                event(new SpotDeleted($spot));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.business.update_error'));
        });

    }

    protected function createUserStub($input)
    {
        $business = self::MODEL;
        $business = new $business;
        $business->business_name = $input['business_name'];
        $business->phone = $input['phone'];
        return $business;
    }

    /**
     * @param  $input
     * @param  $user
     *
     * @throws GeneralException
     */
    protected function checkUserByUsername($input, $business)
    {

        if ($business->business_name != $input['business_name']) {
            if ($this->query()->where('business_name', '=', $input['business_name'])->first()) {
                throw new GeneralException(trans('exceptions.backend.business.business_name_error'));
            }
        }
    }


}