1 | package com.example.eatys_app.controller;
|
---|
2 |
|
---|
3 | import com.example.eatys_app.model.Korisnik;
|
---|
4 | import com.example.eatys_app.model.Menadzer;
|
---|
5 | import com.example.eatys_app.model.Restoran;
|
---|
6 | import com.example.eatys_app.service.KorisnikService;
|
---|
7 | import com.example.eatys_app.service.MenadzerService;
|
---|
8 | import com.example.eatys_app.service.RestoranService;
|
---|
9 | import jakarta.servlet.http.HttpServletRequest;
|
---|
10 | import org.springframework.stereotype.Controller;
|
---|
11 | import org.springframework.ui.Model;
|
---|
12 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
13 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
14 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
15 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
16 |
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 |
|
---|
20 | @Controller
|
---|
21 | public class RestoranController {
|
---|
22 |
|
---|
23 | private final RestoranService restoranService;
|
---|
24 | private final MenadzerService menadzerService;
|
---|
25 | private final KorisnikService korisnikService;
|
---|
26 |
|
---|
27 | public RestoranController(RestoranService restoranService, MenadzerService menadzerService, KorisnikService korisnikService) {
|
---|
28 | this.restoranService = restoranService;
|
---|
29 | this.menadzerService = menadzerService;
|
---|
30 | this.korisnikService = korisnikService;
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | @GetMapping({"/", "/restorani" })
|
---|
35 | public String showList(Model model, HttpServletRequest req){
|
---|
36 | List<Restoran> restorani= this.restoranService.listAll();
|
---|
37 | List<Menadzer>menadzeri=this.menadzerService.listAll();
|
---|
38 | String username = req.getRemoteUser();
|
---|
39 | if(username!=null) {
|
---|
40 | Korisnik korisnik = this.korisnikService.FindByName(username);
|
---|
41 | model.addAttribute("korisnik", korisnik);
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | model.addAttribute("restorani", restorani);
|
---|
46 | model.addAttribute("menadzeri", menadzeri);
|
---|
47 |
|
---|
48 |
|
---|
49 | return "restorani.html";
|
---|
50 | }
|
---|
51 |
|
---|
52 | @GetMapping("/restorani/add")
|
---|
53 | public String showAdd(Model model) {
|
---|
54 | List<Menadzer> menadzeri=this.menadzerService.listAll();
|
---|
55 | model.addAttribute("menadzeri", menadzeri);
|
---|
56 | return "form.html";
|
---|
57 | }
|
---|
58 |
|
---|
59 | @GetMapping("/restorani/{id}/edit")
|
---|
60 | public String showEdit(@PathVariable Integer id, Model model) {
|
---|
61 | Restoran restoran= this.restoranService.findById(id);
|
---|
62 | List<Menadzer> menadzeri=this.menadzerService.listAll();
|
---|
63 | model.addAttribute("restoran", restoran);
|
---|
64 | model.addAttribute("menadzeri", menadzeri);
|
---|
65 | return "form.html";
|
---|
66 | }
|
---|
67 |
|
---|
68 | @PostMapping("/restorani/")
|
---|
69 | public String create(@RequestParam String ime,
|
---|
70 | @RequestParam Integer rejting,
|
---|
71 | @RequestParam String adresa,
|
---|
72 | @RequestParam Integer menadzerId
|
---|
73 | ){
|
---|
74 | this.restoranService.create(ime, rejting, adresa, menadzerId);
|
---|
75 | return "redirect:/restorani";
|
---|
76 | }
|
---|
77 |
|
---|
78 | @PostMapping("/restorani/{id}")
|
---|
79 | public String update(@PathVariable Integer id,
|
---|
80 | @RequestParam String ime,
|
---|
81 | @RequestParam Integer rejting,
|
---|
82 | @RequestParam String adresa,
|
---|
83 | @RequestParam Integer menadzerId
|
---|
84 | ){
|
---|
85 | this.restoranService.update(id, ime, rejting, adresa, menadzerId);
|
---|
86 | return "redirect:/restorani";
|
---|
87 | }
|
---|
88 |
|
---|
89 | @PostMapping("/restorani/{id}/delete")
|
---|
90 | public String delete(@PathVariable Integer id) {
|
---|
91 | this.restoranService.delete(id);
|
---|
92 | return "redirect:/restorani";
|
---|
93 | }
|
---|
94 |
|
---|
95 | } |
---|