1 | package com.project.beautycenter.web;
|
---|
2 |
|
---|
3 | import com.project.beautycenter.model.Uslugi;
|
---|
4 | import com.project.beautycenter.model.Vraboteni;
|
---|
5 | import com.project.beautycenter.model.VraboteniUslugi;
|
---|
6 | import com.project.beautycenter.model.exceptions.ServiceIdReservedException;
|
---|
7 | import com.project.beautycenter.service.UslugiService;
|
---|
8 | import com.project.beautycenter.service.VraboteniService;
|
---|
9 | import com.project.beautycenter.service.VraboteniUslugiService;
|
---|
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 | @Controller
|
---|
20 | public class UslugiController {
|
---|
21 | private final UslugiService uslugiService;
|
---|
22 | private final VraboteniUslugiService vraboteniUslugiService;
|
---|
23 | private final VraboteniService vraboteniService;
|
---|
24 |
|
---|
25 |
|
---|
26 | public UslugiController(UslugiService uslugiService, VraboteniUslugiService vraboteniUslugiService, VraboteniService vraboteniService) {
|
---|
27 | this.uslugiService = uslugiService;
|
---|
28 | this.vraboteniUslugiService = vraboteniUslugiService;
|
---|
29 | this.vraboteniService = vraboteniService;
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | @GetMapping("/services")
|
---|
34 | public String showClients(Model model) {
|
---|
35 | List<Uslugi> uslugi = this.uslugiService.findAll();
|
---|
36 | List<VraboteniUslugi> vrabUslugi = this.vraboteniUslugiService.listAll();
|
---|
37 | List<Vraboteni> vraboteni = this.vraboteniService.findAll();
|
---|
38 | model.addAttribute("uslugi", uslugi);
|
---|
39 | model.addAttribute("vrabUslugi", vrabUslugi);
|
---|
40 | model.addAttribute("vraboteni", vraboteni);
|
---|
41 | return "services.html";
|
---|
42 | }
|
---|
43 |
|
---|
44 | @GetMapping("/services/add")
|
---|
45 | public String showAdd(@RequestParam(required = false) String error,
|
---|
46 | Model model) {
|
---|
47 | List<Vraboteni> vraboteni = this.vraboteniService.findAll();
|
---|
48 | model.addAttribute("vraboteni", vraboteni);
|
---|
49 | model.addAttribute("hasError", error != null);
|
---|
50 | model.addAttribute("error", error);
|
---|
51 |
|
---|
52 | return "createService.html";
|
---|
53 | }
|
---|
54 |
|
---|
55 | @GetMapping("/services/{id}/edit")
|
---|
56 | public String showEdit(@PathVariable String id, Model model) {
|
---|
57 | Uslugi usluga = this.uslugiService.findbyId(id);
|
---|
58 | List<Vraboteni> vraboteni = this.vraboteniService.findAll();
|
---|
59 | List<Vraboteni> vrabUsluga = this.vraboteniUslugiService.getVraboteniByUsluga(usluga);
|
---|
60 |
|
---|
61 | model.addAttribute("vraboteni", vraboteni);
|
---|
62 | model.addAttribute("usluga", usluga);
|
---|
63 | model.addAttribute("vrabUsluga", vrabUsluga);
|
---|
64 |
|
---|
65 | return "services_form.html";
|
---|
66 | }
|
---|
67 |
|
---|
68 | @PostMapping("/services")
|
---|
69 | public String create(@RequestParam String usluga_id,
|
---|
70 | @RequestParam String dejnost,
|
---|
71 | @RequestParam String cena,
|
---|
72 | @RequestParam List<Integer> vraboteniId) {
|
---|
73 |
|
---|
74 | try {
|
---|
75 | this.uslugiService.create(usluga_id, dejnost, cena, vraboteniId);
|
---|
76 | return "redirect:/services";
|
---|
77 | } catch (ServiceIdReservedException exception) {
|
---|
78 |
|
---|
79 | return "redirect:/services/add?error=" + exception.getMessage();
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | @PostMapping("/services/{id}")
|
---|
85 | public String update(@PathVariable String id,
|
---|
86 | @RequestParam String dejnost,
|
---|
87 | @RequestParam String cena,
|
---|
88 | @RequestParam(required = false) List<Integer> vraboteniId) {
|
---|
89 |
|
---|
90 | this.uslugiService.update(id, dejnost, cena, vraboteniId);
|
---|
91 |
|
---|
92 | return "redirect:/services";
|
---|
93 | }
|
---|
94 |
|
---|
95 | @PostMapping({"/services/{id}/delete"})
|
---|
96 | public String delete(@PathVariable String id) {
|
---|
97 |
|
---|
98 | this.uslugiService.delete(id);
|
---|
99 | return "redirect:/services";
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|