2020-07-25 10:38:19 +02:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
2020-10-19 12:13:02 +02:00
|
|
|
import java.util.List;
|
2020-09-25 17:01:38 +02:00
|
|
|
import java.util.Optional;
|
2020-09-14 12:39:25 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import com.plannaplan.entities.User;
|
|
|
|
import com.plannaplan.exceptions.UserNotFoundException;
|
2020-07-25 10:38:19 +02:00
|
|
|
import com.plannaplan.repositories.UserRepository;
|
2020-10-19 12:13:02 +02:00
|
|
|
import com.plannaplan.types.UserRoles;
|
2020-07-25 10:38:19 +02:00
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2020-11-20 13:20:44 +01:00
|
|
|
/**
|
2020-12-07 21:46:47 +01:00
|
|
|
* Service of UserService which can get(By Email), login, save user.
|
2020-11-20 13:20:44 +01:00
|
|
|
*/
|
2020-07-25 10:38:19 +02:00
|
|
|
@Service
|
2020-09-23 18:39:43 +02:00
|
|
|
public class UserService {
|
2020-07-25 10:38:19 +02:00
|
|
|
@Autowired
|
2020-07-25 10:56:11 +02:00
|
|
|
private UserRepository repo;
|
2020-07-25 10:38:19 +02:00
|
|
|
|
2020-07-27 16:44:04 +02:00
|
|
|
public UserService() {
|
|
|
|
super();
|
2020-07-25 10:38:19 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 16:23:39 +01:00
|
|
|
public User checkForUser(String email, String usosId) {
|
|
|
|
if (usosId == null) {
|
2020-12-07 21:46:47 +01:00
|
|
|
Optional<User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
|
|
|
|
if (user.isPresent()) {
|
2020-12-03 16:23:39 +01:00
|
|
|
return user.get();
|
2020-12-07 21:46:47 +01:00
|
|
|
} else {
|
|
|
|
final User newUser = new User(null, null, email.replace("\n", "").trim(), UserRoles.STUDENT);
|
2020-12-03 16:57:53 +01:00
|
|
|
return this.repo.save(newUser);
|
2020-12-03 16:23:39 +01:00
|
|
|
}
|
2020-12-07 21:46:47 +01:00
|
|
|
} else {
|
|
|
|
Optional<User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
|
|
|
|
if (user.isPresent()) {
|
2020-12-03 16:23:39 +01:00
|
|
|
return user.get();
|
2020-12-07 21:46:47 +01:00
|
|
|
} else {
|
|
|
|
final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, UserRoles.STUDENT);
|
2020-12-03 16:57:53 +01:00
|
|
|
return this.repo.save(newUser);
|
2020-12-03 16:23:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:46:47 +01:00
|
|
|
public User login(User authority) throws UserNotFoundException {
|
2020-10-19 12:13:02 +02:00
|
|
|
final String token = UUID.randomUUID().toString();
|
2020-12-07 21:46:47 +01:00
|
|
|
try {
|
2020-12-03 16:57:53 +01:00
|
|
|
authority.setToken(token);
|
|
|
|
this.repo.save(authority);
|
2020-12-07 21:46:47 +01:00
|
|
|
} catch (Exception e) {
|
2020-12-03 16:57:53 +01:00
|
|
|
throw new UserNotFoundException(e.getMessage());
|
|
|
|
}
|
2020-12-07 21:46:47 +01:00
|
|
|
return authority;
|
2020-09-14 12:39:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 19:36:56 +01:00
|
|
|
public User save(User user) {
|
|
|
|
return this.repo.save(user);
|
2020-09-14 12:39:25 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:45:52 +02:00
|
|
|
public User getUserByEmail(String email) throws UserNotFoundException {
|
|
|
|
return this.repo.getByAuthority(email.replace("\n", "").trim())
|
|
|
|
.orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority"));
|
|
|
|
|
2020-09-14 12:39:25 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 17:01:38 +02:00
|
|
|
public Optional<User> getByToken(String token) {
|
2020-09-14 14:02:05 +02:00
|
|
|
return this.repo.getByToken(token);
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:13:02 +02:00
|
|
|
public List<User> searchForStudents(String query) {
|
|
|
|
return this.repo.searchForUsers(query, UserRoles.STUDENT);
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:25:55 +01:00
|
|
|
public Optional<User> getById(Long userId) {
|
|
|
|
return this.repo.findById(userId);
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:24:01 +01:00
|
|
|
public List<User> getAllStudents() {
|
|
|
|
return this.repo.getAllByRole(UserRoles.STUDENT);
|
|
|
|
}
|
|
|
|
|
2020-12-23 11:51:17 +01:00
|
|
|
public Optional<User> getUserByRefreshToken(String refreshToken) {
|
|
|
|
return this.repo.getByRefreshToken(refreshToken);
|
|
|
|
}
|
|
|
|
|
2020-12-23 13:48:26 +01:00
|
|
|
public boolean adminExists(){
|
|
|
|
return this.repo.getAllByRole(UserRoles.ADMIN).size() > 0;
|
|
|
|
}
|
|
|
|
|
2020-07-25 10:38:19 +02:00
|
|
|
}
|