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

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()
public String getCreator(@RequestParam("login") String login, Model model, Principal principal) {
if (login.equals(principal.getName())) {
public String getCreator(Model model, Principal principal) {
model.addAttribute("creators",
creatorService.findAllCreators().stream()
.map(CreatorMangaDto::new)
.toList());
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findByLogin(login));
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findByLogin(principal.getName()));
model.addAttribute("currentCreator", currentCreator);
model.addAttribute("login", login);
return "creatorAction";
}
return "creatorAction";
}
@GetMapping("/edit/{id}/{login}")
public String editManga(@PathVariable Long id, @PathVariable String login, Model model, Principal principal) {
if (login.equals(principal.getName())) {
@GetMapping("/edit/{id}")
public String editManga(@PathVariable Long id, Model model, Principal principal) {
if (principal.getName().equals(principal.getName())) {
model.addAttribute("Id", id);
model.addAttribute("mangaDto", new MangaDto(mangaService.findManga(id)));
model.addAttribute("controller", "manga/");
@ -59,66 +55,52 @@ public class CreatorActionMvcController {
return "creatorAction";
}
@GetMapping("/create/{login}")
public String createManga(@PathVariable String login, Model model, Principal principal) {
if (login.equals(principal.getName())) {
model.addAttribute("login", login);
@GetMapping("/create")
public String createManga(Model model, Principal principal) {
model.addAttribute("mangaDto", new MangaDto());
model.addAttribute("controller", "creator/");
return "creatorAction-edit";
}
return "creatorAction";
}
@PostMapping( "/creator/{login}")
public String saveManga(@PathVariable(value = "login", required = false) String login,
@RequestParam("multipartFile") MultipartFile multipartFile,
@PostMapping( "/creator")
public String saveManga(@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()));
mangaDto.setLogin(login);
mangaDto.setLogin(principal.getName());
mangaService.addManga(mangaDto);
return "redirect:/creatorAction?login=" + login;
}
return "creatorAction";
return "redirect:/creatorAction";
}
@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,
@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,
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;
}
return "creatorAction";
return "redirect:/creatorAction";
}
@PostMapping("/delete/{mangaId}/{login}")
public String deleteCreator(@PathVariable Long mangaId, @PathVariable String login,Principal principal) {
if (login.equals(principal.getName())) {
@PostMapping("/delete/{mangaId}")
public String deleteCreator(@PathVariable Long mangaId, Principal principal) {
Long creatorId = mangaService.findManga(mangaId).getCreatorId();
mangaService.deleteManga(mangaId);
if (creatorId != null){
return "redirect:/creatorAction?login=" + login;
return "redirect:/creatorAction";
} else {
return "redirect:/creatorAction";
}
}
return "creatorAction";
}
}

View File

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

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}/{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/'}">
<label for="mangaNameU" class="form-label">mangaName</label>
<input id="mangaNameU" type='text' class="form-control" th:field="${mangaDto.mangaName}" required="true"/>
@ -30,7 +30,7 @@
</a>
</div>
</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/'}">
<label for="mangaName" class="form-label">mangaName</label>
<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-grid col-sm-2">
<a class="btn btn-success"
th:href="@{/creatorAction/create/{login}(login=${login})}">
th:href="@{/creatorAction/create}">
<i class="fa-solid fa-plus"></i> Добавить
</a>
</div>
</div>
</form>
<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 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">
@ -45,7 +35,7 @@
</h4>
</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>
</a>
</button>
@ -53,7 +43,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}/{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>

View File

@ -13,7 +13,7 @@
<div class="d-flex mt-3">
<div class="d-grid col-sm-2">
<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">
<label for="mangaId" class="form-label">Манга</label>
<select th:name="mangaId" id="mangaId" class="form-select">
@ -29,16 +29,6 @@
</div>
</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 th:each="manga: ${reader?.mangas}" class="d-flex flex-row flex-wrap flex-grow-1 align-items-center mt-3">
<div class="me-3">
@ -58,7 +48,7 @@
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${manga.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</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>