-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArtikelDakwahController.php
160 lines (136 loc) · 5.07 KB
/
ArtikelDakwahController.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace App\Http\Controllers\Konten;
use Hashids\Hashids;
use Illuminate\Http\Request;
use App\Models\ArtikelDakwah;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;
class ArtikelDakwahController extends Controller
{
public function createArtikel(Request $request){
$validator = Validator::make($request->all(), [
'judul' => ['required', 'string'],
'gambar'=>['required', 'mimes:jpeg,jpg,png,svg,webp','max:2048', 'image'],
'deskripsi' => ['required', 'string'],
'author' => ['required', 'string'],
]);
if($validator->fails()){
return response()->json([
'Error' => true,
'Massage' => $validator->errors()
], 402);
}
$uploadedImage = Cloudinary::upload($request->file('gambar')->getRealPath(), [
'folder' => 'MIM/ArtikelDakwah'
]);
$artikels = ArtikelDakwah::create([
'judul' => $request->judul,
'gambar' => $uploadedImage->getSecurePath(),
'deskripsi' => $request->deskripsi,
'author' => $request->author
]);
$hashids = new Hashids('your-secret-salt', 10);
$hashedId = $hashids->encode($artikels->id);
$artikel = $artikels->toArray();
$artikel['id'] = $hashedId;
return response()->json([
'Massage' => 'artikelCreatedSuccessfully',
'Artikel' => $artikel
]);
}
public function showArtikel(){
$artikels = ArtikelDakwah::orderBy('created_at', 'desc')->paginate(8);
$hashids = new Hashids('your-secret-salt', 10);
$transformedData = $artikels->getCollection()->map(function($article) use ($hashids) {
$array = $article->toArray();
$encodedId = $hashids->encode($article->id);
if ($encodedId) {
$array['id'] = $encodedId;
} else {
return response()->json([
'Data User' => 'Id Tidak Bisa DIHash'
]);
}
return $array;
})->toArray();
$artikels->setCollection(collect($transformedData));
return response()->json([
'Artikel' => $artikels
]);
}
public function showOneArtikel($id){
$hashids = new Hashids('your-secret-salt', 10);
$artikels = ArtikelDakwah::where('id', $hashids->decode($id))->first();
if(!$artikels){
return response()->json([
'Artikel' => null, 'Not Found'
], 204);
}
$artikel = $artikels->toArray();
$encodedId = $hashids->encode($artikels->id);
if ($encodedId) {
$artikel['id'] = $encodedId ;
} else {
return response()->json([
'Data User' => 'Id Tidak Bisa DIHash'
]);
}
return response()->json([
'Artikel' => $artikel
]);
}
public function updateArtikel(Request $request, $id){
$hashids = new Hashids('your-secret-salt', 10);
$artikel = ArtikelDakwah::find($hashids->decode($id)[0]);
if (!$artikel) {
return response()->json([
'Error' => true,
'Message' => 'Artikel tidak ditemukan'
], 404);
}
$validator = Validator::make($request->all(), [
'judul' => ['required', 'string'],
'gambar'=>['required', 'mimes:jpeg,jpg,png,svg,webp','max:2048', 'image'],
'deskripsi' => ['required', 'string'],
'author' => ['required', 'string'],
]);
if($validator->fails()){
return response()->json([
'Error' => true,
'Massage' => $validator->errors()
]);
}
if ($request->hasFile('gambar')) {
// Menghapus foto lama jika ada
if ($artikel->gambar) {
$publicId = pathinfo($artikel->gambar, PATHINFO_FILENAME);
Cloudinary::destroy($publicId);
}
// Upload gambar baru ke Cloudinary
$uploadedImage = Cloudinary::upload($request->file('gambar')->getRealPath(), [
'folder' => 'MIM/ArtikelDakwah'
]);
// Simpan URL gambar ke dalam database
$artikel->gambar = $uploadedImage->getSecurePath();
}
$updateArtikel = [
'judul' => $request->judul,
'gambar' => $artikel->gambar,
'deskripsi' => $request->deskripsi,
'author' => $request->author
];
$artikel->update($updateArtikel);
return response()->json([
'Massage' => 'artikelUpdatedSuccessfully',
'Artikel' => $updateArtikel
]);
}
public function deleteArtikel($id){
$hashids = new Hashids('your-secret-salt', 10);
ArtikelDakwah::destroy($hashids->decode($id));
return response()->json([
'Massage' => 'artikelDeletedSuccessfully',
]);
}
}