File size: 1,928 Bytes
560ee81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php


namespace App\Services;


use App\Models\Tagged as TaggedModel;
use Illuminate\Support\Facades\Storage;

class TaggedServices extends BaseServices
{
    private $contentImageServices ;
    public function __construct(TaggedModel $model)
    {
        parent::__construct($model);
    }

    public function index($request)
    {
        $limit = $request->get('limit', TaggedModel::LIMIT_PAGE);
        $query = $this->model;
        return $query->paginate($limit);
    }

    public function show($id)
    {

    }

    public function save(array $attributes)
    {
        if (!empty($attributes['comic_id'])&&!empty($attributes['hashtag_id'])) {
            $entity = $this->model
                ->where('comic_id',$attributes['comic_id'])
                ->where('hashtag_id',$attributes['hashtag_id'])
                ->first();
            if ($entity) {
                $this->model->where('comic_id', $attributes['comic_id'])
                    ->update(['is_main_tag' => 0]);
                $entity->fill($attributes)->save();
                return $entity;
            } else {
                return $this->model->create($attributes);
            }
        } else {
            return null;
        }
    }

    public function deleteByComicIdandHashtagId($comic_id,$hashtags){
        return $this->model->where('comic_id',$comic_id)->whereIn('hashtag_id',$hashtags)->delete();
    }

    public function findByComicIdandHashtagandIsMain($comic_code,$is_main_tag=true){
        $query = TaggedModel::query();
        $query = $query->whereHas('comic',function ($query)use ($comic_code){
            $query->where('comic_code',$comic_code);
        })->where('is_main_tag',$is_main_tag);
        return $query->first();
    }

    public function delete($id)
    {
        $entity = $this->model
            ->where('id', $id)->first();
        return !empty($entity) ? $entity->delete() : null;
    }
}