1 | package com.example.cookbook.controller;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.cookbook.model.*;
|
---|
5 | import com.example.cookbook.model.exception.ReceptNeEPronajdenException;
|
---|
6 | import com.example.cookbook.service.*;
|
---|
7 | import org.springframework.stereotype.Controller;
|
---|
8 | import org.springframework.ui.Model;
|
---|
9 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
10 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
11 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
13 |
|
---|
14 | import java.sql.SQLException;
|
---|
15 | import java.time.LocalDateTime;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Controller
|
---|
19 | public class HomeController {
|
---|
20 |
|
---|
21 | private final ReceptService receptService;
|
---|
22 | private final SlikiService slikiService;
|
---|
23 |
|
---|
24 | private final SostojkiService sostojkiService;
|
---|
25 |
|
---|
26 | private final KomentariService komentariService;
|
---|
27 |
|
---|
28 | private final KorisniciService korisniciService;
|
---|
29 |
|
---|
30 | public HomeController(ReceptService receptService,
|
---|
31 | SlikiService slikiService,
|
---|
32 | SostojkiService sostojkiService, KomentariService komentariService, KorisniciService korisniciService) {
|
---|
33 |
|
---|
34 | this.receptService = receptService;
|
---|
35 | this.slikiService = slikiService;
|
---|
36 | this.sostojkiService = sostojkiService;
|
---|
37 | this.komentariService = komentariService;
|
---|
38 | this.korisniciService = korisniciService;
|
---|
39 | }
|
---|
40 |
|
---|
41 | @GetMapping({"/", "/recepti"})
|
---|
42 | public String getHomePage(Model model){
|
---|
43 |
|
---|
44 | List<Recept> recepti = null;
|
---|
45 | try {
|
---|
46 | recepti = receptService.listAll();
|
---|
47 | } catch (SQLException e) {
|
---|
48 | return "redirect:/error-page/SQL%20Exception";
|
---|
49 | }
|
---|
50 | model.addAttribute("recepti", recepti);
|
---|
51 | return "home";
|
---|
52 | }
|
---|
53 |
|
---|
54 | @GetMapping("/recept/{id}")
|
---|
55 | public String getReceptPage(@PathVariable Long id, Model model){
|
---|
56 |
|
---|
57 | Recept recept = null;
|
---|
58 | List<Slika> sliki;
|
---|
59 | List<Sostojka> sostojki;
|
---|
60 | List<Komentar> komentari;
|
---|
61 | List<Korisnik> posetiteli;
|
---|
62 | try {
|
---|
63 | recept = receptService.findById(id);
|
---|
64 | sliki = slikiService.findAllPicById(id);
|
---|
65 | sostojki = sostojkiService.findAllById(id);
|
---|
66 | komentari = komentariService.findAllById(id);
|
---|
67 | posetiteli = korisniciService.findAllByRole(TipKorisnik.Posetitel);
|
---|
68 | } catch (SQLException e) {
|
---|
69 | return "redirect:/error-page/SQL%20Exception";
|
---|
70 | }catch (ReceptNeEPronajdenException e){
|
---|
71 | return "redirect:/error-page/" + e.getMessage();
|
---|
72 | }
|
---|
73 | model.addAttribute("recept", recept);
|
---|
74 | model.addAttribute("sliki", sliki);
|
---|
75 | model.addAttribute("sostojki", sostojki);
|
---|
76 | model.addAttribute("komentari", komentari);
|
---|
77 | model.addAttribute("posetiteli", posetiteli);
|
---|
78 | return "recept";
|
---|
79 | }
|
---|
80 |
|
---|
81 | @PostMapping("/komentar/{id}")
|
---|
82 | public String processKomentar(@PathVariable Long id,
|
---|
83 | @RequestParam Integer ocena,
|
---|
84 | @RequestParam String komentar,
|
---|
85 | @RequestParam String telefon){
|
---|
86 | if (id == null){
|
---|
87 | return "redirect:/error-page/Ne%20dodavanje%20komentar(Identifikatorot%20na%20receptot%20ne%20postoi)";
|
---|
88 | }
|
---|
89 | try {
|
---|
90 | komentariService.add(telefon, id, ocena, komentar);
|
---|
91 | } catch (SQLException e) {
|
---|
92 | return "redirect:/error-page/SQL%20Exception";
|
---|
93 | }catch (IllegalArgumentException e){
|
---|
94 | return "redirect:/error-page/Illegal%20Arguments";
|
---|
95 | }
|
---|
96 | return "redirect:/recept/" + id;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | @GetMapping("/error-page/{error}")
|
---|
101 | public String getErrorPage(@PathVariable(required = false) String error, Model model){
|
---|
102 |
|
---|
103 | if (error != null){
|
---|
104 | model.addAttribute("error", error);
|
---|
105 | }
|
---|
106 | return "error-page";
|
---|
107 | }
|
---|
108 | }
|
---|