File size: 2,429 Bytes
907b200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Api\BaseController;
use App\Http\Resources\ScientistResource;
use App\Services\ScientistService;
use Illuminate\Http\Request;

class ScientistController extends BaseController
{
    protected $service;

    public function __construct(ScientistService $service)
    {
        $this->service = $service;
    }

    public function index(Request $request)
    {
        $perPage = min(100, max(1, (int)$request->get('per_page', 10)));

        $scientists = $this->service->getScientists($perPage);

        return $this->successResponse(
            'Scientists retrieved successfully',
            [
                'results' => ScientistResource::collection($scientists),
                'pagination' => [
                    'currentPage' => $scientists->currentPage(),
                    'totalPages' => $scientists->lastPage(),
                    'totalResults' => $scientists->total(),
                    'perPage' => $scientists->perPage(),
                    'hasNextPage' => $scientists->hasMorePages(),
                    'hasPrevPage' => !$scientists->onFirstPage()
                ]
            ]
        );
    }

    public function show($id)
    {
        $scientist = $this->service->getScientist($id);

        return $this->successResponse(
            'Scientist retrieved successfully',
            [
                'results' => [new ScientistResource($scientist)],
                'pagination' => [
                    'currentPage' => 1,
                    'totalPages' => 1,
                    'totalResults' => 1,
                    'perPage' => 1,
                    'hasNextPage' => false,
                    'hasPrevPage' => false
                ]
            ]
        );
    }

    public function getAwardsByScientist($id)
    {
        $scientist = $this->service->getScientist($id);

        $awards = $scientist->awards;

        return $this->successResponse(
            'Awards retrieved successfully',
            [
                'results' => $awards,
                'pagination' => [
                    'currentPage' => 1,
                    'totalPages' => 1,
                    'totalResults' => $awards->count(),
                    'perPage' => $awards->count(),
                    'hasNextPage' => false,
                    'hasPrevPage' => false
                ]
            ]
        );
    }
}