backend/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java
2020-09-25 16:43:24 +02:00

45 lines
1.2 KiB
Java
Executable File

package com.plannaplan.services;
import java.util.UUID;
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 {
@Autowired
private UserRepository repo;
public UserService() {
super();
}
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);
}
public User getUserByEmail(String email) throws UserNotFoundException {
return this.repo.getByAuthority(email.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority"));
}
public User getByToken(String token) {
return this.repo.getByToken(token);
}
}