Skip to content

Instantly share code, notes, and snippets.

@pochka15
Created March 28, 2021 10:58
Show Gist options
  • Select an option

  • Save pochka15/19b1c7fe36d4392ce13067245b7e4522 to your computer and use it in GitHub Desktop.

Select an option

Save pochka15/19b1c7fe36d4392ce13067245b7e4522 to your computer and use it in GitHub Desktop.
save by adding to the list
// 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