- Timestamp:
- 02/03/24 15:58:58 (9 months ago)
- Branches:
- master
- Children:
- aea04dd
- Parents:
- 3e572eb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/cookbook/controller/HomeController.java
r3e572eb r501396e 1 package com.example.cookbook.controller;public class HomeController { 1 package com.example.cookbook.controller; 2 3 4 import com.example.cookbook.model.Recept; 5 import com.example.cookbook.model.Slika; 6 import com.example.cookbook.model.Sostojka; 7 import com.example.cookbook.model.exception.ReceptNeEPronajdenException; 8 import com.example.cookbook.service.ReceptService; 9 import com.example.cookbook.service.SlikiService; 10 import com.example.cookbook.service.SostojkiService; 11 import org.springframework.stereotype.Controller; 12 import org.springframework.ui.Model; 13 import org.springframework.web.bind.annotation.GetMapping; 14 import org.springframework.web.bind.annotation.PathVariable; 15 16 import java.sql.SQLException; 17 import java.util.List; 18 19 @Controller 20 public 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 } 2 77 }
Note:
See TracChangeset
for help on using the changeset viewer.