Bat/src/main/java/cc/fascinated/bat/service/UserService.java

57 lines
1.5 KiB
Java

package cc.fascinated.bat.service;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.repository.UserRepository;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* @author Fascinated (fascinated7)
*/
@Service @Log4j2
@DependsOn("discordService")
public class UserService {
/**
* The user repository to use
*/
private final UserRepository userRepository;
@Autowired
public UserService(@NonNull UserRepository userRepository) {
this.userRepository = userRepository;
}
/**
* Gets a user by their ID
*
* @param id The ID of the user
* @return The user
*/
// @Cacheable(cacheNames = {"users"}, key = "#id")
public BatUser getUser(@NonNull String id) {
long start = System.currentTimeMillis();
Optional<BatUser> optionalUser = userRepository.findById(id);
if (optionalUser.isPresent()) {
return optionalUser.get();
}
BatUser user = userRepository.save(new BatUser(id));
log.info("Created user \"{}\" in {}ms", id, System.currentTimeMillis() - start);
return user;
}
/**
* Saves a user
*
* @param user The user to save
*/
// @CachePut(cacheNames = {"users"}, key = "#id")
public void saveUser(@NonNull BatUser user) {
userRepository.save(user);
}
}