Skip to content

Commit

Permalink
#790 create backend methods to create users
Browse files Browse the repository at this point in the history
  • Loading branch information
janikEndtner committed Feb 2, 2024
1 parent 4e03df3 commit 6927b65
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
12 changes: 12 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.puzzle.okr.controller;

import ch.puzzle.okr.dto.NewUserDto;
import ch.puzzle.okr.dto.UserDto;
import ch.puzzle.okr.mapper.UserMapper;
import ch.puzzle.okr.service.authorization.AuthorizationService;
Expand Down Expand Up @@ -68,4 +69,15 @@ public UserDto setOkrChampion(
return userMapper.toDto(user);
}

@Operation(summary = "Create users", description = "Creates a user entity for every user in the method body")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Returned users", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = UserDto.class)) }), })
@PostMapping(path = "/createall")
public List<UserDto> createUsers(
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "The Team as json to create a new Team.", required = true) @RequestBody List<NewUserDto> newUserDtoList
) {
var createdUsers = this.userAuthorizationService.createUsers(userMapper.toUserList(newUserDtoList));
return userMapper.toDtos(createdUsers);
}

}
18 changes: 18 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ch.puzzle.okr.mapper;

import ch.puzzle.okr.dto.NewUserDto;
import ch.puzzle.okr.dto.UserDto;
import ch.puzzle.okr.dto.UserTeamDto;
import ch.puzzle.okr.models.User;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
Expand All @@ -16,6 +18,10 @@ public UserMapper(TeamMapper teamMapper) {
this.teamMapper = teamMapper;
}

public List<UserDto> toDtos(List<User> userList) {
return userList.stream().map(this::toDto).toList();
}

public UserDto toDto(User user) {
var userTeams = user.getUserTeamList().stream().map(
ut -> new UserTeamDto(ut.getId(), user.getVersion(), teamMapper.toDto(ut.getTeam()), ut.isTeamAdmin()))
Expand All @@ -24,4 +30,16 @@ public UserDto toDto(User user) {
return new UserDto(user.getId(), user.getVersion(), user.getFirstname(), user.getLastname(), user.getEmail(),
userTeams, user.isOkrChampion());
}

public List<User> toUserList(List<NewUserDto> newUserList) {
return newUserList.stream().map(this::toUser).toList();
}

public User toUser(NewUserDto newUserDto) {
var user = new User();
user.setFirstname(newUserDto.firstname());
user.setLastname(newUserDto.lastname());
user.setEmail(newUserDto.email());
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UserAuthorizationService {
private final TeamAuthorizationService teamAuthorizationService;

public UserAuthorizationService(UserBusinessService userBusinessService, AuthorizationService authorizationService,
TeamAuthorizationService teamAuthorizationService) {
TeamAuthorizationService teamAuthorizationService) {
this.userBusinessService = userBusinessService;
this.authorizationService = authorizationService;
this.teamAuthorizationService = teamAuthorizationService;
Expand Down Expand Up @@ -52,4 +52,12 @@ public User setIsOkrChampion(long id, boolean isOkrChampion) {
OkrResponseStatusException.of(ErrorKey.NOT_AUTHORIZED_TO_WRITE, USER));
return userBusinessService.setIsOkrChampion(user, isOkrChampion);
}

public List<User> createUsers(List<User> userList) {
AuthorizationService.checkRoleWriteAndReadAll(
authorizationService.updateOrAddAuthorizationUser(),
OkrResponseStatusException.of(ErrorKey.NOT_AUTHORIZED_TO_WRITE, USER)
);
return userBusinessService.createUsers(userList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import ch.puzzle.okr.service.CacheService;
import ch.puzzle.okr.service.persistence.UserPersistenceService;
import ch.puzzle.okr.service.validation.UserValidationService;
import jakarta.transaction.Transactional;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.StreamSupport;

@Service
public class UserBusinessService {
Expand Down Expand Up @@ -61,4 +63,10 @@ private void checkAtLeastOneOkrChampionExists(User user) {
public User saveUser(User user) {
return userPersistenceService.save(user);
}

@Transactional
public List<User> createUsers(List<User> userList) {
var userIter = userPersistenceService.saveAll(userList);
return StreamSupport.stream(userIter.spliterator(), false).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public User save(User user) {
public List<User> findAllOkrChampions() {
return getRepository().findByIsOkrChampion(true);
}
public Iterable<User> saveAll(List<User> userList) {
return getRepository().saveAll(userList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,29 @@ void setOkrChampion_shouldThrowErrorIfLoggedInUserIsNotOkrChampion() {
assertThrows(OkrResponseStatusException.class,
() -> userAuthorizationService.setIsOkrChampion(user.getId(), true));
}

@Test
void createUsers_shouldCallBusinessService() {
var loggedInUser = defaultUser(1L);
loggedInUser.setOkrChampion(true);

List<User> users = List.of(user, user2);
when(userBusinessService.createUsers(users)).thenReturn(users);
when(authorizationService.updateOrAddAuthorizationUser()).thenReturn(new AuthorizationUser(loggedInUser));

userAuthorizationService.createUsers(users);

verify(userBusinessService, times(1)).createUsers(users);
}

@Test
void createUsers_shouldThrowErrorIfLoggedInUserIsNotOkrChampion() {
var loggedInUser = defaultUser(1L);
loggedInUser.setOkrChampion(false);

when(authorizationService.updateOrAddAuthorizationUser()).thenReturn(new AuthorizationUser(loggedInUser));

assertThrows(OkrResponseStatusException.class,
() -> userAuthorizationService.createUsers(List.of(user, user2)));
}
}

0 comments on commit 6927b65

Please sign in to comment.