теперь вместо id login

This commit is contained in:
Николай 2023-05-11 16:09:51 +04:00
parent 0a6ba9f820
commit b5c2db3e02
12 changed files with 114 additions and 1845 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.security.Principal;
import java.util.Base64;
@Controller
@ -32,27 +33,18 @@ public class CreatorActionMvcController {
this.mangaService = mangaService;
}
@GetMapping("/{user}")
public String getCreator(@PathVariable String user, Model model) {
@GetMapping()
public String getCreator(@RequestParam("login") String login, Model model, Principal principal) {
if (login.equals(principal.getName())) {
model.addAttribute("creators",
creatorService.findAllCreators().stream()
.map(CreatorMangaDto::new)
.toList());
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findByLogin(user));
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findByLogin(login));
model.addAttribute("currentCreator", currentCreator);
model.addAttribute("creatorId", currentCreator.getId());
model.addAttribute("login", login);
return "creatorAction";
}
@GetMapping()
public String getCreator(@RequestParam("creatorId") Long creatorId, Model model) {
model.addAttribute("creators",
creatorService.findAllCreators().stream()
.map(CreatorMangaDto::new)
.toList());
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findCreator(creatorId));
model.addAttribute("currentCreator", currentCreator);
model.addAttribute("creatorId", currentCreator.getId());
return "creatorAction";
}
@ -64,16 +56,16 @@ public class CreatorActionMvcController {
return "creatorAction-edit";
}
@GetMapping("/create/{id}")
public String createManga(@PathVariable Long id, Model model) {
model.addAttribute("Id", id);
@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";
}
@PostMapping( "/creator/{creatorId}")
public String saveManga(@PathVariable(value = "creatorId", required = false) Long creatorId,
@PostMapping( "/creator/{login}")
public String saveManga(@PathVariable(value = "login", required = false) String login,
@RequestParam("multipartFile") MultipartFile multipartFile,
@ModelAttribute @Valid MangaDto mangaDto,
BindingResult bindingResult,
@ -83,9 +75,9 @@ public class CreatorActionMvcController {
return "creatorAction-edit";
}
mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes()));
mangaDto.setCreatorId(creatorId);
mangaDto.setLogin(login);
mangaService.addManga(mangaDto);
return "redirect:/creatorAction?creatorId=" + creatorId;
return "redirect:/creatorAction?login=" + login;
}
@ -100,13 +92,13 @@ public class CreatorActionMvcController {
}
mangaDto.setImage("data:" + multipartFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(multipartFile.getBytes()));
mangaService.updateManga(mangaId, mangaDto.getChapterCount(), mangaDto.getImage());
return "redirect:/creatorAction?creatorId=" + mangaService.findManga(mangaId).getCreatorId();
return "redirect:/creatorAction?login=" + creatorService.findCreator(mangaService.findManga(mangaId).getCreatorId()).getUser().getLogin();
}
@PostMapping("/delete/{id}")
public String deleteCreator(@PathVariable Long id) {
Long creatorId = mangaService.findManga(id).getCreatorId();
mangaService.deleteManga(id);
@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 {

View File

@ -1,5 +1,6 @@
package com.LabWork.app.MangaStore.controller.Reader;
import com.LabWork.app.MangaStore.model.Dto.CreatorMangaDto;
import com.LabWork.app.MangaStore.model.Dto.ReaderMangaDto;
import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto;
import com.LabWork.app.MangaStore.service.ReaderService;
@ -15,6 +16,8 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
@Controller
@RequestMapping("/readerAction")
@Secured({UserRole.AsString.USER})
@ -28,21 +31,25 @@ public class ReaderActionMvcController {
this.mangaService = mangaService;
}
@GetMapping("/{user}")
public String getReader(@PathVariable String user, Model model) {
@GetMapping()
public String getReader(@RequestParam("readerLogin") String readerLogin, Model model, Principal principal) {
if (readerLogin.equals(principal.getName())) {
model.addAttribute("readers",
readerService.findAllReaders().stream()
.map(ReaderMangaDto::new)
.toList());
ReaderMangaDto currentReader = new ReaderMangaDto(readerService.findByLogin(user));
ReaderMangaDto currentReader = new ReaderMangaDto(readerService.findByLogin(readerLogin));
model.addAttribute("readerLogin", readerLogin);
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()
/* @GetMapping()
public String getReader(@RequestParam(value = "readerId", required = false) Long readerId, Model model) {
model.addAttribute("readers",
readerService.findAllReaders().stream()
@ -58,10 +65,10 @@ public class ReaderActionMvcController {
model.addAttribute("MangaDto", new MangaDto());
model.addAttribute("mangaList", mangaService.findAllMangas());
return "readerAction";
}
}*/
@PostMapping("/manga/{readerId}")
public String saveManga(@PathVariable Long readerId,
@PostMapping("/manga/{readerLogin}")
public String saveManga(@PathVariable String readerLogin,
@RequestParam("mangaId") Long mangaId,
@ModelAttribute @Valid MangaDto MangaDto,
BindingResult bindingResult,
@ -70,13 +77,13 @@ public class ReaderActionMvcController {
model.addAttribute("errors", bindingResult.getAllErrors());
return "readerAction";
}
readerService.addManga(mangaId, readerId);
return "redirect:/readerAction/?readerId=" + readerId;
readerService.addManga(mangaId, readerLogin);
return "redirect:/readerAction/?readerLogin=" + readerLogin;
}
@PostMapping("/{id}/removeManga/{mangaId}")
public String removeManga(@PathVariable Long id, @PathVariable Long mangaId) {
readerService.removeManga(mangaId, id);
return "redirect:/readerAction/?readerId=" + id;
@PostMapping("/{readerLogin}/removeManga/{mangaId}")
public String removeManga(@PathVariable String readerLogin, @PathVariable Long mangaId) {
readerService.removeManga(mangaId, readerLogin);
return "redirect:/readerAction/?readerLogin=" + readerLogin;
}
}

View File

@ -14,6 +14,8 @@ public class MangaDto {
private Integer chapterCount;
private String image;
private String login;
public MangaDto() {
}
@ -22,6 +24,7 @@ public class MangaDto {
this.creatorId = manga.getCreator().getId();
this.mangaName = manga.getMangaName();
this.chapterCount = manga.getChapterCount();
this.login = null;
this.image = new String(manga.getImage(), StandardCharsets.UTF_8);
}
public Long getId() {
@ -44,6 +47,10 @@ public class MangaDto {
return image;
}
public String getLogin() {
return login;
}
public void setMangaName(String mangaName) {
this.mangaName = mangaName;
}
@ -57,4 +64,10 @@ public class MangaDto {
}
public void setImage(String image) {this.image = image;}
public void setLogin(String login) {
this.login = login;
}
}

View File

@ -8,6 +8,7 @@ import com.LabWork.app.MangaStore.service.Repository.MangaRepository;
import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException;
import com.LabWork.app.MangaStore.service.Exception.MangaNotFoundException;
import com.LabWork.app.MangaStore.service.Repository.ReaderRepository;
import com.LabWork.app.MangaStore.service.Repository.UserRepository;
import com.LabWork.app.MangaStore.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -18,17 +19,20 @@ import java.util.Optional;
public class MangaService {
public final MangaRepository mangaRepository;
public final CreatorRepository creatorRepository;
public final UserRepository userRepository;
public final ReaderService readerService;
private final ValidatorUtil validatorUtil;
public MangaService(MangaRepository mangaRepository,
CreatorRepository creatorRepository,
ReaderService readerService,
ValidatorUtil validatorUtil) {
ValidatorUtil validatorUtil,
UserRepository userRepository) {
this.mangaRepository = mangaRepository;
this.readerService = readerService;
this.creatorRepository = creatorRepository;
this.validatorUtil = validatorUtil;
this.userRepository = userRepository;
}
@Transactional(readOnly = true)
@ -36,6 +40,9 @@ public class MangaService {
final Optional<Manga> manga = mangaRepository.findById(id);
return manga.orElseThrow(() -> new MangaNotFoundException(id));
}
public Creator findCreatorByLogin(String login) {
return findCreator(userRepository.findOneByLoginIgnoreCase(login).getId());
}
@Transactional
public List<Reader> getReader(Long id) {
@ -57,7 +64,7 @@ public class MangaService {
@Transactional
public Manga addManga(MangaDto mangaDto) {
final Creator currentCreator = findCreator(mangaDto.getCreatorId());
final Creator currentCreator = findCreatorByLogin(mangaDto.getLogin());
final Manga manga = new Manga(currentCreator, mangaDto);
validatorUtil.validate(manga);
return mangaRepository.save(manga);

View File

@ -62,9 +62,9 @@ public class ReaderService {
}
@Transactional
public Manga addManga(Long mangaId, Long readerId) {
public Manga addManga(Long mangaId, String readerLogin) {
final Manga manga = findManga(mangaId);
final Reader reader = findReader(readerId);
final Reader reader = findByLogin(readerLogin);
validatorUtil.validate(reader);
if (reader.getMangas().contains(manga))
{
@ -75,8 +75,8 @@ public class ReaderService {
}
@Transactional
public Manga removeManga(Long mangaId, Long readerId) {
final Reader currentReader = findReader(readerId);
public Manga removeManga(Long mangaId, String readerLogin) {
final Reader currentReader = findByLogin(readerLogin);
final Manga currentManga = findManga(mangaId);
currentReader.getMangas().remove(currentManga);
return currentManga;

View File

@ -7,7 +7,30 @@
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/creatorAction/{controller}{id}(id=${id}, controller=${controller})}" 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"/>
</div>
<div class="mb-3">
<label for="chapterCountU" class="form-label">chapterCount</label>
<input id="chapterCountU" type='number' class="form-control" th:field="${mangaDto.chapterCount}" required="true"/>
</div>
<div class="mb-3">
<label for="multipartFileU" class="form-label">image</label>
<input type="file" id="multipartFileU" th:name="multipartFile" accept="image/png, image/jpeg" class="form-control" required="true"/>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span>Обновить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/creator}">
Назад
</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">
<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"/>
@ -23,8 +46,7 @@
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span th:if="${controller == 'creator/'}">Добавить</span>
<span th:if="${controller != 'creator/'}">Обновить</span>
<span>Добавить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/creator}">
Назад

View File

@ -14,7 +14,7 @@
<div class="d-flex mt-3">
<div class="d-grid col-sm-2">
<a class="btn btn-success"
th:href="@{/creatorAction/create/{id}(id=${creatorId})}">
th:href="@{/creatorAction/create/{login}(login=${login})}">
<i class="fa-solid fa-plus"></i> Добавить
</a>
</div>
@ -24,9 +24,9 @@
<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="creatorId" class="form-select">
<select id="creatorId" th:name="login" class="form-select">
<option value="" disabled selected>Select your option</option>
<option th:each="value: ${creators}" th:selected="${creatorId} == ${value}" th:text="${value.creatorName}" th:value="${value.Id}">
<option th:each="value: ${creators}" th:selected="${creatorName} == ${value}" th:text="${value.creatorName}" th:value="${value.creatorName}">
</option>
</select>
</div>

View File

@ -29,9 +29,10 @@
<ul class="navbar-nav" th:with="activeLink=${#request.requestURI}" sec:authorize="isAuthenticated()">
<a class="nav-link" href="/"
th:classappend="${#strings.equals(activeLink, '/')} ? 'active' : ''">Index</a>
<a sec:authorize="hasRole('ROLE_ADMIN')" class="nav-link" th:href="@{/creatorAction/{login}(login=${#authentication.name})}"
<!--(login=${#authentication.name})-->
<a sec:authorize="hasRole('ROLE_ADMIN')" class="nav-link" th:href="@{/creatorAction(login=${#authentication.name})}"
th:classappend="${#strings.equals(activeLink, '/creatorAction')} ? 'active' : ''">CreatorAction</a>
<a sec:authorize="hasRole('ROLE_USER')" class="nav-link" th:href="@{/readerAction/{login}(login=${#authentication.name})}"
<a sec:authorize="hasRole('ROLE_USER')" class="nav-link" th:href="@{/readerAction(readerLogin=${#authentication.name})}"
th:classappend="${#strings.equals(activeLink, '/readerAction')} ? 'active' : ''">ReaderAction</a>
<a class="nav-link" href="/manga"
th:classappend="${#strings.equals(activeLink, '/manga')} ? 'active' : ''">Catalog</a>

View File

@ -45,7 +45,7 @@
<div>
<div class="pt-3 description mb-3 fs-6 fw-bold">
Имя пользователя:
<a th:href="@{/readerAction/(readerId=${reader.id})}" th:text="${reader.readerName}"></a>
<a th:href="@{/readerAction/(readerLogin=${reader.readerName})}" th:text="${reader.readerName}"></a>
</div>
</div>
</div>

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/{id}(id=${readerId})}" method="post">
<form action="#" th:action="@{/readerAction/manga/{readerLogin}(readerLogin=${readerLogin})}" method="post">
<div class="mb-3">
<label for="mangaId" class="form-label">Манга</label>
<select th:name="mangaId" id="mangaId" class="form-select">
@ -32,9 +32,9 @@
<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="readerId" class="form-select">
<select id="readerId" th:name="login" class="form-select">
<option value="" disabled selected>Select your option</option>
<option th:each="value: ${readers}" th:selected="${readerId} == ${value}" th:text="${value.readerName}" th:value="${value.Id}">
<option th:each="value: ${readers}" th:selected="${login} == ${readerName}" th:text="${value.readerName}" th:value="${value.readerName}">
</option>
</select>
</div>
@ -58,7 +58,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/' + ${readerId} + '/removeManga/' + ${manga.id}}" method="post">
<form th:action="@{'/readerAction/' + ${readerLogin} + '/removeManga/' + ${manga.id}}" method="post">
<button th:id="'remove-' + ${manga.id}" type="submit" style="display: none">
Удалить
</button>