Skip to content

Commit

Permalink
refactor: Added transactional annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-merlin committed Apr 8, 2024
1 parent b505931 commit 51c3e51
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.sitblueprint.admin.model.users.AuthUser;
import com.sitblueprint.admin.model.users.User;
import com.sitblueprint.admin.repository.users.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Service
Expand All @@ -29,7 +31,9 @@ public List<User> getAllUsers() {

@Override
public User getUserById(Long userId) {
return userRepository.findById(userId).orElse(null);
return userRepository.findById(userId).orElseThrow(
() -> new NoSuchElementException("User with id " + userId + " was not found")
);
}

@Override
Expand All @@ -55,6 +59,7 @@ public User updateUser(User user) {
return userRepository.saveAndFlush(user);
}

@Transactional
@Override
public void deleteUserById(Long userId) {
Optional<User> optionalUserToDelete = userRepository.findById(userId);
Expand Down

0 comments on commit 51c3e51

Please sign in to comment.