|
5 | 5 | import lombok.extern.apachecommons.CommonsLog;
|
6 | 6 | import org.hibernate.validator.constraints.NotEmpty;
|
7 | 7 | import org.springframework.beans.factory.annotation.Autowired;
|
8 |
| -import org.springframework.context.ApplicationEventPublisher; |
| 8 | +import org.springframework.context.MessageSource; |
9 | 9 | import org.springframework.http.HttpStatus;
|
10 | 10 | import org.springframework.http.MediaType;
|
11 | 11 | import org.springframework.http.ResponseEntity;
|
12 | 12 | import org.springframework.security.access.annotation.Secured;
|
13 | 13 | import org.springframework.security.authentication.BadCredentialsException;
|
14 | 14 | import org.springframework.stereotype.Controller;
|
15 | 15 | import org.springframework.validation.BindingResult;
|
| 16 | +import org.springframework.validation.FieldError; |
16 | 17 | import org.springframework.web.bind.annotation.*;
|
17 | 18 | import org.springframework.web.multipart.MultipartFile;
|
18 | 19 | import org.springframework.web.servlet.ModelAndView;
|
@@ -57,6 +58,9 @@ public class UserController {
|
57 | 58 | @Autowired
|
58 | 59 | private ApplicationSettings applicationSettings;
|
59 | 60 |
|
| 61 | + @Autowired |
| 62 | + private MessageSource messageSource; |
| 63 | + |
60 | 64 | @GetMapping(value = { "/user/home" })
|
61 | 65 | public ModelAndView userHome() {
|
62 | 66 | ModelAndView modelAndView = new ModelAndView( "user/home" );
|
@@ -243,16 +247,54 @@ public String verifyContactEmail( @RequestParam String token, RedirectAttributes
|
243 | 247 | @Data
|
244 | 248 | static class ProfileWithOrganUberonIds {
|
245 | 249 | @Valid
|
246 |
| - private Profile profile; |
247 |
| - private Set<String> organUberonIds; |
| 250 | + private final Profile profile; |
| 251 | + private final Set<String> organUberonIds; |
| 252 | + } |
| 253 | + |
| 254 | + @Data |
| 255 | + static class ProfileSavedModel { |
| 256 | + private final String message; |
| 257 | + private final boolean contactEmailVerified; |
| 258 | + } |
| 259 | + |
| 260 | + @Data |
| 261 | + static class FieldErrorModel { |
| 262 | + private final String field; |
| 263 | + private final String message; |
| 264 | + private final Object rejectedValue; |
| 265 | + |
| 266 | + public static FieldErrorModel fromFieldError( FieldError fieldError ) { |
| 267 | + return new FieldErrorModel( fieldError.getField(), fieldError.getDefaultMessage(), fieldError.getRejectedValue() ); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + @Data |
| 272 | + static class BindingResultModel { |
| 273 | + private final List<FieldErrorModel> fieldErrors; |
| 274 | + |
| 275 | + public static BindingResultModel fromBindingResult( BindingResult bindingResult ) { |
| 276 | + return new BindingResultModel( bindingResult.getFieldErrors().stream().map( FieldErrorModel::fromFieldError ).collect( Collectors.toList() ) ); |
| 277 | + } |
248 | 278 | }
|
249 | 279 |
|
250 | 280 | @ResponseBody
|
251 |
| - @PostMapping(value = "/user/profile", produces = MediaType.TEXT_PLAIN_VALUE) |
252 |
| - public String saveProfile( @RequestBody ProfileWithOrganUberonIds profileWithOrganUberonIds, Locale locale ) { |
| 281 | + @PostMapping(value = "/user/profile", produces = MediaType.APPLICATION_JSON_VALUE) |
| 282 | + public ResponseEntity<?> saveProfile( @Valid @RequestBody ProfileWithOrganUberonIds profileWithOrganUberonIds, BindingResult bindingResult, Locale locale ) { |
253 | 283 | User user = userService.findCurrentUser();
|
254 |
| - userService.updateUserProfileAndPublicationsAndOrgans( user, profileWithOrganUberonIds.profile, profileWithOrganUberonIds.profile.getPublications(), profileWithOrganUberonIds.organUberonIds, locale ); |
255 |
| - return "Saved."; |
| 284 | + if ( bindingResult.hasErrors() ) { |
| 285 | + return ResponseEntity.badRequest() |
| 286 | + .body( BindingResultModel.fromBindingResult( bindingResult ) ); |
| 287 | + } else { |
| 288 | + String previousContactEmail = user.getProfile().getContactEmail(); |
| 289 | + user = userService.updateUserProfileAndPublicationsAndOrgans( user, profileWithOrganUberonIds.profile, profileWithOrganUberonIds.profile.getPublications(), profileWithOrganUberonIds.organUberonIds, locale ); |
| 290 | + String message = messageSource.getMessage( "UserController.profileSaved", new Object[]{ user.getProfile().getContactEmail() }, locale ); |
| 291 | + if ( user.getProfile().getContactEmail() != null && |
| 292 | + !user.getProfile().getContactEmail().equals( previousContactEmail ) && |
| 293 | + !user.getProfile().isContactEmailVerified() ) { |
| 294 | + message = messageSource.getMessage( "UserController.profileSavedAndContactEmailUpdated", new String[]{ user.getProfile().getContactEmail() }, locale ); |
| 295 | + } |
| 296 | + return ResponseEntity.ok( new ProfileSavedModel( message, user.getProfile().isContactEmailVerified() ) ); |
| 297 | + } |
256 | 298 | }
|
257 | 299 |
|
258 | 300 | @ResponseBody
|
|
0 commit comments