aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/Controller.php
blob: f3276b6aa1f36495b4c6568307874210e150589b (plain) (blame)
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
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;


abstract class Controller
{
    protected string $apiUrl;
    // API URLs for appointments, students, and companies, based on the provided URL structure
    protected string $appointmentApiUrl;
    protected string $studentsApiUrl;
    protected string $companiesApiUrl;
    protected string $connectionsApiUrl;
    protected string $messagesApiUrl;
    protected string $diplomasApiUrl;

    public function __construct() {
        $this->apiUrl = config('api.ip_address');

        $this->appointmentApiUrl = $this->apiUrl . '/appointments';
        $this->studentsApiUrl = $this->apiUrl . '/students';
        $this->companiesApiUrl = $this->apiUrl .'/companies';
        $this->connectionsApiUrl = $this->apiUrl . '/connections';
        $this->messagesApiUrl = $this->apiUrl . '/messages';
        $this->diplomasApiUrl = $this->apiUrl . '/diplomas';
    }

    #bottom functions generated by github copilot. Use a student or company object to get the name of the student or company
    protected function translateStudent($id, $token='')
    {

        $student = Http::withHeaders( [
            "Authorization" => $token
        ])->get("{$this->studentsApiUrl}/{$id}")->json('data');

        if (isset($student['first_name']) && isset($student['last_name'])) {
                $id = $student['first_name'] . ' ' . $student['last_name'];
            } else {
                $id = '*Verwijderde student*';
            }
            return $id;
    }
    protected function translateCompany($id, $token= '')
    {
        $company = Http::get("{$this->companiesApiUrl}/{$id}")->json('data');
        if (isset($company['name'])) {
            $id = $company['name'];
        } else {
            $id = '*Verwijderd bedrijf*';
        }
        return $id;
    }

    protected function translateStudents($array, $students)
    {
        foreach ($array as &$value) {
            foreach ($students as $student) {
                if ($value['student_id'] == $student['id']) {
                    $value['student_id'] = $student['first_name'] . ' ' . $student['last_name'];
                    break;
                }
            }
        }
        return $array;
    }
    protected function translateCompanies($array, $companies)
    {
        foreach ($array as &$value) {
            foreach ($companies as $company) {
                if ($value['company_id'] == $company['id']) {
                    $value['company_id'] = $company['name'];
                    break;
                }
            }
        }
        return $array;
    }
}