-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Oh-my-class/enhanced_validation
Enhanced validation
- Loading branch information
Showing
45 changed files
with
775 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/main/java/com/ohmyclass/api/components/user/dto/in/UserChangeInDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...a/com/ohmyclass/api/components/user/service/processors/UserChangeSubmissionProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ohmyclass.api.components.user.service.processors; | ||
|
||
import com.ohmyclass.api.components.user.dto.in.UserChangeInDTO; | ||
import com.ohmyclass.api.components.user.entity.User; | ||
import com.ohmyclass.api.components.user.repository.UserRepository; | ||
import com.ohmyclass.api.util.SubmissionProcessor; | ||
import com.ohmyclass.api.components.user.service.validation.UserChangeSubmissionValidator; | ||
import com.ohmyclass.api.exceptions.ApiException; | ||
import com.ohmyclass.api.util.validation.ValidationResult; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class UserChangeSubmissionProcessor extends SubmissionProcessor<UserChangeInDTO> { | ||
|
||
private final UserChangeSubmissionValidator userChangeSubmissionValidator; | ||
|
||
private final UserRepository userRepository; | ||
|
||
@Override | ||
protected ValidationResult validate(UserChangeInDTO userIn) { | ||
return userChangeSubmissionValidator.validate(userIn); | ||
} | ||
|
||
@Override | ||
protected void persist(UserChangeInDTO userIn) { | ||
|
||
User user = userRepository.findByUsername(userIn.resolveHeaderById("username")) | ||
.orElseThrow(() -> new ApiException("User to edit not found")); | ||
|
||
user.setEmail(userIn.getNewEmail()); | ||
user.setPassword(userIn.getNewPassword()); | ||
|
||
userRepository.save(user); | ||
} | ||
|
||
@Override | ||
protected void prePersistOperations(UserChangeInDTO userIn) { | ||
if (userRepository.findByEmail(userIn.getNewEmail()).isPresent()) { | ||
throw new ApiException("Email already exists"); | ||
} | ||
} | ||
|
||
@Override | ||
protected void postPersistOperations(UserChangeInDTO userIn) { | ||
if (userRepository.findByEmailAndPassword(userIn.getNewEmail(), userIn.getNewPassword()).isEmpty()) { | ||
throw new ApiException("User not updated correctly"); | ||
} | ||
} | ||
} |
Oops, something went wrong.