добавленна проверка по логину

This commit is contained in:
Николай 2023-05-11 16:29:35 +04:00
parent b5c2db3e02
commit 1317a2824d
5 changed files with 76 additions and 53 deletions

View File

@ -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";
}
}

View File

@ -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))

View File

@ -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";
}
}

View File

@ -7,7 +7,7 @@
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form th:if="${controller != 'creator/'}" action="#" th:action="@{/creatorAction/manga/{id}(id=${id})}" th:object="${mangaDto}" enctype="multipart/form-data" method="post">
<form th:if="${controller != 'creator/'}" action="#" th:action="@{/creatorAction/manga/{id}/{login}(login=${login}, id=${id})}" th:object="${mangaDto}" enctype="multipart/form-data" method="post">
<div class="mb-3" th:if="${controller == 'creator/'}">
<label for="mangaNameU" class="form-label">mangaName</label>
<input id="mangaNameU" type='text' class="form-control" th:field="${mangaDto.mangaName}" required="true"/>

View File

@ -45,7 +45,7 @@
</h4>
</div>
<div>
<a class="btn btn-primary" th:href="@{/creatorAction/edit/{id}(id=${manga.id})}">
<a class="btn btn-primary" th:href="@{/creatorAction/edit/{id}/{login}(id=${manga.id}, login=${login})}">
<i class="fas fa-edit"></i>
</a>
</button>
@ -53,7 +53,7 @@
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${manga.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
<form th:action="@{/creatorAction/delete/{id}(id=${manga.id})}" method="post">
<form th:action="@{/creatorAction/delete/{id}/{login}(id=${manga.id}, login=${login})}" method="post">
<button th:id="'remove-' + ${manga.id}" type="submit" style="display: none">
Удалить
</button>