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

Last change on this file since 501396e was 501396e, checked in by Blazho <aleksandar.blazhevski@…>, 5 months ago

added missing files

  • Property mode set to 100644
File size: 2.4 KB
Line 
1package com.example.cookbook.controller;
2
3
4import com.example.cookbook.model.Recept;
5import com.example.cookbook.model.Slika;
6import com.example.cookbook.model.Sostojka;
7import com.example.cookbook.model.exception.ReceptNeEPronajdenException;
8import com.example.cookbook.service.ReceptService;
9import com.example.cookbook.service.SlikiService;
10import com.example.cookbook.service.SostojkiService;
11import org.springframework.stereotype.Controller;
12import org.springframework.ui.Model;
13import org.springframework.web.bind.annotation.GetMapping;
14import org.springframework.web.bind.annotation.PathVariable;
15
16import java.sql.SQLException;
17import java.util.List;
18
19@Controller
20public class HomeController {
21
22 private final ReceptService receptService;
23 private final SlikiService slikiService;
24
25 private final SostojkiService sostojkiService;
26
27 public HomeController(ReceptService receptService, SlikiService slikiService, SostojkiService sostojkiService) {
28
29 this.receptService = receptService;
30 this.slikiService = slikiService;
31 this.sostojkiService = sostojkiService;
32 }
33
34 @GetMapping({"/", "/recepti"})
35 public String getHomePage(Model model){
36
37 List<Recept> recepti = null;
38 try {
39 recepti = receptService.listAll();
40 } catch (SQLException e) {
41 return "redirect:/error-page/SQL%20Exception";
42 }
43 model.addAttribute("recepti", recepti);
44 return "home";
45 }
46
47 @GetMapping("/recept/{id}")
48 public String getReceptPage(@PathVariable Long id, Model model){
49
50 Recept recept = null;
51 List<Slika> sliki;
52 List<Sostojka> sostojki;
53 try {
54 recept = receptService.findById(id);
55 sliki = slikiService.findAllPicById(id);
56 sostojki = sostojkiService.findAllById(id);
57 } catch (SQLException e) {
58 return "redirect:/error-page/SQL%20Exception";
59 }catch (ReceptNeEPronajdenException e){
60 return "redirect:/error-page/" + e.getMessage();
61 }
62 model.addAttribute("recept", recept);
63 model.addAttribute("sliki", sliki);
64 model.addAttribute("sostojki", sostojki);
65 return "recept";
66 }
67
68
69 @GetMapping("/error-page/{error}")
70 public String getErrorPage(@PathVariable(required = false) String error, Model model){
71
72 if (error != null){
73 model.addAttribute("error", error);
74 }
75 return "error-page";
76 }
77}
Note: See TracBrowser for help on using the repository browser.