File size: 3,674 Bytes
40dca3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

namespace App\Http\Controllers\AjaxControllers\V1\Backend;

use App\Http\Controllers\BaseController;
use App\Http\Requests\ApiChapterRequest;
use App\Services\ChapterServices;
use App\Services\ComicServices;
use App\Tranformers\ChapterResource\ChapterDetailResource;
use App\Tranformers\ChapterResource\ChapterListResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ChapterController extends BaseController
{
    private $chapterServices;
    private $comicServices;
    public function __construct(ChapterServices $chapterServices,ComicServices $comicServices)
    {
        $this->comicServices = $comicServices;
        $this->chapterServices = $chapterServices;
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $comic = $this->chapterServices->index($request);
       return (new ChapterListResource($comic))->additional([
            'total' => $comic->total(),
            'lastPage' => $comic->lastPage(),
            'currentPage' => $comic->currentPage(),
            'perPage' => (int)$comic->perPage(),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(ApiChapterRequest $request)
    {
        $comic = $request->only([
            'publish_at',
            'free_at',
            'status',
            'chapter_number',
            'next_chapter_id',
            'prv_chapter_id',
            'comic_id',
            'chapter_name',
        ]);

        try{
            $this->chapterServices->uploadGGDrive($request,$comic);
        }catch(\Exception $e){
            return $this->responseJson( trans('chapter.msg_content.msg_edit_fail'),500,$e->getMessage());
        }

        return $this->chapterServices->save($request,$comic);
    }


    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        return $this->responseJson('ok',200);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Request $request,string $comic_code,string $id)
    {
        $chapters = $this->chapterServices->show($id);
        return new ChapterDetailResource($chapters);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request,string $comic_code, string $id)
    {
        $comic = $request->only([
            'publish_at',
            'free_at',
            'status',
            'chapter_number',
            'next_chapter_id',
            'prv_chapter_id',
            'comic_id',
            'chapter_name',
            'content_images_id',
            'id'
        ]);
        $comic['comic_code'] = $comic_code;

        try{
            $this->chapterServices->uploadGGDrive($request,$comic);
        }catch(\Exception $e){
            return $this->responseJson( trans('chapter.msg_content.msg_edit_fail'),500,$e->getMessage());
        }

        return $this->chapterServices->save($request,$comic);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Request $request,string $comic_code, string $id)
    {
        $result = $this->chapterServices->delete($id);
        if (!$result) {
            return $this->responseJson( trans('chapter.msg_content.msg_delete_fail'),500,'');
        }
        return $this->responseJson( trans('chapter.msg_content.msg_delete_success'),200,['redirect'=>route('comics.edit',['code'=>$comic_code])]);
    }
}