Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
markliu2013 committed Jun 15, 2024
1 parent 60b9b9e commit b16748c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 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.65");
return new DataResponse<>("93.66");
}

@GetMapping("/test3")
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/cn/biq/mn/currency/ChangeRateForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.biq.mn.currency;

import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;

@Getter
@Setter
public class ChangeRateForm {

private String base;
private BigDecimal rate;

}
13 changes: 7 additions & 6 deletions src/main/java/cn/biq/mn/currency/CurrencyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import cn.biq.mn.response.DataResponse;
import cn.biq.mn.response.PageResponse;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
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;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

Expand Down Expand Up @@ -47,7 +45,10 @@ public BaseResponse handleCalc(@RequestParam String from, @RequestParam String t
return new DataResponse<>(currencyService.calc(from, to, amount));
}



@RequestMapping(method = RequestMethod.PUT, value = "/{id}/rate")
public BaseResponse handleUpdateRate(@PathVariable("id") Integer id, @Valid @RequestBody ChangeRateForm form) {
currencyService.changeRate(id, form);
return new BaseResponse(true);
}

}
12 changes: 12 additions & 0 deletions src/main/java/cn/biq/mn/currency/CurrencyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,16 @@ public BigDecimal calc(String from, String to, BigDecimal amount) {
return amount.multiply(rate).setScale(2, RoundingMode.HALF_EVEN);
}

public void changeRate(Integer id, ChangeRateForm form) {
List<CurrencyDetails> currencyList = applicationScopeBean.getCurrencyDetailsList();
CurrencyDetails currencyDetails;
Optional<CurrencyDetails> currencyDetailsOptional = currencyList.stream().filter(i -> i.getId().equals(id)).findFirst();
if (currencyDetailsOptional.isPresent()) {
currencyDetails = currencyDetailsOptional.get();
} else {
throw new ItemNotFoundException();
}
currencyDetails.setRate(form.getRate().multiply(convert(form.getBase(), "USD")).doubleValue());
}

}

0 comments on commit b16748c

Please sign in to comment.