source: src/main/java/com/example/eatys_app/controller/RezervacijaController.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: 4.0 KB
Line 
1package com.example.eatys_app.controller;
2
3
4import com.example.eatys_app.model.Menadzer;
5import com.example.eatys_app.model.Restoran;
6import com.example.eatys_app.model.Rezervacija;
7import com.example.eatys_app.service.RestoranService;
8import com.example.eatys_app.service.RezervacijaService;
9import jakarta.servlet.http.HttpServletRequest;
10import org.springframework.format.annotation.DateTimeFormat;
11import org.springframework.security.core.Authentication;
12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
14import org.springframework.web.bind.annotation.*;
15
16import java.sql.Timestamp;
17import java.util.Date;
18import java.util.List;
19
20@Controller
21@RequestMapping("/rezervacii")
22public 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}
Note: See TracBrowser for help on using the repository browser.