сделал красиво, не тестил, скорее всего сломается

This commit is contained in:
Николай 2023-05-11 17:18:01 +04:00
parent b468a84b1a
commit 29605e7fbe
5 changed files with 68 additions and 117 deletions

View File

@ -34,23 +34,19 @@ public class CreatorActionMvcController {
} }
@GetMapping() @GetMapping()
public String getCreator(@RequestParam("login") String login, Model model, Principal principal) { public String getCreator(Model model, Principal principal) {
if (login.equals(principal.getName())) { model.addAttribute("creators",
model.addAttribute("creators", creatorService.findAllCreators().stream()
creatorService.findAllCreators().stream() .map(CreatorMangaDto::new)
.map(CreatorMangaDto::new) .toList());
.toList()); CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findByLogin(principal.getName()));
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findByLogin(login)); model.addAttribute("currentCreator", currentCreator);
model.addAttribute("currentCreator", currentCreator);
model.addAttribute("login", login);
return "creatorAction";
}
return "creatorAction"; return "creatorAction";
} }
@GetMapping("/edit/{id}/{login}") @GetMapping("/edit/{id}")
public String editManga(@PathVariable Long id, @PathVariable String login, Model model, Principal principal) { public String editManga(@PathVariable Long id, Model model, Principal principal) {
if (login.equals(principal.getName())) { if (principal.getName().equals(principal.getName())) {
model.addAttribute("Id", id); model.addAttribute("Id", id);
model.addAttribute("mangaDto", new MangaDto(mangaService.findManga(id))); model.addAttribute("mangaDto", new MangaDto(mangaService.findManga(id)));
model.addAttribute("controller", "manga/"); model.addAttribute("controller", "manga/");
@ -59,66 +55,52 @@ public class CreatorActionMvcController {
return "creatorAction"; return "creatorAction";
} }
@GetMapping("/create/{login}") @GetMapping("/create")
public String createManga(@PathVariable String login, Model model, Principal principal) { public String createManga(Model model, Principal principal) {
if (login.equals(principal.getName())) { model.addAttribute("mangaDto", new MangaDto());
model.addAttribute("login", login); model.addAttribute("controller", "creator/");
model.addAttribute("mangaDto", new MangaDto()); return "creatorAction-edit";
model.addAttribute("controller", "creator/");
return "creatorAction-edit";
}
return "creatorAction";
} }
@PostMapping( "/creator/{login}") @PostMapping( "/creator")
public String saveManga(@PathVariable(value = "login", required = false) String login, public String saveManga(@RequestParam("multipartFile") MultipartFile multipartFile,
@RequestParam("multipartFile") MultipartFile multipartFile,
@ModelAttribute @Valid MangaDto mangaDto, @ModelAttribute @Valid MangaDto mangaDto,
BindingResult bindingResult, BindingResult bindingResult,
Model model, Model model,
Principal principal) throws IOException { Principal principal) throws IOException {
if (login.equals(principal.getName())) { if (bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors());
model.addAttribute("errors", bindingResult.getAllErrors()); return "creatorAction-edit";
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;
} }
return "creatorAction"; mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes()));
mangaDto.setLogin(principal.getName());
mangaService.addManga(mangaDto);
return "redirect:/creatorAction";
} }
@PostMapping( "/manga/{mangaId}/{login}") @PostMapping( "/manga/{mangaId}")
public String updateManga(@PathVariable(value = "mangaId", required = false) Long mangaId, @PathVariable(value = "login", required = false) String login, @RequestParam("multipartFile") MultipartFile multipartFile, public String updateManga(@PathVariable(value = "mangaId", required = false) Long mangaId, @RequestParam("multipartFile") MultipartFile multipartFile,
@ModelAttribute @Valid MangaDto mangaDto, @ModelAttribute @Valid MangaDto mangaDto,
BindingResult bindingResult, BindingResult bindingResult,
Model model, Model model,
Principal principal) throws IOException { Principal principal) throws IOException {
if (login.equals(principal.getName())) { if (bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors());
model.addAttribute("errors", bindingResult.getAllErrors()); return "creatorAction-edit";
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;
} }
return "creatorAction"; mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes()));
mangaService.updateManga(mangaId, mangaDto.getChapterCount(), mangaDto.getImage());
return "redirect:/creatorAction";
} }
@PostMapping("/delete/{mangaId}/{login}") @PostMapping("/delete/{mangaId}")
public String deleteCreator(@PathVariable Long mangaId, @PathVariable String login,Principal principal) { public String deleteCreator(@PathVariable Long mangaId, Principal principal) {
if (login.equals(principal.getName())) { Long creatorId = mangaService.findManga(mangaId).getCreatorId();
Long creatorId = mangaService.findManga(mangaId).getCreatorId(); mangaService.deleteManga(mangaId);
mangaService.deleteManga(mangaId); if (creatorId != null){
if (creatorId != null){ return "redirect:/creatorAction";
return "redirect:/creatorAction?login=" + login; } else {
} else { return "redirect:/creatorAction";
return "redirect:/creatorAction";
}
} }
return "creatorAction";
} }
} }

View File

@ -32,20 +32,16 @@ public class ReaderActionMvcController {
} }
@GetMapping() @GetMapping()
public String getReader(@RequestParam("readerLogin") String readerLogin, Model model, Principal principal) { public String getReader(Model model, Principal principal) {
if (readerLogin.equals(principal.getName())) { model.addAttribute("readers",
model.addAttribute("readers", readerService.findAllReaders().stream()
readerService.findAllReaders().stream() .map(ReaderMangaDto::new)
.map(ReaderMangaDto::new) .toList());
.toList()); ReaderMangaDto currentReader = new ReaderMangaDto(readerService.findByLogin(principal.getName()));
ReaderMangaDto currentReader = new ReaderMangaDto(readerService.findByLogin(readerLogin)); model.addAttribute("readerId", currentReader.getId());
model.addAttribute("readerLogin", readerLogin); model.addAttribute("reader", new ReaderMangaDto(readerService.findReader(currentReader.getId())));
model.addAttribute("readerId", currentReader.getId()); model.addAttribute("MangaDto", new MangaDto());
model.addAttribute("reader", new ReaderMangaDto(readerService.findReader(currentReader.getId()))); model.addAttribute("mangaList", mangaService.findAllMangas());
model.addAttribute("MangaDto", new MangaDto());
model.addAttribute("mangaList", mangaService.findAllMangas());
return "readerAction";
}
return "readerAction"; return "readerAction";
} }
@ -67,30 +63,23 @@ public class ReaderActionMvcController {
return "readerAction"; return "readerAction";
}*/ }*/
@PostMapping("/manga/{readerLogin}") @PostMapping("/manga")
public String saveManga(@PathVariable String readerLogin, public String saveManga(@RequestParam("mangaId") Long mangaId,
@RequestParam("mangaId") Long mangaId,
@ModelAttribute @Valid MangaDto MangaDto, @ModelAttribute @Valid MangaDto MangaDto,
BindingResult bindingResult, BindingResult bindingResult,
Model model, Model model,
Principal principal){ Principal principal){
if (readerLogin.equals(principal.getName())) { if (bindingResult.hasErrors()) {
if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors());
model.addAttribute("errors", bindingResult.getAllErrors()); return "readerAction";
return "readerAction";
}
readerService.addManga(mangaId, readerLogin);
return "redirect:/readerAction/?readerLogin=" + readerLogin;
} }
return "readerAction"; readerService.addManga(mangaId, principal.getName());
return "redirect:/readerAction";
} }
@PostMapping("/{readerLogin}/removeManga/{mangaId}") @PostMapping("/removeManga/{mangaId}")
public String removeManga(@PathVariable String readerLogin, @PathVariable Long mangaId, Principal principal) { public String removeManga(@PathVariable Long mangaId, Principal principal) {
if (readerLogin.equals(principal.getName())) { readerService.removeManga(mangaId, principal.getName());
readerService.removeManga(mangaId, readerLogin); return "redirect:/readerAction/?readerLogin=" + principal.getName();
return "redirect:/readerAction/?readerLogin=" + readerLogin;
}
return "readerAction";
} }
} }

View File

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

View File

@ -14,23 +14,13 @@
<div class="d-flex mt-3"> <div class="d-flex mt-3">
<div class="d-grid col-sm-2"> <div class="d-grid col-sm-2">
<a class="btn btn-success" <a class="btn btn-success"
th:href="@{/creatorAction/create/{login}(login=${login})}"> th:href="@{/creatorAction/create}">
<i class="fa-solid fa-plus"></i> Добавить <i class="fa-solid fa-plus"></i> Добавить
</a> </a>
</div> </div>
</div> </div>
</form> </form>
<div th:text="${errors}" class="margin-bottom alert-danger"></div> <div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/creatorAction}" method="get">
<div class="col-sm-2 mb-3">
<label for="creatorId" class="form-label">Reader</label>
<select id="creatorId" th:name="login" class="form-select">
<option value="" disabled selected>Select your option</option>
<option th:each="value: ${creators}" th:selected="${creatorName} == ${value}" th:text="${value.creatorName}" th:value="${value.creatorName}">
</option>
</select>
</div>
</form>
<div class="row table-responsive text-white"> <div class="row table-responsive text-white">
<div th:each="manga, iterator: ${currentCreator?.mangas}" class="d-flex flex-row flex-wrap flex-grow-1 align-items-center mt-3"> <div th:each="manga, iterator: ${currentCreator?.mangas}" class="d-flex flex-row flex-wrap flex-grow-1 align-items-center mt-3">
<div class="me-3"> <div class="me-3">
@ -45,7 +35,7 @@
</h4> </h4>
</div> </div>
<div> <div>
<a class="btn btn-primary" th:href="@{/creatorAction/edit/{id}/{login}(id=${manga.id}, login=${login})}"> <a class="btn btn-primary" th:href="@{/creatorAction/edit/{id}(id=${manga.id})}">
<i class="fas fa-edit"></i> <i class="fas fa-edit"></i>
</a> </a>
</button> </button>
@ -53,7 +43,7 @@
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${manga.id}').click()|"> th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${manga.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить <i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button> </button>
<form th:action="@{/creatorAction/delete/{id}/{login}(id=${manga.id}, login=${login})}" method="post"> <form th:action="@{/creatorAction/delete/{id}(id=${manga.id})}" method="post">
<button th:id="'remove-' + ${manga.id}" type="submit" style="display: none"> <button th:id="'remove-' + ${manga.id}" type="submit" style="display: none">
Удалить Удалить
</button> </button>

View File

@ -13,7 +13,7 @@
<div class="d-flex mt-3"> <div class="d-flex mt-3">
<div class="d-grid col-sm-2"> <div class="d-grid col-sm-2">
<div th:text="${errors}" class="margin-bottom alert-danger"></div> <div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/readerAction/manga/{readerLogin}(readerLogin=${readerLogin})}" method="post"> <form action="#" th:action="@{/readerAction/manga}" method="post">
<div class="mb-3"> <div class="mb-3">
<label for="mangaId" class="form-label">Манга</label> <label for="mangaId" class="form-label">Манга</label>
<select th:name="mangaId" id="mangaId" class="form-select"> <select th:name="mangaId" id="mangaId" class="form-select">
@ -29,16 +29,6 @@
</div> </div>
</div> </div>
<div th:text="${errors}" class="margin-bottom alert-danger"></div> <div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/readerAction}" method="get">
<div class="col-sm-2 mb-3">
<label for="readerId" class="form-label">Reader</label>
<select id="readerId" th:name="login" class="form-select">
<option value="" disabled selected>Select your option</option>
<option th:each="value: ${readers}" th:selected="${login} == ${readerName}" th:text="${value.readerName}" th:value="${value.readerName}">
</option>
</select>
</div>
</form>
<div class="row table-responsive text-white"> <div class="row table-responsive text-white">
<div th:each="manga: ${reader?.mangas}" class="d-flex flex-row flex-wrap flex-grow-1 align-items-center mt-3"> <div th:each="manga: ${reader?.mangas}" class="d-flex flex-row flex-wrap flex-grow-1 align-items-center mt-3">
<div class="me-3"> <div class="me-3">
@ -58,7 +48,7 @@
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${manga.id}').click()|"> th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${manga.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить <i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button> </button>
<form th:action="@{'/readerAction/' + ${readerLogin} + '/removeManga/' + ${manga.id}}" method="post"> <form th:action="@{'/readerAction/' + 'removeManga/' + ${manga.id}}" method="post">
<button th:id="'remove-' + ${manga.id}" type="submit" style="display: none"> <button th:id="'remove-' + ${manga.id}" type="submit" style="display: none">
Удалить Удалить
</button> </button>