backend/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java

46 lines
1.3 KiB
Java
Raw Normal View History

2020-07-25 10:38:19 +02:00
package com.plannaplan.services;
2020-09-25 17:01:38 +02:00
import java.util.Optional;
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
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
}
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);
2020-09-14 12:55:47 +02:00
this.repo.save(user);
return token;
}
public void save(User user) {
this.repo.save(user);
}
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-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-07-25 10:38:19 +02:00
}