теперь пользователь и создатель объедененны и можно использовать старый функционал

This commit is contained in:
Николай 2023-05-01 13:48:06 +04:00
parent 0463cef7fa
commit ee2f2cd7df
10 changed files with 58 additions and 177 deletions

View File

@ -28,16 +28,27 @@ public class CreatorActionMvcController {
this.mangaService = mangaService;
}
@GetMapping()
public String getCreator(@RequestParam(value = "creatorId", required = false) Long creatorId, Model model) {
@GetMapping("/{user}")
public String getCreator(@PathVariable String user, Model model) {
model.addAttribute("creators",
creatorService.findAllCreators().stream()
.map(CreatorMangaDto::new)
.toList());
if(creatorId != null){
model.addAttribute("currentCreator", new CreatorMangaDto(creatorService.findCreator(creatorId)));
}
model.addAttribute("creatorId", creatorId);
CreatorMangaDto currentCreator = new CreatorMangaDto(creatorService.findCreator(creatorService.findByLogin(user).getId()));
model.addAttribute("currentCreator", currentCreator);
model.addAttribute("creatorId", currentCreator.getId());
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";
}

View File

@ -1,63 +0,0 @@
package com.LabWork.app.MangaStore.controller.Creator;
import com.LabWork.app.MangaStore.model.Dto.CreatorMangaDto;
import com.LabWork.app.MangaStore.service.CreatorService;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/creator")
public class CreatorMvcController {
private final CreatorService creatorService;
public CreatorMvcController(CreatorService creatorService) {
this.creatorService = creatorService;
}
@GetMapping
public String getCreators(Model model) {
model.addAttribute("creators",
creatorService.findAllCreators().stream()
.map(CreatorMangaDto::new)
.toList());
return "creator";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editCreator(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("CreatorMangaDto", new CreatorMangaDto());
} else {
model.addAttribute("creatorId", id);
model.addAttribute("CreatorMangaDto", new CreatorMangaDto(creatorService.findCreator(id)));
}
return "creator-edit";
}
@PostMapping(value = {"", "/{id}"})
public String saveCreator(@PathVariable(required = false) Long id,
@ModelAttribute @Valid CreatorMangaDto creatorMangaDto,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "creator-edit";
}
if (id == null || id <= 0) {
creatorService.addCreator(creatorMangaDto.getCreatorName(), creatorMangaDto.getHashedPassword());
} else {
creatorService.updateCreator(id, creatorMangaDto.getCreatorName(), creatorMangaDto.getHashedPassword());
}
return "redirect:/creator";
}
@PostMapping("/delete/{id}")
public String deleteCreator(@PathVariable Long id) {
creatorService.deleteCreator(id);
return "redirect:/creator";
}
}

View File

@ -26,6 +26,13 @@ public class Creator {
public Creator(String creatorName, String hashedPassword) {
this.user = user;
this.id = user.getId();
this.mangas = new ArrayList<>();
}
public Creator(User user) {
this.user = user;
this.id = user.getId();
this.mangas = new ArrayList<>();
}
@ -60,8 +67,6 @@ public class Creator {
public String toString() {
return "Creator{" +
"id=" + id +
", creatorName='" + creatorName + '\'' +
", hashedPassword='" + hashedPassword + '\'' +
'}';
}
}

View File

@ -16,8 +16,8 @@ public class CreatorMangaDto {
public CreatorMangaDto(Creator creator) {
this.id = creator.getId();
this.creatorName = creator.getCreatorName();
this.hashedPassword = creator.getHashedPassword();
this.creatorName = creator.getUser().getLogin();
this.hashedPassword = creator.getUser().getPassword();
this.mangas = creator.getMangas().stream()
.map(x -> new MangaDto(x))
.toList();

View File

@ -5,6 +5,8 @@ import com.LabWork.app.MangaStore.model.Default.Manga;
import com.LabWork.app.MangaStore.model.Default.Reader;
import com.LabWork.app.MangaStore.service.Repository.CreatorRepository;
import com.LabWork.app.MangaStore.service.Exception.CreatorNotFoundException;
import com.LabWork.app.MangaStore.user.model.User;
import com.LabWork.app.MangaStore.user.repository.UserRepository;
import com.LabWork.app.MangaStore.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -14,13 +16,17 @@ import java.util.Optional;
@Service
public class CreatorService {
private final CreatorRepository creatorRepository;
private final UserRepository userRepository;
private final MangaService mangaService;
private final ValidatorUtil validatorUtil;
public CreatorService(CreatorRepository creatorRepository,
MangaService mangaService,
ValidatorUtil validatorUtil) {
ValidatorUtil validatorUtil,
UserRepository userRepository) {
this.creatorRepository = creatorRepository;
this.userRepository = userRepository;
this.mangaService = mangaService;
this.validatorUtil = validatorUtil;
}
@ -30,6 +36,14 @@ public class CreatorService {
final Optional<Creator> creator = creatorRepository.findById(id);
return creator.orElseThrow(() -> new CreatorNotFoundException(id));
}
public User findByLogin(String login) {
return userRepository.findOneByLoginIgnoreCase(login);
}
@Transactional(readOnly = true)
public User findUser(Long id) {
final Optional<User> user = userRepository.findById(id);
return user.orElseThrow(() -> new CreatorNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Creator> findAllCreators() { return creatorRepository.findAll(); }
@ -43,11 +57,11 @@ public class CreatorService {
@Transactional
public Creator updateCreator(Long id, String creatorName, String password) {
final Creator currentCreator = findCreator(id);
currentCreator.setCreatorName(creatorName);
currentCreator.setHashedPassword(password);
validatorUtil.validate(currentCreator);
return currentCreator;
final User currentUser = findUser(id);
currentUser.setLogin(creatorName);
currentUser.setPassword(password);
validatorUtil.validate(currentUser);
return findCreator(id);
}
@Transactional

View File

@ -1,6 +1,7 @@
package com.LabWork.app.MangaStore.service.Repository;
import com.LabWork.app.MangaStore.model.Default.Creator;
import com.LabWork.app.MangaStore.user.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CreatorRepository extends JpaRepository<Creator, Long> {

View File

@ -1,5 +1,7 @@
package com.LabWork.app.MangaStore.user.service;
import com.LabWork.app.MangaStore.model.Default.Creator;
import com.LabWork.app.MangaStore.service.Repository.CreatorRepository;
import com.LabWork.app.MangaStore.user.model.UserRole;
import com.LabWork.app.MangaStore.util.validation.ValidationException;
import com.LabWork.app.MangaStore.util.validation.ValidatorUtil;
@ -19,13 +21,16 @@ import java.util.Objects;
@Service
public class UserService implements UserDetailsService {
private final UserRepository userRepository;
private final CreatorRepository creatorRepository;
private final PasswordEncoder passwordEncoder;
private final ValidatorUtil validatorUtil;
public UserService(UserRepository userRepository,
CreatorRepository creatorRepository,
PasswordEncoder passwordEncoder,
ValidatorUtil validatorUtil) {
this.userRepository = userRepository;
this.creatorRepository = creatorRepository;
this.passwordEncoder = passwordEncoder;
this.validatorUtil = validatorUtil;
}
@ -51,7 +56,10 @@ public class UserService implements UserDetailsService {
if (!Objects.equals(password, passwordConfirm)) {
throw new ValidationException("Passwords not equals");
}
return userRepository.save(user);
userRepository.save(user);
final Creator creator = new Creator(user);
creatorRepository.save(creator);
return user;
}
@Override

View File

@ -1,31 +0,0 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/creator/{id}(id=${id})}" th:object="${CreatorMangaDto}" method="post">
<div class="mb-3">
<label for="creatorName" class="form-label">creatorName</label>
<input id="creatorName" type='text' class="form-control" th:field="${CreatorMangaDto.creatorName}" required="true"/>
</div>
<div class="mb-3">
<label for="hashedPassword" class="form-label">hashedPassword</label>
<input id="hashedPassword" type='text' class="form-control" th:field="${CreatorMangaDto.hashedPassword}" required="true"/>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span th:if="${id == null}">Добавить</span>
<span th:if="${id != null}">Обновить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/creator}">
Назад
</a>
</div>
</form>
</div>
</body>
</html>

View File

@ -1,60 +0,0 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div class="container" id="root-div" layout:fragment="content">
<div class="content">
<h1>Creator</h1>
<a class="btn btn-success"
th:href="@{/creator/edit}">
<i class="fa-solid fa-plus"></i> Добавить
</a>
<div class="row table-responsive text-white">
<table class="table mt-3 text-white">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">CreatorName</th>
<th scope="col">Password</th>
<th scope="col">Mangas</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="creator: ${creators}">
<td th:text="${creator.id}"/>
<td th:text="${creator.creatorName}"/>
<td th:text="${creator.hashedPassword}"/>
<td>
<select className="form-select" aria-label="Default select example">
<option th:each="manga, iterator: ${creator.mangas}" th:text="${manga.mangaName}"/>
</select>
</td>
<td style="width: 10%">
<div class="btn-group" role="group" aria-label="Basic example">
<a class="btn btn-warning button-fixed button-sm"
th:href="@{/creator/edit/{id}(id=${creator.id})}">
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
</a>
<button type="button" class="btn btn-danger button-fixed button-sm"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${creator.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/creator/delete/{id}(id=${creator.id})}" method="post">
<button th:id="'remove-' + ${creator.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

View File

@ -28,13 +28,9 @@
<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 class="nav-link" href="/creator"
th:classappend="${#strings.equals(activeLink, '/creator')} ? 'active' : ''">Creator</a>
<a class="nav-link" href="/reader"
th:classappend="${#strings.equals(activeLink, '/reader')} ? 'active' : ''">Reader</a>
<a class="nav-link" href="/creatorAction"
<a class="nav-link" th:href="@{/creatorAction/{login}(login=${#authentication.name})}"
th:classappend="${#strings.equals(activeLink, '/creatorAction')} ? 'active' : ''">CreatorAction</a>
<a class="nav-link" href="/readerAction"
<a class="nav-link" href="/readerAction/"
th:classappend="${#strings.equals(activeLink, '/readerAction')} ? 'active' : ''">ReaderAction</a>
<a class="nav-link" href="/manga"
th:classappend="${#strings.equals(activeLink, '/manga')} ? 'active' : ''">Catalog</a>