source: src/main/java/com/example/eatys_app/controller/ObrokController.java@ b3f2adb

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

Adding project to repo

  • Property mode set to 100644
File size: 3.8 KB
Line 
1package com.example.eatys_app.controller;
2
3import com.example.eatys_app.model.Cena;
4import com.example.eatys_app.model.Korisnik;
5import com.example.eatys_app.model.Meni;
6import com.example.eatys_app.model.Obrok;
7import com.example.eatys_app.service.CenaService;
8import com.example.eatys_app.service.KorisnikService;
9import com.example.eatys_app.service.MeniService;
10import com.example.eatys_app.service.ObrokService;
11import jakarta.servlet.http.HttpServletRequest;
12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
14import org.springframework.web.bind.annotation.*;
15
16import java.util.List;
17
18@Controller
19public class ObrokController {
20
21 private final ObrokService obrokService;
22 private final MeniService meniService;
23 private final CenaService cenaService;
24 private final KorisnikService korisnikService;
25
26
27 public ObrokController(ObrokService obrokService, MeniService meniService, CenaService cenaService, KorisnikService korisnikService) {
28 this.obrokService = obrokService;
29 this.meniService = meniService;
30 this.cenaService = cenaService;
31 this.korisnikService = korisnikService;
32 }
33
34 @GetMapping( "/obroci" )
35 public String showList(Model model, HttpServletRequest req){
36 List<Obrok> obroci= this.obrokService.listAll();
37 List<Meni> menija= this.meniService.listAll();
38 List<Cena> ceni= this.cenaService.listAll();
39 String username = req.getRemoteUser();
40 if(username!=null) {
41 Korisnik korisnik = this.korisnikService.FindByName(username);
42 model.addAttribute("korisnik", korisnik);
43 }
44
45
46 model.addAttribute("obroci", obroci);
47 model.addAttribute("menija", menija);
48 model.addAttribute("ceni", ceni);
49 return "obroci.html";
50 }
51
52 @PostMapping( "/obroci/{id}/show" )
53 public String showListMenijaObroci(@PathVariable(required = false) Integer id,Model model){
54 List<Obrok> obroci= this.obrokService.listAll();
55 List<Meni> menija= this.meniService.listAll();
56 List<Cena> ceni= this.cenaService.listCeniByObrok(id);
57
58 model.addAttribute("obroci", obroci);
59 model.addAttribute("menija", menija);
60 model.addAttribute("ceni", ceni);
61 return "obroci.html";
62 }
63
64 @GetMapping("/obroci/add")
65 public String showAdd(Model model) {
66 List<Cena> ceni= this.cenaService.listAll();
67 List<Meni> menija= this.meniService.listAll();
68 model.addAttribute("menija", menija);
69 model.addAttribute("ceni", ceni);
70 return "formObrok.html";
71 }
72
73 @GetMapping("/obroci/{id}/edit")
74 public String showEdit(@PathVariable Integer id, Model model) {
75 Obrok obrok =this.obrokService.findById(id);
76 List<Meni> menija= this.meniService.listAll();
77 List<Cena> ceni= this.cenaService.listAll();
78 model.addAttribute("obrok", obrok);
79 model.addAttribute("menija", menija);
80 model.addAttribute("ceni", ceni);
81 return "formObrok.html";
82 }
83
84 @PostMapping("/obroci/")
85 public String create(@RequestParam String ime,
86 @RequestParam String opis,
87 @RequestParam Integer meniId
88 ){
89 this.obrokService.create(ime,opis,meniId);
90 return "redirect:/ceni/add";
91 }
92
93
94
95 @PostMapping("/obroci/{id}")
96 public String update(@PathVariable Integer id,
97 @RequestParam String ime,
98 @RequestParam String opis,
99 @RequestParam Integer meniId
100 ){
101 this.obrokService.update(id,ime,opis,meniId);
102 return "redirect:/obroci";
103 }
104
105 @PostMapping("/obroci/{id}/delete")
106 public String delete(@PathVariable Integer id) {
107 this.obrokService.delete(id);
108 return "redirect:/obroci";
109 }
110
111
112
113
114
115}
Note: See TracBrowser for help on using the repository browser.