长数字
This commit is contained in:
parent
29ee759b39
commit
485c15184b
@ -17,6 +17,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.FindAndModifyOptions;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
@ -84,7 +85,18 @@ public class UserBankBalanceServiceImpl implements UserBankBalanceService {
|
||||
return BankBalanceDTO.of(mongoPrimaryService.find(UserBankBalance.class,
|
||||
Filters.eq("_id", userId),
|
||||
documents -> CollectionUtils.isEmpty(documents) ? 0L
|
||||
: documents.get(0).getLong("balance")));
|
||||
: getLongValue(documents.get(0), "balance")));
|
||||
}
|
||||
|
||||
static Long getLongValue(Document document, String key) {
|
||||
Object value = document.get(key);
|
||||
if (Objects.isNull(value)) {
|
||||
return 0L;
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).longValue();
|
||||
}
|
||||
return Long.valueOf(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package com.red.circle.wallet.infra.database.mongo.service.bank.impl;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UserBankBalanceServiceImplTest {
|
||||
|
||||
@Test
|
||||
void getLongValueAcceptsIntegerBalance() {
|
||||
Document document = new Document("balance", 0);
|
||||
|
||||
assertEquals(0L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLongValueAcceptsLongBalance() {
|
||||
Document document = new Document("balance", 123L);
|
||||
|
||||
assertEquals(123L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLongValueDefaultsMissingBalanceToZero() {
|
||||
Document document = new Document();
|
||||
|
||||
assertEquals(0L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user