@@ -15,9 +15,8 @@ class ModulController extends Controller
1515{
1616 public function index ()
1717 {
18- $ user = Auth::user ();
1918 $ moduls = Modul::with (['modulImage ' , 'user ' ])
20- ->withCount ('quiz ' ,'article ' , 'video ' )
19+ ->withCount ('quiz ' , 'article ' , 'video ' )
2120 ->paginate (5 );
2221
2322 return response ()->json ([
@@ -29,7 +28,7 @@ public function index()
2928 'last_page ' => $ moduls ->lastPage (),
3029 'per_page ' => $ moduls ->perPage (),
3130 'total ' => $ moduls ->total (),
32- ]
31+ ]
3332 ]);
3433 }
3534
@@ -156,14 +155,15 @@ public function getExceptByUser($userId)
156155 ]);
157156 }
158157
159- public function getMostFavoriteModul (){
158+ public function getMostFavoriteModul ()
159+ {
160160
161161 $ moduls = Modul::with (['modulImage ' , 'user ' ])
162162 ->withCount ('favoriteModul ' )
163163 ->orderBy ('favorite_modul_count ' , 'desc ' )
164164 ->paginate (5 );
165165
166-
166+
167167
168168 return response ()->json ([
169169 'message ' => 'Modul by most liked ' ,
@@ -313,9 +313,9 @@ public function generateContent(Request $request)
313313 $ videoController = new videoController ();
314314 $ articleController = new articleController ();
315315
316- $ articleResult = $ articleController ->generateArticles ( $ title , $ modul ->id );
316+ $ articleResult = $ articleController ->generateArticles ($ title , $ modul ->id );
317317
318- $ videoResult = $ videoController ->generateVideos ( $ title ,$ modul ->id );
318+ $ videoResult = $ videoController ->generateVideos ($ title , $ modul ->id );
319319
320320 $ quizzes = $ quizController ->generateQuiz ($ modul ->id , $ articleResult );
321321
@@ -358,11 +358,12 @@ public function fetchImage($imageKeyword)
358358 return null ;
359359 }
360360
361- public function fetchGoogleImages ($ imageKeyword ){
361+ public function fetchGoogleImages ($ imageKeyword )
362+ {
362363 $ apikey = env ('GOOGLE_API_KEY ' );
363364 $ cseId = env ('GOOGLE_CSE_ID ' );
364365
365- if (!$ apikey || !$ cseId ){
366+ if (!$ apikey || !$ cseId ) {
366367 Log::warning ('Google API key or CSE ID not set in .env file ' );
367368 return [];
368369 }
@@ -376,7 +377,7 @@ public function fetchGoogleImages($imageKeyword){
376377 'num ' => 5 ,
377378 ]);
378379
379- if (!$ response ->successful ()){
380+ if (!$ response ->successful ()) {
380381 Log::error ('Google API request failed ' , [
381382 'status ' => $ response ->status (),
382383 'body ' => $ response ->body (),
@@ -398,35 +399,55 @@ public function fetchGoogleImages($imageKeyword){
398399 }
399400 }
400401
401- public function favoriteUser (Request $ request , $ id ){
402- $ user = User::where ('id ' , $ request ->user_id );
403- dd ($ user );
404- $ modul = Modul::findOrFail ($ id );
402+ public function favoriteUser ($ modulId )
403+ {
404+ if (!is_numeric ($ modulId )) {
405+ return response ()->json ([
406+ 'status ' => 'error ' ,
407+ 'message ' => 'ID modul tidak valid. '
408+ ], 400 );
409+ }
410+
411+ $ user = Auth::user ();
405412
406- if ($ user ->favoriteModul ()->where ('modul_id ' , $ id )->exists ()) {
407- $ user ->favoriteModul ()->detach ($ id );
413+ if ($ user ->favoriteModul ()->where ('modul_id ' , $ modulId )->exists ()) {
414+ $ user ->favoriteModul ()->detach ($ modulId );
408415 return response ()->json ([
409416 'status ' => 'unfavorited ' ,
410417 'message ' => 'Modul dihapus dari favorit. '
411418 ]);
412419 } else {
413- $ user ->favoriteModul ()->attach ($ id );
420+ $ user ->favoriteModul ()->attach ($ modulId );
414421 return response ()->json ([
415422 'status ' => 'favorited ' ,
416423 'message ' => 'Modul ditambahkan ke favorit. '
417424 ]);
418425 }
419426 }
420427
421- public function getFavoriteModul (Request $ request )
428+
429+ public function getFavoriteModul ()
422430 {
423- $ user = $ request -> user ();
431+ $ user = Auth:: user ();
424432 $ favoriteModuls = $ user ->favoriteModul ()->get ();
425433
434+ if ($ favoriteModuls ->isEmpty ()) {
435+ return response ()->json ([
436+ 'message ' => 'Tidak ada modul favorit ' ,
437+ 'data ' => []
438+ ]);
439+ }
440+
426441 return response ()->json ([
442+ 'message ' => 'Modul favorit ditemukan ' ,
443+ 'user ' => [
444+ 'id ' => $ user ->id ,
445+ 'name ' => $ user ->username ,
446+ 'email ' => $ user ->email
447+ ],
427448 'data ' => $ favoriteModuls
428449 ]);
429450 }
430-
451+
431452}
432453
0 commit comments