package com.plannaplan.services; import java.util.UUID; import com.plannaplan.abstracts.EventWatcher; import com.plannaplan.entities.User; import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.repositories.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService extends EventWatcher { @Autowired private UserRepository repo; public UserService() { super(); } // this code is more idiomatic to java using java 8 stream API public String login(String authority) throws UserNotFoundException { User user = this.repo.getByAuthority(authority.replace("\n", "").trim()) .orElseThrow(() -> new UserNotFoundException("Can not find user with given authority")); String token = UUID.randomUUID().toString(); user.setToken(token); this.repo.save(user); return token; } public void save(User user) { this.repo.save(user); } // this code is more idiomatic to java public User getUserByEmail(String email) throws UserNotFoundException { return this.repo.getByAuthority(email.replace("\n", "").trim()) .orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority")); } // why is not throwing exception? public User getByToken(String token) { return this.repo.getByToken(token); } }