Skip to content

Commit 359f51a

Browse files
committed
New Version
1 parent 5a7a566 commit 359f51a

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

app/Http/Controllers/API/Admin/AdminController.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ class AdminController extends Controller
2121
use ResponseTrait;
2222
use FunctionTemplateTrait;
2323

24-
public function index(){
25-
try{
26-
return $this->successResponse(UserResource::collection(User::where('verify','!=',0)->get()));
27-
}catch (\Exception $e){
24+
public function index()
25+
{
26+
try {
27+
// Retrieve a collection of users where the 'verify' column is not equal to 0
28+
$users = User::where('verify', '!=', 0)->get();
29+
30+
// Return a success response with the user collection
31+
return $this->successResponse(UserResource::collection($users));
32+
} catch (\Exception $e) {
33+
// Return an error response if an exception is caught
2834
return $this->errorResponse();
2935
}
3036
}

app/Http/Controllers/API/Courses/CourseController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function index()
3636
try {
3737
return $this->successResponse(CourseResource::collection(Course::get()));
3838
} catch (\Exception $exception) {
39-
return $this->errorResponse();
39+
return $exception->getMessage();
4040
}
4141

4242
// $courses = CourseResource::collection(Course::get());
@@ -370,7 +370,7 @@ public function getData($id){
370370

371371
return $this->successResponse($data);
372372
}catch (\Exception $e){
373-
return $this->errorResponse();
373+
return $e;
374374
}
375375
}
376376

app/Http/Resources/CourseCommentResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function toArray(Request $request): array
2222
'user' => $user,
2323
'comment_id' =>$this->id,
2424
'content' => $this->content,
25-
'created_at' => $this->created_at->diffForHumans(),
25+
'created_at' => isset($this->created_at)?$this->created_at->diffForHumans():0,
2626
'replay' => isset($this ->replay)? $this ->replay: 0,
2727
'replies' =>$commentsController->replies($this->id),
2828
];

app/Http/Resources/CourseResource.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\User;
66
use Illuminate\Http\Request;
77
use Illuminate\Http\Resources\Json\JsonResource;
8+
use Illuminate\Support\Facades\DB;
89

910
class CourseResource extends JsonResource
1011
{
@@ -15,6 +16,16 @@ class CourseResource extends JsonResource
1516
*/
1617
public function toArray(Request $request): array
1718
{
19+
/**
20+
* the subscribed has three values:
21+
* 0: if user is not login in site
22+
* 1: if user logged in and not subscribed in the course yet
23+
* 2: if user logged in successfully and subscribed in course
24+
*/
25+
$subscribe = 0 ;
26+
if(auth()->user()){
27+
$subscribe = (DB::table('user_course_pivot')->where('course_id', $this->id)->where('user_id', auth()->user()->id)->first()) ? 2 : 1 ;
28+
}
1829
return [
1930
'id'=> $this->id,
2031
'teacher'=> User::find($this->teacher_id)->name,
@@ -25,6 +36,7 @@ public function toArray(Request $request): array
2536
'hours' => $this->hours,
2637
'image' => 'http://127.0.0.1:8000/images/'.$this->image,
2738
'create_at' => $this->create_at,
39+
'subscribe' => $subscribe
2840
];
2941
}
3042
}

app/Http/Resources/PostResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class PostResource extends JsonResource
1616
public function toArray(Request $request): array
1717
{
1818
return [
19+
'id' =>$this->id,
1920
'title' => $this->title,
2021
'body' => $this->body,
2122
'user' =>isset(User::find($this->user_id)->name)?User::find($this->user_id)->name:0,

routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@
374374
Route::post('/add-replay/{id}',[\App\Http\Controllers\API\Post\ReplayPostController::class,'insert'])->middleware(['verify.check', 'auth:api']);
375375

376376
/**
377-
* Destroy Replay on Post
377+
* 26. Destroy Replay on Post
378378
*/
379379
Route::post('/delete/{id}',[\App\Http\Controllers\API\Post\ReplayPostController::class,'destroy'])->middleware(['verify.check', 'auth:api']);
380380

0 commit comments

Comments
 (0)