package com.plannaplan.services; import java.util.List; import java.util.Optional; import java.util.UUID; import com.plannaplan.entities.User; import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.models.UserApiResponse; import com.plannaplan.repositories.UserRepository; import com.plannaplan.types.UserRoles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Service of UserService which can get(By Email), login, save user. */ @Service public class UserService { @Autowired private UserRepository repo; @Autowired private UsosApiService service; public UserService() { super(); } /** * checks if user exist and return him or creates new one with student role * otherwise * * @param email user email in usos * @param usosId user id in usos * @return user entity instace containing changes saved in database */ public User checkForUser(String email, String usosId) { return this.checkForUser(email, usosId, UserRoles.STUDENT); } /** * checks if user exist and creates new one if doesn't * * @param email user email in usos * @param usosId user id in usos * @param roleIfNotExist role to be set in case user is not in database yet * @return user entity instace containing changes saved in database */ public User checkForUser(String email, String usosId, UserRoles roleIfNotExist) { if (usosId == null) { Optional user = this.repo.getByEmail(email.replace("\n", "").trim()); if (user.isPresent()) { return user.get(); } else { final User newUser = new User(null, null, email.replace("\n", "").trim(), roleIfNotExist); return this.repo.save(newUser); } } else { Optional user = this.repo.getByUsosId(usosId.replace("\n", "").trim()); if (user.isPresent()) { return user.get(); } else { final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, roleIfNotExist); return this.repo.save(newUser); } } } /** * generates token for user and if user don't have name in database it will * attemp to obtain these from usos api and saves changes in database * * @param authority user we want to login * @return user with changed values after save in db * @throws UserNotFoundException throwed if user doesn't exist */ public User login(User authority) throws UserNotFoundException { final String token = UUID.randomUUID().toString(); if ((authority.getName() == null || authority.getSurname() == null) && authority.getUsosId() != null) { final UserApiResponse resp = this.service.getUserData(authority.getUsosId()); authority.updateWithUsosData(resp); } try { authority.setToken(token); this.repo.save(authority); } catch (Exception e) { throw new UserNotFoundException(e.getMessage()); } return authority; } /** * sacves user to databse and return instatnce with id * * @param user to be saved * @return instatnce with bd id */ public User save(User user) { return this.repo.save(user); } /** * * @param email of user to be find * @return user with given mail * @throws UserNotFoundException throwed if user doesn't exist */ public User getUserByEmail(String email) throws UserNotFoundException { return this.repo.getByEmail(email.replace("\n", "").trim()) .orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority")); } /** * return user by given authority * * @param authority user usosId or email * @return optional with user if found */ public Optional getByAuthority(String authority) { return this.repo.getByAuthority(authority); } public Optional getByToken(String token) { return this.repo.getByToken(token); } /** * search for user with given query * * @param query string that will be matched to users name and surname * @return list opf results */ public List searchForStudents(String query) { return this.repo.searchForUsers(query, UserRoles.STUDENT); } public Optional getById(Long userId) { return this.repo.findById(userId); } public List getAllStudents() { return this.repo.getAllByRole(UserRoles.STUDENT); } public Optional getUserByRefreshToken(String refreshToken) { return this.repo.getByRefreshToken(refreshToken); } public boolean adminExists() { return this.repo.getAllByRole(UserRoles.ADMIN).size() > 0; } }