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

59 lines
1.6 KiB
Java
Raw Normal View History

2024-06-24 12:56:01 +00:00
package cc.fascinated.bat.service;
2024-06-25 10:55:26 +00:00
import cc.fascinated.bat.model.BatUser;
2024-06-24 12:56:01 +00:00
import cc.fascinated.bat.repository.UserRepository;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
2024-06-26 01:14:12 +00:00
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
2024-06-24 12:56:01 +00:00
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
*/
2024-06-26 11:19:11 +00:00
// @Cacheable(cacheNames = {"users"}, key = "#id")
2024-06-24 12:56:01 +00:00
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
*/
2024-06-26 11:19:11 +00:00
// @CachePut(cacheNames = {"users"}, key = "#id")
2024-06-24 12:56:01 +00:00
public void saveUser(@NonNull BatUser user) {
userRepository.save(user);
}
}