diff --git a/src/main/java/com/LabWork/app/MangaStore/controller/Creator/CreatorActionMvcController.java b/src/main/java/com/LabWork/app/MangaStore/controller/Creator/CreatorActionMvcController.java index 8be4215..e97f346 100644 --- a/src/main/java/com/LabWork/app/MangaStore/controller/Creator/CreatorActionMvcController.java +++ b/src/main/java/com/LabWork/app/MangaStore/controller/Creator/CreatorActionMvcController.java @@ -48,20 +48,26 @@ public class CreatorActionMvcController { return "creatorAction"; } - @GetMapping("/edit/{id}") - public String editManga(@PathVariable Long id, Model model) { - model.addAttribute("Id", id); - model.addAttribute("mangaDto", new MangaDto(mangaService.findManga(id))); - model.addAttribute("controller", "manga/"); - return "creatorAction-edit"; + @GetMapping("/edit/{id}/{login}") + public String editManga(@PathVariable Long id, @PathVariable String login, Model model, Principal principal) { + if (login.equals(principal.getName())) { + model.addAttribute("Id", id); + model.addAttribute("mangaDto", new MangaDto(mangaService.findManga(id))); + model.addAttribute("controller", "manga/"); + return "creatorAction-edit"; + } + return "creatorAction"; } @GetMapping("/create/{login}") - public String createManga(@PathVariable String login, Model model) { - model.addAttribute("login", login); - model.addAttribute("mangaDto", new MangaDto()); - model.addAttribute("controller", "creator/"); - return "creatorAction-edit"; + public String createManga(@PathVariable String login, Model model, Principal principal) { + if (login.equals(principal.getName())) { + model.addAttribute("login", login); + model.addAttribute("mangaDto", new MangaDto()); + model.addAttribute("controller", "creator/"); + return "creatorAction-edit"; + } + return "creatorAction"; } @PostMapping( "/creator/{login}") @@ -69,40 +75,50 @@ public class CreatorActionMvcController { @RequestParam("multipartFile") MultipartFile multipartFile, @ModelAttribute @Valid MangaDto mangaDto, BindingResult bindingResult, - Model model) throws IOException { - if (bindingResult.hasErrors()) { - model.addAttribute("errors", bindingResult.getAllErrors()); - return "creatorAction-edit"; + Model model, + Principal principal) throws IOException { + if (login.equals(principal.getName())) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "creatorAction-edit"; + } + mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes())); + mangaDto.setLogin(login); + mangaService.addManga(mangaDto); + return "redirect:/creatorAction?login=" + login; } - mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes())); - mangaDto.setLogin(login); - mangaService.addManga(mangaDto); - return "redirect:/creatorAction?login=" + login; - + return "creatorAction"; } - @PostMapping( "/manga/{mangaId}") - public String updateManga(@PathVariable(value = "mangaId", required = false) Long mangaId, @RequestParam("multipartFile") MultipartFile multipartFile, - @ModelAttribute @Valid MangaDto mangaDto, - BindingResult bindingResult, - Model model) throws IOException { - if (bindingResult.hasErrors()) { - model.addAttribute("errors", bindingResult.getAllErrors()); - return "creatorAction-edit"; + @PostMapping( "/manga/{mangaId}/{login}") + public String updateManga(@PathVariable(value = "mangaId", required = false) Long mangaId, @PathVariable(value = "login", required = false) String login, @RequestParam("multipartFile") MultipartFile multipartFile, + @ModelAttribute @Valid MangaDto mangaDto, + BindingResult bindingResult, + Model model, + Principal principal) throws IOException { + if (login.equals(principal.getName())) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "creatorAction-edit"; + } + mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes())); + mangaService.updateManga(mangaId, mangaDto.getChapterCount(), mangaDto.getImage()); + return "redirect:/creatorAction?login=" + login; } - mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes())); - mangaService.updateManga(mangaId, mangaDto.getChapterCount(), mangaDto.getImage()); - return "redirect:/creatorAction?login=" + creatorService.findCreator(mangaService.findManga(mangaId).getCreatorId()).getUser().getLogin(); + return "creatorAction"; } - @PostMapping("/delete/{mangaId}") - public String deleteCreator(@PathVariable Long mangaId) { - Long creatorId = mangaService.findManga(mangaId).getCreatorId(); - mangaService.deleteManga(mangaId); - if (creatorId != null){ - return "redirect:/creatorAction?creatorId=" + creatorId; - } else { - return "redirect:/creatorAction"; + @PostMapping("/delete/{mangaId}/{login}") + public String deleteCreator(@PathVariable Long mangaId, @PathVariable String login,Principal principal) { + if (login.equals(principal.getName())) { + Long creatorId = mangaService.findManga(mangaId).getCreatorId(); + mangaService.deleteManga(mangaId); + if (creatorId != null){ + return "redirect:/creatorAction?login=" + login; + } else { + return "redirect:/creatorAction"; + } } + return "creatorAction"; } } diff --git a/src/main/java/com/LabWork/app/MangaStore/controller/Manga/MangaMvcController.java b/src/main/java/com/LabWork/app/MangaStore/controller/Manga/MangaMvcController.java index 858810a..bcf7ca5 100644 --- a/src/main/java/com/LabWork/app/MangaStore/controller/Manga/MangaMvcController.java +++ b/src/main/java/com/LabWork/app/MangaStore/controller/Manga/MangaMvcController.java @@ -20,7 +20,7 @@ public class MangaMvcController { } @GetMapping() - public String getMangaAnfReaders(Model model) { + public String getMangaAndReaders(Model model) { model.addAttribute("mangaList", mangaService.findAllMangas().stream() .map(x -> new MangaDto(x)) .toList()); @@ -28,7 +28,7 @@ public class MangaMvcController { } @GetMapping("/{id}") - public String getMangaAnfReaders(@PathVariable Long id, Model model) { + public String getMangaAndReaders(@PathVariable Long id, Model model) { model.addAttribute("manga", new MangaReaderDto(mangaService.findManga(id), mangaService.getReader(id))); model.addAttribute("readers", mangaService.getReader(id).stream() .map(x -> new ReaderMangaDto(x)) diff --git a/src/main/java/com/LabWork/app/MangaStore/controller/Reader/ReaderActionMvcController.java b/src/main/java/com/LabWork/app/MangaStore/controller/Reader/ReaderActionMvcController.java index 6ffebb1..fdd878e 100644 --- a/src/main/java/com/LabWork/app/MangaStore/controller/Reader/ReaderActionMvcController.java +++ b/src/main/java/com/LabWork/app/MangaStore/controller/Reader/ReaderActionMvcController.java @@ -72,18 +72,25 @@ public class ReaderActionMvcController { @RequestParam("mangaId") Long mangaId, @ModelAttribute @Valid MangaDto MangaDto, BindingResult bindingResult, - Model model){ - if (bindingResult.hasErrors()) { - model.addAttribute("errors", bindingResult.getAllErrors()); - return "readerAction"; + Model model, + Principal principal){ + if (readerLogin.equals(principal.getName())) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "readerAction"; + } + readerService.addManga(mangaId, readerLogin); + return "redirect:/readerAction/?readerLogin=" + readerLogin; } - readerService.addManga(mangaId, readerLogin); - return "redirect:/readerAction/?readerLogin=" + readerLogin; + return "readerAction"; } @PostMapping("/{readerLogin}/removeManga/{mangaId}") - public String removeManga(@PathVariable String readerLogin, @PathVariable Long mangaId) { - readerService.removeManga(mangaId, readerLogin); - return "redirect:/readerAction/?readerLogin=" + readerLogin; + public String removeManga(@PathVariable String readerLogin, @PathVariable Long mangaId, Principal principal) { + if (readerLogin.equals(principal.getName())) { + readerService.removeManga(mangaId, readerLogin); + return "redirect:/readerAction/?readerLogin=" + readerLogin; + } + return "readerAction"; } } diff --git a/src/main/resources/templates/creatorAction-edit.html b/src/main/resources/templates/creatorAction-edit.html index 8268f91..b7e664d 100644 --- a/src/main/resources/templates/creatorAction-edit.html +++ b/src/main/resources/templates/creatorAction-edit.html @@ -7,7 +7,7 @@