Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
markliu2013 committed Jun 13, 2024
1 parent a6555b4 commit 60b9b9e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cn/biq/mn/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TestController {

@RequestMapping(method = RequestMethod.GET, value = "/version")
public BaseResponse handleVersion() {
return new DataResponse<>("93.64");
return new DataResponse<>("93.65");
}

@GetMapping("/test3")
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/biq/mn/currency/CurrencyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import cn.biq.mn.base.BaseController;
import cn.biq.mn.response.BaseResponse;
import cn.biq.mn.response.DataResponse;
import cn.biq.mn.response.PageResponse;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -25,6 +27,11 @@ public BaseResponse handleAll() {
return new DataResponse<>(currencyService.queryAll());
}

@RequestMapping(method = RequestMethod.GET, value = "")
public BaseResponse handleQuery(CurrencyQueryForm form, Pageable page) {
return new PageResponse<>(currencyService.query(form, page));
}

@RequestMapping(method = RequestMethod.POST, value = "/refresh")
public BaseResponse handleRefresh() {
return new DataResponse<>(currencyService.refreshCurrency());
Expand All @@ -40,4 +47,7 @@ public BaseResponse handleCalc(@RequestParam String from, @RequestParam String t
return new DataResponse<>(currencyService.calc(from, to, amount));
}




}
1 change: 1 addition & 0 deletions src/main/java/cn/biq/mn/currency/CurrencyDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CurrencyDetails {
private String name;
private String description;
private Double rate;
private Double rate2; // for base

public CurrencyDetails() { }

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/cn/biq/mn/currency/CurrencyQueryForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.biq.mn.currency;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CurrencyQueryForm {

private String base;
private String name;

}
51 changes: 51 additions & 0 deletions src/main/java/cn/biq/mn/currency/CurrencyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import cn.biq.mn.utils.WebUtils;
import cn.biq.mn.bean.ApplicationScopeBean;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -23,6 +28,52 @@ public List<CurrencyDetails> queryAll() {
return applicationScopeBean.getCurrencyDetailsList();
}

public Page<CurrencyDetails> query(CurrencyQueryForm query, Pageable page) {
List<CurrencyDetails> currencyList = applicationScopeBean.getCurrencyDetailsList();
List<CurrencyDetails> filteredList = currencyList;
if (StringUtils.hasText(query.getName())) {
filteredList = currencyList.stream()
.filter(person -> person.getName().contains(query.getName()))
.toList();
}
if (StringUtils.hasText(query.getBase())) {
filteredList.forEach(i -> {
i.setRate2(convert(query.getBase(), i.getName()).doubleValue());
});
}
if (!page.getSort().isEmpty() && filteredList.size() > 1) {
for (Sort.Order order : page.getSort()) {
if ("rate2".equals(order.getProperty())) {
if ("ASC".equals(order.getDirection().toString())) {
filteredList.sort(new Comparator<CurrencyDetails>() {
@Override
public int compare(CurrencyDetails c1, CurrencyDetails c2) {
return c1.getRate2().compareTo(c2.getRate2());
}
});
}
if ("DESC".equals(order.getDirection().toString())) {
filteredList.sort(new Comparator<CurrencyDetails>() {
@Override
public int compare(CurrencyDetails c1, CurrencyDetails c2) {
return c2.getRate2().compareTo(c1.getRate2());
}
});
}
}
}
}
List<CurrencyDetails> pageList;
int start = page.getPageNumber() * page.getPageSize();
int end = Math.min(start + page.getPageSize(), filteredList.size());
if (start < end) {
pageList = filteredList.subList(start, end);
} else {
pageList = new ArrayList<>();
}
return new PageImpl<>(pageList, page, filteredList.size());
}

public void checkCode(String code) {
if (!StringUtils.hasText(code)) {
throw new FailureMessageException("valid.fail");
Expand Down
24 changes: 0 additions & 24 deletions src/main/resources/currency.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@
"description": "Bahamian Dollar",
"rate": 1
},
{
"id": 22,
"name": "BTC",
"description": "Bitcoin",
"rate": 0
},
{
"id": 23,
"name": "BTN",
Expand Down Expand Up @@ -959,12 +953,6 @@
"description": "Silver Ounce",
"rate": 0.05
},
{
"id": 161,
"name": "XAU",
"description": "Gold Ounce",
"rate": 0
},
{
"id": 162,
"name": "XCD",
Expand All @@ -983,24 +971,12 @@
"description": "CFA Franc BCEAO",
"rate": 624.89
},
{
"id": 165,
"name": "XPD",
"description": "Palladium Ounce",
"rate": 0
},
{
"id": 166,
"name": "XPF",
"description": "CFP Franc",
"rate": 113.68
},
{
"id": 167,
"name": "XPT",
"description": "Platinum Ounce",
"rate": 0
},
{
"id": 168,
"name": "YER",
Expand Down

0 comments on commit 60b9b9e

Please sign in to comment.