mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-28 17:10:48 +08:00
Compare commits
11 Commits
feature/ic
...
codex/anal
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c43e4b85bc | ||
|
|
fb3a2839db | ||
|
|
db8c896b71 | ||
|
|
2a090442cc | ||
|
|
aa86909598 | ||
|
|
5eb1416c6b | ||
|
|
7320df6d20 | ||
|
|
9406bf3392 | ||
|
|
ccaada8f4e | ||
|
|
5534573a19 | ||
|
|
35c6d29b8f |
@@ -22,6 +22,8 @@ import org.springframework.data.redis.serializer.RedisSerializer;
|
|||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis 缓存配置类
|
* Redis 缓存配置类
|
||||||
@@ -80,13 +82,16 @@ public class CachingConfig {
|
|||||||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
||||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
|
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
|
||||||
.disableCachingNullValues(); // 禁止缓存 null 值
|
.disableCachingNullValues(); // 禁止缓存 null 值
|
||||||
// 个别缓存单独设置TTL时间
|
|
||||||
// Map<String, RedisCacheConfiguration> cacheConfigs = new HashMap<>();
|
// 个别缓存单独设置 TTL 时间
|
||||||
// cacheConfigs.put("openisle_tags", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ZERO));
|
Map<String, RedisCacheConfiguration> cacheConfigs = new HashMap<>();
|
||||||
// cacheConfigs.put("openisle_categories", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ZERO));
|
RedisCacheConfiguration oneHourConfig = config.entryTtl(Duration.ofHours(1));
|
||||||
|
cacheConfigs.put(TAG_CACHE_NAME, oneHourConfig);
|
||||||
|
cacheConfigs.put(CATEGORY_CACHE_NAME, oneHourConfig);
|
||||||
|
|
||||||
return RedisCacheManager.builder(connectionFactory)
|
return RedisCacheManager.builder(connectionFactory)
|
||||||
.cacheDefaults(config)
|
.cacheDefaults(config)
|
||||||
|
.withInitialCacheConfigurations(cacheConfigs)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface PointHistoryRepository extends JpaRepository<PointHistory, Long> {
|
public interface PointHistoryRepository extends JpaRepository<PointHistory, Long> {
|
||||||
List<PointHistory> findByUserOrderByIdDesc(User user);
|
List<PointHistory> findByUserOrderByIdDesc(User user);
|
||||||
|
List<PointHistory> findByUserOrderByIdAsc(User user);
|
||||||
long countByUser(User user);
|
long countByUser(User user);
|
||||||
|
|
||||||
List<PointHistory> findByUserAndCreatedAtAfterOrderByCreatedAtDesc(User user, LocalDateTime createdAt);
|
List<PointHistory> findByUserAndCreatedAtAfterOrderByCreatedAtDesc(User user, LocalDateTime createdAt);
|
||||||
|
|||||||
@@ -225,17 +225,20 @@ public class PointService {
|
|||||||
*/
|
*/
|
||||||
public int recalculateUserPoints(User user) {
|
public int recalculateUserPoints(User user) {
|
||||||
// 获取用户所有的积分历史记录(由于@Where注解,已删除的记录会被自动过滤)
|
// 获取用户所有的积分历史记录(由于@Where注解,已删除的记录会被自动过滤)
|
||||||
List<PointHistory> histories = pointHistoryRepository.findByUserOrderByIdDesc(user);
|
List<PointHistory> histories = pointHistoryRepository.findByUserOrderByIdAsc(user);
|
||||||
|
|
||||||
int totalPoints = 0;
|
int totalPoints = 0;
|
||||||
for (PointHistory history : histories) {
|
for (PointHistory history : histories) {
|
||||||
totalPoints += history.getAmount();
|
totalPoints += history.getAmount();
|
||||||
|
// 重新计算每条历史记录的余额
|
||||||
|
history.setBalance(totalPoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新用户积分
|
// 批量更新历史记录及用户积分
|
||||||
|
pointHistoryRepository.saveAll(histories);
|
||||||
user.setPoint(totalPoints);
|
user.setPoint(totalPoints);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
return totalPoints;
|
return totalPoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.openisle.service;
|
package com.openisle.service;
|
||||||
|
|
||||||
import com.openisle.exception.FieldException;
|
import com.openisle.exception.FieldException;
|
||||||
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,6 +18,11 @@ public class UsernameValidator {
|
|||||||
if (username == null || username.isEmpty()) {
|
if (username == null || username.isEmpty()) {
|
||||||
throw new FieldException("username", "Username cannot be empty");
|
throw new FieldException("username", "Username cannot be empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NumberUtils.isDigits(username)) {
|
||||||
|
throw new FieldException("username", "Username cannot be pure number");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ spring.jpa.hibernate.ddl-auto=update
|
|||||||
# for redis
|
# for redis
|
||||||
spring.data.redis.host=${REDIS_HOST:localhost}
|
spring.data.redis.host=${REDIS_HOST:localhost}
|
||||||
spring.data.redis.port=${REDIS_PORT:6379}
|
spring.data.redis.port=${REDIS_PORT:6379}
|
||||||
spring.data.redis.database=0
|
spring.data.redis.database=${REDIS_DATABASE:0}
|
||||||
|
|
||||||
# for jwt
|
# for jwt
|
||||||
app.jwt.secret=${JWT_SECRET:jwt_sec}
|
app.jwt.secret=${JWT_SECRET:jwt_sec}
|
||||||
@@ -56,9 +56,10 @@ app.captcha.comment-enabled=${CAPTCHA_COMMENT_ENABLED:false}
|
|||||||
# ========= Optional =========
|
# ========= Optional =========
|
||||||
# for resend email send service, you can improve your service by yourself
|
# for resend email send service, you can improve your service by yourself
|
||||||
resend.api.key=${RESEND_API_KEY:}
|
resend.api.key=${RESEND_API_KEY:}
|
||||||
resend.from.email=${RESEND.FROM.EMAIL}
|
resend.from.email=${RESEND_FROM_EMAIL:}
|
||||||
# your email services: ...
|
# your email services: ...
|
||||||
|
|
||||||
|
|
||||||
# for tencent cloud image upload service, you can improve your service by yourself
|
# for tencent cloud image upload service, you can improve your service by yourself
|
||||||
cos.base-url=${:https://example.com}
|
cos.base-url=${:https://example.com}
|
||||||
cos.secret-id=${COS_SECRET_ID:}
|
cos.secret-id=${COS_SECRET_ID:}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.openisle.service;
|
||||||
|
|
||||||
|
import com.openisle.model.PointHistory;
|
||||||
|
import com.openisle.model.PointHistoryType;
|
||||||
|
import com.openisle.model.Role;
|
||||||
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.repository.PointHistoryRepository;
|
||||||
|
import com.openisle.repository.UserRepository;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@DataJpaTest
|
||||||
|
@Import(PointService.class)
|
||||||
|
class PointServiceRecalculateUserPointsTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PointService pointService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PointHistoryRepository pointHistoryRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void recalculatesBalanceAfterDeletion() {
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername("u");
|
||||||
|
user.setEmail("u@example.com");
|
||||||
|
user.setPassword("p");
|
||||||
|
user.setRole(Role.USER);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
PointHistory h1 = new PointHistory();
|
||||||
|
h1.setUser(user);
|
||||||
|
h1.setType(PointHistoryType.POST);
|
||||||
|
h1.setAmount(30);
|
||||||
|
h1.setBalance(30);
|
||||||
|
h1.setCreatedAt(LocalDateTime.now().minusMinutes(2));
|
||||||
|
pointHistoryRepository.save(h1);
|
||||||
|
|
||||||
|
PointHistory h2 = new PointHistory();
|
||||||
|
h2.setUser(user);
|
||||||
|
h2.setType(PointHistoryType.COMMENT);
|
||||||
|
h2.setAmount(10);
|
||||||
|
h2.setBalance(40);
|
||||||
|
h2.setCreatedAt(LocalDateTime.now().minusMinutes(1));
|
||||||
|
pointHistoryRepository.save(h2);
|
||||||
|
|
||||||
|
user.setPoint(40);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
pointHistoryRepository.delete(h1);
|
||||||
|
|
||||||
|
int total = pointService.recalculateUserPoints(user);
|
||||||
|
|
||||||
|
assertEquals(10, total);
|
||||||
|
assertEquals(10, userRepository.findById(user.getId()).orElseThrow().getPoint());
|
||||||
|
assertEquals(10, pointHistoryRepository.findById(h2.getId()).orElseThrow().getBalance());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user