Created
March 28, 2021 10:58
-
-
Save pochka15/19b1c7fe36d4392ce13067245b7e4522 to your computer and use it in GitHub Desktop.
save by adding to the list
This file contains hidden or 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
| // It doesn't make inserts | |
| @Transactional | |
| public boolean saveByAddingToTheList(SavePostForm form, String authorName) { | |
| final Optional<User> found = userRepository.findByName(authorName); | |
| if (found.isPresent()) { | |
| User user = found.get(); | |
| Post post = postFormToPostConverter.convert(form); | |
| post.setAuthor(User.builder().id(found.get().getId()).build()); | |
| user.getPosts().add(post); | |
| return true; | |
| } | |
| return false; | |
| } | |
| // It inserts into the table | |
| @Transactional | |
| public boolean saveUsingRepository(SavePostForm form, String authorName) { | |
| final Optional<UserDto> found = userManagementService.findByName(authorName); | |
| if (found.isPresent()) { | |
| Post post = postFormToPostConverter.convert(form); | |
| post.setAuthor(User.builder().id(found.get().getId()).build()); | |
| postsRepository.save(post); | |
| return true; | |
| } | |
| return false; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment