@RequestMapping(method = RequestMethod.GET, value = "/") public List<UserGroupEditDTO> userGroupList(final @PathVariable("userId") int userId) { final List<UserGroup> userGroups = userGroupService.loadUserGroupsWhereUserIsOwner(userService.load(userId)); final Function<UserGroup, UserGroupEditDTO> mapper = userGroup -> { final UserGroupEditDTO dto = new UserGroupEditDTO(); dto.setUserGroupId(userGroup.getId()); dto.setUserGroupName(userGroup.getGroupName()); dto.setCupIds( userGroupService .loadCups(userGroup) .stream() .map(AbstractEntity::getId) .collect(Collectors.toList())); return dto; }; return userGroups.stream().map(mapper).collect(Collectors.toList()); }
@ResponseStatus(HttpStatus.OK) @ResponseBody @RequestMapping(method = RequestMethod.PUT, value = "/0", produces = APPLICATION_JSON_VALUE) public UserGroupEditDTO create( @RequestBody final UserGroupEditDTO dto, @PathVariable("userId") final int userId) { // TODO: validate! final UserGroup userGroup = save(userId, dto, new UserGroup()); dto.setUserGroupId(userGroup.getId()); return dto; }
private UserGroup save(final int userId, final UserGroupEditDTO dto, final UserGroup userGroup) { final User groupOwner = userService.load(userId); userGroup.setOwner(groupOwner); userGroup.setGroupName(dto.getUserGroupName()); return userGroupService.save(userGroup, dto.getCupIds()); }