SpotRepository.php
2.88 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
<?php
namespace App\Repositories\Business;
use App\Exceptions\Api\ApiException;
use App\Modules\Enums\ErrorCode;
use App\Modules\Models\Business\Business;
use App\Modules\Models\Spot\Spot;
use App\Modules\Repositories\BaseRepository;
/**
 * Class SpotRepository
 * @package App\Repositories\Spot
 */
class SpotRepository extends BaseRepository
{
    function getSelectedSpot($user_obj)
    {
        $where = [
            ['id', '=', $user_obj->business_id]
        ];
        $business = Business::where($where)->first();
        if ($business == null) {
            throw new ApiException(ErrorCode::BUSINESS_NOT_EXIST, trans('api.error.business_not_exist'));
        }
        if ($business->selected_spot_id) {
            return $business->selected_spot_id;
        }
        return false;
    }
    public function getToShowSpot($user_obj, $bool = false)
    {
        if ($selected_spot = $this->getSelectedSpot($user_obj)) {
            return $selected_spot;
        }
        $spots_obj = $this->getMylist($user_obj);
        if($bool){
            return $spots_obj[0];
        }else{
            return $spots_obj[0]->id;
        }
    }
    public function getToShowSpotObj($user_obj)
    {
        if ($selected_spot = $this->getSelectedSpot($user_obj)) {
            $spot = Spot::where('id', $selected_spot)->first();
            if ($spot == null) {
                throw new ApiException(ErrorCode::BUSINESS_DO_NOT_HAS_THIS_SPOT, trans('api.error.business_do_not_has_this_spot'));
            }
            return $spot;
        }
        $spots_obj = $this->getMylist($user_obj);
        return $spots_obj[0];
    }
    public function getMylist($user_obj)
    {
        $where = [
            [
                'business_id', '=', $user_obj->business_id,
            ],
        ];
        $spots = Spot::where($where)->get();
        if ($spots == null) {
            throw new ApiException(ErrorCode::BUSINESS_DO_NOT_HAS_SPOT, trans('api.error.business_do_not_has_spot'));
        }
        return $spots;
    }
    public function setSpotSelected($user_obj, $spot_id)
    {
        $business = $user_obj->business()->first();
        if ($business == null) {
            throw new ApiException(ErrorCode::USER_BUSINESS_NOT_FIND, trans('api.error.user_business_not_find'));
        }
        if ($business->selected_spot_id == $spot_id) {
            return true;
        }
        $business->selected_spot_id = $spot_id;
        $business->save();
        return true;
    }
    function spotPermission($user_obj, $spot_id)
    {
        $where = [
            ['business_id', '=', $user_obj->business_id],
            ['id', '=', $spot_id],
        ];
        $spot = spot::where($where)->first();
        if ($spot == null) {
            throw new ApiException(ErrorCode::BUSINESS_DO_NOT_HAS_SPOT_PERMISSION, trans('api.error.business_do_not_has_spot_permission'));
        }
        return $spot;
    }
}