Skip to content

Commit

Permalink
Use asDto(...) function instead of the direct AdminUserDTO/UserDTO re…
Browse files Browse the repository at this point in the history
…ferences
  • Loading branch information
gzsombor committed Aug 17, 2023
1 parent 4c812d2 commit 3ae28ce
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import <%=packageName%>.repository.AuthorityRepository;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.AuthoritiesConstants;
import <%=packageName%>.security.SecurityUtils;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import <%=packageName%>.service.dto.<%= ${generator.asDto('User')} %>;
import <%=packageName%>.service.util.RandomUtil;
import <%=packageName%>.web.rest.errors.*;
<%_ if (authenticationType !== 'oauth2') { _%>
Expand Down Expand Up @@ -143,7 +143,7 @@ public class UserService {
});
}
public User registerUser(AdminUserDTO userDTO, String password) {
public User registerUser(<%= ${generator.asDto('AdminUser')} %> userDTO, String password) {
userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).ifPresent(existingUser -> {
boolean removed = removeNonActivatedUser(existingUser);
if (!removed) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public class UserService {
return true;
}
public User createUser(AdminUserDTO userDTO) {
public User createUser(<%= ${generator.asDto('AdminUser')} %> userDTO) {
User user = new User();
user.setLogin(userDTO.getLogin().toLowerCase());
user.setFirstName(userDTO.getFirstName());
Expand Down Expand Up @@ -232,7 +232,7 @@ public class UserService {
* @param userDTO user to update.
* @return updated user.
*/
public Optional<AdminUserDTO> updateUser(AdminUserDTO userDTO) {
public Optional<<%= ${generator.asDto('AdminUser')} %>> updateUser(<%= ${generator.asDto('AdminUser')} %> userDTO) {
return Optional.of(userRepository
.findById(userDTO.getId()))
.filter(Optional::isPresent)
Expand Down Expand Up @@ -262,7 +262,7 @@ public class UserService {
log.debug("Changed Information for User: {}", user);
return user;
})
.map(AdminUserDTO::new);
.map(<%= ${generator.asDto('AdminUser')} %>::new);
}
public void deleteUser(String login) {
Expand Down Expand Up @@ -321,15 +321,15 @@ public class UserService {
}
@ReadOnly
public Page<AdminUserDTO> getAllManagedUsers(Pageable pageable) {
public Page<<%= ${generator.asDto('AdminUser')} %>> getAllManagedUsers(Pageable pageable) {
Page<User> userPage = userRepository.findByLoginNotEqual(Constants.ANONYMOUS_USER, pageable);
return Page.of(userPage.getContent().stream().map(AdminUserDTO::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
return Page.of(userPage.getContent().stream().map(<%= ${generator.asDto('AdminUser')} %>::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
}

@ReadOnly
public Page<UserDTO> getAllPublicUsers(Pageable pageable) {
public Page<<%= ${generator.asDto('User')} %>> getAllPublicUsers(Pageable pageable) {
Page<User> userPage = userRepository.findByLoginNotEqual(Constants.ANONYMOUS_USER, pageable);
return Page.of(userPage.getContent().stream().map(UserDTO::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
return Page.of(userPage.getContent().stream().map(<%= ${generator.asDto('User')} %>::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
}

@ReadOnly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,52 @@ package <%=packageName%>.service.mapper;

import <%=packageName%>.domain.Authority;
import <%=packageName%>.domain.User;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import <%=packageName%>.service.dto.<%= ${generator.asDto('User')} %>;

import jakarta.inject.Singleton;
import java.util.*;
import java.util.stream.Collectors;

/**
* Mapper for the entity {@link User} and its DTO called {@link UserDTO}.
* Mapper for the entity {@link User} and its DTO called {@link <%= ${generator.asDto('User')} %>}.
*
* Normal mappers are generated using MapStruct, this one is hand-coded as MapStruct
* support is still in beta, and requires a manual step with an IDE.
*/
@Singleton
public class UserMapper {

public List<UserDTO> usersToUserDTOs(List<User> users) {
public List<<%= ${generator.asDto('User')} %>> usersToUserDTOs(List<User> users) {
return users.stream()
.filter(Objects::nonNull)
.map(this::userToUserDTO)
.collect(Collectors.toList());
}

public UserDTO userToUserDTO(User user) {
return new UserDTO(user);
public <%= ${generator.asDto('User')} %> userToUserDTO(User user) {
return new <%= ${generator.asDto('User')} %>(user);
}

public List<AdminUserDTO> usersToAdminUserDTOs(List<User> users) {
public List<<%= ${generator.asDto('AdminUser')} %>> usersToAdminUserDTOs(List<User> users) {
return users.stream()
.filter(Objects::nonNull)
.map(this::userToAdminUserDTO)
.collect(Collectors.toList());
}

public AdminUserDTO userToAdminUserDTO(User user) {
return new AdminUserDTO(user);
public <%= ${generator.asDto('AdminUser')} %> userToAdminUserDTO(User user) {
return new <%= ${generator.asDto('AdminUser')} %>(user);
}

public List<User> userDTOsToUsers(List<AdminUserDTO> userDTOs) {
public List<User> userDTOsToUsers(List<<%= ${generator.asDto('AdminUser')} %>> userDTOs) {
return userDTOs.stream()
.filter(Objects::nonNull)
.map(this::userDTOToUser)
.collect(Collectors.toList());
}

public User userDTOToUser(AdminUserDTO userDTO) {
public User userDTOToUser(<%= ${generator.asDto('AdminUser')} %> userDTO) {
if (userDTO == null) {
return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package <%= packageName %>.web.rest;

<%_ if (authenticationType === 'oauth2') { _%>
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import io.micronaut.http.annotation.*;
import io.micronaut.scheduling.TaskExecutors;
Expand Down Expand Up @@ -59,9 +59,9 @@ public class AccountResource {
*/
@Get("/account")
@ExecuteOn(TaskExecutors.IO)
public AdminUserDTO getAccount() {
public <%= ${generator.asDto('AdminUser')} %> getAccount() {
return userService.getUserWithAuthorities()
.map(AdminUserDTO::new)
.map(<%= ${generator.asDto('AdminUser')} %>::new)
.orElseThrow(() -> new AccountResourceException("User could not be found"));
}
}
Expand All @@ -71,7 +71,7 @@ import <%=packageName%>.domain.User;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.service.MailService;
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import <%=packageName%>.service.dto.PasswordChangeDTO;
import <%=packageName%>.web.rest.errors.*;
import <%=packageName%>.web.rest.vm.KeyAndPasswordVM;
Expand Down Expand Up @@ -175,9 +175,9 @@ public class AccountResource {
*/
@Get("/account")
@ExecuteOn(TaskExecutors.IO)
public AdminUserDTO getAccount() {
public <%= ${generator.asDto('AdminUser')} %> getAccount() {
return userService.getUserWithAuthorities()
.map(AdminUserDTO::new)
.map(<%= ${generator.asDto('AdminUser')} %>::new)
.orElseThrow(() -> new AccountResourceException("User could not be found"));
}
Expand All @@ -190,7 +190,7 @@ public class AccountResource {
*/
@Post("/account")
@ExecuteOn(TaskExecutors.IO)
public void saveAccount(@Valid @Body AdminUserDTO userDTO) {
public void saveAccount(@Valid @Body <%= ${generator.asDto('AdminUser')} %> userDTO) {
String userLogin = userService.getCurrentUserLogin().orElseThrow(() -> new AccountResourceException("Current user login not found"));
Optional<User> existingUser = userRepository.findOneByEmailIgnoreCase(userDTO.getEmail());
if (existingUser.isPresent() && (!existingUser.get().getLogin().equalsIgnoreCase(userLogin))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('User')} %>;
import <%=packageName%>.util.PaginationUtil;

import io.micronaut.data.model.Page;
Expand Down Expand Up @@ -42,9 +42,9 @@ public class PublicUserResource {
*/
@Get("/users")
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<List<UserDTO>> getAllUsers(HttpRequest request, Pageable pageable) {
public HttpResponse<List<<%= ${generator.asDto('User')} %>>> getAllUsers(HttpRequest request, Pageable pageable) {
log.debug("REST request to get Users: {}", pageable);
final Page<UserDTO> page = userService.getAllPublicUsers(pageable);
final Page<<%= ${generator.asDto('User')} %>> page = userService.getAllPublicUsers(pageable);
return HttpResponse.ok(page.getContent()).headers(headers ->
PaginationUtil.generatePaginationHttpHeaders(headers, UriBuilder.of(request.getPath()), page)
);
Expand All @@ -60,4 +60,4 @@ public class PublicUserResource {
return userService.getAuthorities();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import <%=packageName%>.security.AuthoritiesConstants;
import <%=packageName%>.service.MailService;
<%_ } _%>
import <%=packageName%>.service.UserService;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import <%=packageName%>.util.HeaderUtil;
import <%=packageName%>.util.PaginationUtil;
<%_ if (authenticationType !== 'oauth2') { _%>
Expand Down Expand Up @@ -103,7 +103,7 @@ public class UserResource {
@Post("/users")
@Secured(AuthoritiesConstants.ADMIN)
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<AdminUserDTO> createUser(@Valid @Body AdminUserDTO userDTO) throws URISyntaxException {
public HttpResponse<<%= ${generator.asDto('AdminUser')} %>> createUser(@Valid @Body <%= ${generator.asDto('AdminUser')} %> userDTO) throws URISyntaxException {
log.debug("REST request to save User : {}", userDTO);
if (userDTO.getId() != null) {
Expand All @@ -122,7 +122,7 @@ public class UserResource {
applicationName,
"userManagement.created",
newUser.getLogin()))
.body(new AdminUserDTO(newUser));
.body(new <%= ${generator.asDto('AdminUser')} %>(newUser));
}
}
Expand All @@ -137,7 +137,7 @@ public class UserResource {
@Put("/users")
@Secured(AuthoritiesConstants.ADMIN)
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<AdminUserDTO> updateUser(@Valid @Body AdminUserDTO userDTO) {
public HttpResponse<<%= ${generator.asDto('AdminUser')} %>> updateUser(@Valid @Body <%= ${generator.asDto('AdminUser')} %> userDTO) {
log.debug("REST request to update User : {}", userDTO);
Optional<User> existingUser = userRepository.findOneByEmailIgnoreCase(userDTO.getEmail());
if (existingUser.isPresent() && (!existingUser.get().getId().equals(userDTO.getId()))) {
Expand All @@ -147,7 +147,7 @@ public class UserResource {
if (existingUser.isPresent() && (!existingUser.get().getId().equals(userDTO.getId()))) {
throw new LoginAlreadyUsedException();
}
Optional<AdminUserDTO> updatedUser = userService.updateUser(userDTO);
Optional<<%= ${generator.asDto('AdminUser')} %>> updatedUser = userService.updateUser(userDTO);
return updatedUser.map(user ->
HttpResponse.ok(user)
Expand All @@ -164,9 +164,9 @@ public class UserResource {
*/
@Get("/users")
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<List<AdminUserDTO>> getAllUsers(HttpRequest request, Pageable pageable) {
public HttpResponse<List<<%= ${generator.asDto('AdminUser')} %>>> getAllUsers(HttpRequest request, Pageable pageable) {
log.debug("REST request to get Users for admin: {}", pageable);
final Page<AdminUserDTO> page = userService.getAllManagedUsers(pageable);
final Page<<%= ${generator.asDto('AdminUser')} %>> page = userService.getAllManagedUsers(pageable);
return HttpResponse.ok(page.getContent()).headers(headers ->
PaginationUtil.generatePaginationHttpHeaders(headers, UriBuilder.of(request.getPath()), page)
);
Expand All @@ -180,10 +180,10 @@ public class UserResource {
*/
@Get("/users/{login:" + Constants.LOGIN_REGEX + "}")
@ExecuteOn(TaskExecutors.IO)
public Optional<AdminUserDTO> getUser(@PathVariable String login) {
public Optional<<%= ${generator.asDto('AdminUser')} %>> getUser(@PathVariable String login) {
log.debug("REST request to get User : {}", login);
return userService.getUserWithAuthoritiesByLogin(login)
.map(AdminUserDTO::new);
.map(<%= ${generator.asDto('AdminUser')} %>::new);
}

<%_ if (authenticationType !== 'oauth2') { _%>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package <%=packageName%>.web.rest.vm;

import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import io.micronaut.core.annotation.Introspected;

import javax.validation.constraints.Size;

/**
* View Model extending the AdminUserDTO, which is meant to be used in the user management UI.
* View Model extending the <%= ${generator.asDto('AdminUser')} %>, which is meant to be used in the user management UI.
*/
@Introspected
public class ManagedUserVM extends AdminUserDTO {
public class ManagedUserVM extends <%= ${generator.asDto('AdminUser')} %> {

public static final int PASSWORD_MIN_LENGTH = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import <%=packageName%>.domain.User;
import <%=packageName%>.repository.AuthorityRepository;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.AuthoritiesConstants;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import <%=packageName%>.service.util.RandomUtil;
<%_ if (authenticationType !== 'oauth2') { _%>
import <%=packageName%>.web.rest.errors.EmailAlreadyUsedException;
Expand Down Expand Up @@ -181,7 +181,7 @@ public class UserServiceIT {
userRepository.saveAndFlush(user);
}
final Pageable pageable = Pageable.from(0, (int) userRepository.count());
final Page<AdminUserDTO> allManagedUsers = userService.getAllManagedUsers(pageable);
final Page<<%= ${generator.asDto('AdminUser')} %>> allManagedUsers = userService.getAllManagedUsers(pageable);
assertThat(allManagedUsers.getContent().stream()
.noneMatch(user -> Constants.ANONYMOUS_USER.equals(user.getLogin())))
.isTrue();
Expand Down Expand Up @@ -304,7 +304,7 @@ public class UserServiceIT {
assertThat(testUser4.get().getEmail()).isEqualTo("[email protected]");
testUser4.get().setActivated(true);
userService.updateUser(new AdminUserDTO(testUser4.get()));
userService.updateUser(new <%= ${generator.asDto('AdminUser')} %>(testUser4.get()));
// Register 4th (already activated) user
Assertions.assertThrows(EmailAlreadyUsedException.class, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import <%= packageName %>.RedisTestContainerExtension;
<%_ } _%>
import <%=packageName%>.<%= mainClass %>;
import <%=packageName%>.domain.User;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.dto.<%= ${generator.asDto('AdminUser')} %>;
import <%=packageName%>.service.dto.<%= ${generator.asDto('User')} %>;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class UserMapperIT {
@Inject UserMapper userMapper;

private User user;
private AdminUserDTO userDto;
private <%= ${generator.asDto('AdminUser')} %> userDto;

@BeforeEach
public void init() {
Expand All @@ -58,7 +58,7 @@ public class UserMapperIT {
user.setImageUrl("image_url");
user.setLangKey("en");

userDto = new AdminUserDTO(user);
userDto = new <%= ${generator.asDto('AdminUser')} %>(user);
}

@Test
Expand All @@ -67,15 +67,15 @@ public class UserMapperIT {
users.add(user);
users.add(null);

List<UserDTO> userDTOS = userMapper.usersToUserDTOs(users);
List<<%= ${generator.asDto('User')} %>> userDTOS = userMapper.usersToUserDTOs(users);

assertThat(userDTOS).isNotEmpty();
assertThat(userDTOS).size().isEqualTo(1);
}

@Test
public void userDTOsToUsersShouldMapOnlyNonNullUsers() {
List<AdminUserDTO> usersDto = new ArrayList<>();
List<<%= ${generator.asDto('AdminUser')} %>> usersDto = new ArrayList<>();
usersDto.add(userDto);
usersDto.add(null);

Expand All @@ -91,7 +91,7 @@ public class UserMapperIT {
authoritiesAsString.add("ADMIN");
userDto.setAuthorities(authoritiesAsString);

List<AdminUserDTO> usersDto = new ArrayList<>();
List<<%= ${generator.asDto('AdminUser')} %>> usersDto = new ArrayList<>();
usersDto.add(userDto);

List<User> users = userMapper.userDTOsToUsers(usersDto);
Expand All @@ -107,7 +107,7 @@ public class UserMapperIT {
public void userDTOsToUsersMapWithNullAuthoritiesStringShouldReturnUserWithEmptyAuthorities() {
userDto.setAuthorities(null);

List<AdminUserDTO> usersDto = new ArrayList<>();
List<<%= ${generator.asDto('AdminUser')} %>> usersDto = new ArrayList<>();
usersDto.add(userDto);

List<User> users = userMapper.userDTOsToUsers(usersDto);
Expand Down
Loading

0 comments on commit 3ae28ce

Please sign in to comment.