BusinessRepository.php
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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'));
            }
        }
    }
}