1 | package com.example.eatys_app.controller;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.eatys_app.model.Menadzer;
|
---|
5 | import com.example.eatys_app.model.Restoran;
|
---|
6 | import com.example.eatys_app.model.Rezervacija;
|
---|
7 | import com.example.eatys_app.service.RestoranService;
|
---|
8 | import com.example.eatys_app.service.RezervacijaService;
|
---|
9 | import jakarta.servlet.http.HttpServletRequest;
|
---|
10 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
11 | import org.springframework.security.core.Authentication;
|
---|
12 | import org.springframework.stereotype.Controller;
|
---|
13 | import org.springframework.ui.Model;
|
---|
14 | import org.springframework.web.bind.annotation.*;
|
---|
15 |
|
---|
16 | import java.sql.Timestamp;
|
---|
17 | import java.util.Date;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | @Controller
|
---|
21 | @RequestMapping("/rezervacii")
|
---|
22 | public class RezervacijaController {
|
---|
23 |
|
---|
24 | private final RezervacijaService rezervacijaService;
|
---|
25 | private final RestoranService restoranService;
|
---|
26 |
|
---|
27 |
|
---|
28 | public RezervacijaController(RezervacijaService rezervacijaService, RestoranService restoranService) {
|
---|
29 | this.rezervacijaService = rezervacijaService;
|
---|
30 | this.restoranService = restoranService;
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | @GetMapping
|
---|
35 | public String getRezervaciiPage(@RequestParam(required = false) String error,
|
---|
36 | HttpServletRequest req,
|
---|
37 | Model model) {
|
---|
38 | if (error != null && !error.isEmpty()) {
|
---|
39 | model.addAttribute("hasError", true);
|
---|
40 | model.addAttribute("error", error);
|
---|
41 | }
|
---|
42 | String username = req.getRemoteUser();
|
---|
43 | List<Rezervacija> rezervacii= this.rezervacijaService.getActiveRezervacii(username);
|
---|
44 | model.addAttribute("rezervacii", rezervacii);
|
---|
45 | return "rezervacii.html";
|
---|
46 | }
|
---|
47 |
|
---|
48 | @GetMapping("/add")
|
---|
49 | public String showAdd(Model model) {
|
---|
50 | List<Restoran> restorani=this.restoranService.listAll();
|
---|
51 | model.addAttribute("restorani", restorani);
|
---|
52 | return "formRezervacija.html";
|
---|
53 | }
|
---|
54 |
|
---|
55 | @GetMapping("/{id}/edit")
|
---|
56 | public String showEdit(@PathVariable Integer id, Model model) {
|
---|
57 | Rezervacija rezervacija= this.rezervacijaService.findById(id);
|
---|
58 | List<Restoran> restorani=this.restoranService.listAll();
|
---|
59 | model.addAttribute("rezervacija", rezervacija);
|
---|
60 | model.addAttribute("restorani", restorani);
|
---|
61 | return "formRezervacija.html";
|
---|
62 | }
|
---|
63 |
|
---|
64 | @PostMapping("/add-rezervacija/")
|
---|
65 | public String addRezervacija(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date vreme,
|
---|
66 | @RequestParam Integer lugje,
|
---|
67 | @RequestParam String opis,
|
---|
68 | @RequestParam Integer restoranId,
|
---|
69 | Authentication authentication, HttpServletRequest req) {
|
---|
70 | try {
|
---|
71 | String username = req.getRemoteUser();
|
---|
72 | this.rezervacijaService.create(username,vreme,lugje,opis,restoranId);
|
---|
73 | return "redirect:/rezervacii";
|
---|
74 | } catch (RuntimeException exception) {
|
---|
75 | return "redirect:/rezervacii?error=" + exception.getMessage();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | @PostMapping("/add-rezervacija/{id}")
|
---|
80 | public String editRezervacija(@PathVariable Integer id,
|
---|
81 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date vreme,
|
---|
82 | @RequestParam Integer lugje,
|
---|
83 | @RequestParam String opis,
|
---|
84 | @RequestParam Integer restoranId,
|
---|
85 | Authentication authentication, HttpServletRequest req) {
|
---|
86 | try {
|
---|
87 | String username = req.getRemoteUser();
|
---|
88 | this.rezervacijaService.update(id,username,vreme,lugje,opis,restoranId);
|
---|
89 | return "redirect:/rezervacii";
|
---|
90 | } catch (RuntimeException exception) {
|
---|
91 | return "redirect:/rezervacii?error=" + exception.getMessage();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | @PostMapping("/{id}/delete")
|
---|
96 | public String delete(@PathVariable Integer id) {
|
---|
97 | this.rezervacijaService.delete(id);
|
---|
98 | return "redirect:/rezervacii";
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|