package com.project.beautycenter.web; import com.project.beautycenter.model.Uslugi; import com.project.beautycenter.model.Vraboteni; import com.project.beautycenter.model.VraboteniUslugi; import com.project.beautycenter.model.exceptions.ServiceIdReservedException; import com.project.beautycenter.service.UslugiService; import com.project.beautycenter.service.VraboteniService; import com.project.beautycenter.service.VraboteniUslugiService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller public class UslugiController { private final UslugiService uslugiService; private final VraboteniUslugiService vraboteniUslugiService; private final VraboteniService vraboteniService; public UslugiController(UslugiService uslugiService, VraboteniUslugiService vraboteniUslugiService, VraboteniService vraboteniService) { this.uslugiService = uslugiService; this.vraboteniUslugiService = vraboteniUslugiService; this.vraboteniService = vraboteniService; } @GetMapping("/services") public String showClients(Model model) { List uslugi = this.uslugiService.findAll(); List vrabUslugi = this.vraboteniUslugiService.listAll(); List vraboteni = this.vraboteniService.findAll(); model.addAttribute("uslugi", uslugi); model.addAttribute("vrabUslugi", vrabUslugi); model.addAttribute("vraboteni", vraboteni); return "services.html"; } @GetMapping("/services/add") public String showAdd(@RequestParam(required = false) String error, Model model) { List vraboteni = this.vraboteniService.findAll(); model.addAttribute("vraboteni", vraboteni); model.addAttribute("hasError", error != null); model.addAttribute("error", error); return "createService.html"; } @GetMapping("/services/{id}/edit") public String showEdit(@PathVariable String id, Model model) { Uslugi usluga = this.uslugiService.findbyId(id); List vraboteni = this.vraboteniService.findAll(); List vrabUsluga = this.vraboteniUslugiService.getVraboteniByUsluga(usluga); model.addAttribute("vraboteni", vraboteni); model.addAttribute("usluga", usluga); model.addAttribute("vrabUsluga", vrabUsluga); return "services_form.html"; } @PostMapping("/services") public String create(@RequestParam String usluga_id, @RequestParam String dejnost, @RequestParam String cena, @RequestParam List vraboteniId) { try { this.uslugiService.create(usluga_id, dejnost, cena, vraboteniId); return "redirect:/services"; } catch (ServiceIdReservedException exception) { return "redirect:/services/add?error=" + exception.getMessage(); } } @PostMapping("/services/{id}") public String update(@PathVariable String id, @RequestParam String dejnost, @RequestParam String cena, @RequestParam(required = false) List vraboteniId) { this.uslugiService.update(id, dejnost, cena, vraboteniId); return "redirect:/services"; } @PostMapping({"/services/{id}/delete"}) public String delete(@PathVariable String id) { this.uslugiService.delete(id); return "redirect:/services"; } }