source: src/main/java/com/example/cookbook/controller/HomeController.java

Last change on this file was d4d8fb9, checked in by Blazho <aleksandar.blazhevski@…>, 9 months ago

Posetitelot komentira i dava ocena za recept(https://develop.finki.ukim.mk/projects/cbdb/wiki/useCase11)

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package com.example.cookbook.controller;
2
3
4import com.example.cookbook.model.*;
5import com.example.cookbook.model.exception.ReceptNeEPronajdenException;
6import com.example.cookbook.service.*;
7import org.springframework.stereotype.Controller;
8import org.springframework.ui.Model;
9import org.springframework.web.bind.annotation.GetMapping;
10import org.springframework.web.bind.annotation.PathVariable;
11import org.springframework.web.bind.annotation.PostMapping;
12import org.springframework.web.bind.annotation.RequestParam;
13
14import java.sql.SQLException;
15import java.time.LocalDateTime;
16import java.util.List;
17
18@Controller
19public 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}
Note: See TracBrowser for help on using the repository browser.