2020-07-25 10:38:19 +02:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
@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-09-14 12:39:25 +02:00
|
|
|
public String login(String authority) throws UserNotFoundException {
|
2020-09-21 17:45:52 +02:00
|
|
|
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
|
|
|
|
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
|
|
|
|
|
2020-09-14 12:39:25 +02:00
|
|
|
String token = UUID.randomUUID().toString();
|
|
|
|
user.setToken(token);
|
2020-09-14 12:55:47 +02:00
|
|
|
this.repo.save(user);
|
2020-09-14 12:39:25 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save(User user) {
|
|
|
|
this.repo.save(user);
|
|
|
|
}
|
|
|
|
|
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-14 14:02:05 +02:00
|
|
|
public User getByToken(String token) {
|
|
|
|
return this.repo.getByToken(token);
|
|
|
|
}
|
|
|
|
|
2020-07-25 10:38:19 +02:00
|
|
|
}
|