Projekt

Obecné

Profil

Stáhnout (2.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 74e290c2 Adam Mištera
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Artefact;
6 fb354d59 rizir01
use App\ArtefactCategory;
7 bfa73340 Adam Mištera
use App\User;
8 2fee1bb5 Marek Lovčí
use Illuminate\Contracts\View\Factory;
9
use Illuminate\Http\RedirectResponse;
10 bfa73340 Adam Mištera
use Illuminate\Support\Facades\Auth;
11 2fee1bb5 Marek Lovčí
use Illuminate\View\View;
12 74e290c2 Adam Mištera
13
class ArtefactController extends Controller
14
{
15 21570473 Adam Mištera
    public function __construct()
16
    {
17
        $this->middleware('auth');
18
    }
19
20 10222730 Adam Mištera
    /**
21
     * Returns view of all artefacts.
22
     *
23 2fee1bb5 Marek Lovčí
     * @return Factory|View
24 10222730 Adam Mištera
     */
25 74e290c2 Adam Mištera
    public function default()
26
    {
27 10222730 Adam Mištera
        $artefacts = Artefact::all();
28 74e290c2 Adam Mištera
29
        return view('artefact.default', ['artefacts' => $artefacts]);
30
    }
31 10222730 Adam Mištera
32 fb354d59 rizir01
    /**
33
     * Returns view of artefacts related to the chosen category
34
     *
35 2fee1bb5 Marek Lovčí
     * @param $id id of the category
36
     * @return Factory|View
37 fb354d59 rizir01
     */
38
    public function showCategory($id)
39
    {
40 2fee1bb5 Marek Lovčí
        $categoryArtefacts = ArtefactCategory::where('category_id', $id)->get();
41
        if(count($categoryArtefacts) > 0)
42 fb354d59 rizir01
        {
43
            $artefacts = array();
44 2fee1bb5 Marek Lovčí
            foreach($categoryArtefacts as $ar)
45 fb354d59 rizir01
            {
46
                array_push($artefacts, Artefact::where('id', $ar->artefact_id)->get());
47
            }
48
            return view('artefact.category', ['artefacts' => $artefacts]);
49
        }
50
        else
51
        {
52
            return view('artefact.category', ['artefacts' => array()]);
53
        }
54
    }
55
56 10222730 Adam Mištera
    /**
57
     * Returns view of single artefact given by its id.
58
     *
59
     * @param $id int id of the artefact
60 2fee1bb5 Marek Lovčí
     * @return Factory|View
61 10222730 Adam Mištera
     */
62
    public function view($id)
63
    {
64
        $artefact = Artefact::find($id);
65 70f69b41 Adam Mištera
        $artefact['likes'] = Artefact::find($id)->users()->count();
66 bfa73340 Adam Mištera
        $artefact['favourite'] = is_null(User::find(Auth::id())->likesArtefacts()->find($id)) ? false : true;
67 10222730 Adam Mištera
68
        return view('artefact.view', ['artefact' => $artefact]);
69
    }
70 bfa73340 Adam Mištera
71
    /**
72 1bcdcb05 Adam Mištera
     * Likes artefact given by its id.
73 bfa73340 Adam Mištera
     *
74
     * @param $id int id of metadata
75 2fee1bb5 Marek Lovčí
     * @return RedirectResponse
76 bfa73340 Adam Mištera
     */
77
    public function like($id)
78
    {
79
        $user = User::find(Auth::id());
80
        $artefact = Artefact::find($id);
81
82
        $user->likesArtefacts()->attach($artefact);
83
84
        return back()->withInput();
85
    }
86
87
    /**
88 1bcdcb05 Adam Mištera
     * Unlikes artefact given by its id.
89 bfa73340 Adam Mištera
     *
90
     * @param $id int id of metadata
91 2fee1bb5 Marek Lovčí
     * @return RedirectResponse
92 bfa73340 Adam Mištera
     */
93
    public function unlike($id)
94
    {
95
        $user = User::find(Auth::id());
96
        $artefact = Artefact::find($id);
97
98
        $user->likesArtefacts()->detach($artefact);
99
100
        return back()->withInput();
101
    }
102 74e290c2 Adam Mištera
}