source: src/main/java/com/example/eatys_app/controller/RestoranController.java

Last change on this file was b3f2adb, checked in by Aleksandar Siljanoski <acewow3@…>, 14 months ago

Adding project to repo

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package com.example.eatys_app.controller;
2
3import com.example.eatys_app.model.Korisnik;
4import com.example.eatys_app.model.Menadzer;
5import com.example.eatys_app.model.Restoran;
6import com.example.eatys_app.service.KorisnikService;
7import com.example.eatys_app.service.MenadzerService;
8import com.example.eatys_app.service.RestoranService;
9import jakarta.servlet.http.HttpServletRequest;
10import org.springframework.stereotype.Controller;
11import org.springframework.ui.Model;
12import org.springframework.web.bind.annotation.GetMapping;
13import org.springframework.web.bind.annotation.PathVariable;
14import org.springframework.web.bind.annotation.PostMapping;
15import org.springframework.web.bind.annotation.RequestParam;
16
17import java.util.List;
18
19
20@Controller
21public 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}
Note: See TracBrowser for help on using the repository browser.