Webflux:累积和详细信息

huangapple go评论107阅读模式
英文:

Webflux : Accumulate & details

问题

I'm new to webflux and I'm trying to transform this code:

  1. @Getter
  2. public class Stock {
  3. private int value;
  4. private String productNumber;
  5. public Stock(int value, String productNumber){
  6. this.value = value;
  7. this.productNumber = productNumber;
  8. }
  9. }
  10. @Getter
  11. public class StockResponse {
  12. private int value;
  13. private String productNumber;
  14. public StockResponse(int value, String productNumber){
  15. this.value = value;
  16. this.productNumber = productNumber;
  17. }
  18. public StockResponse(Stock stock){
  19. this.value = stock.getValue();
  20. this.productNumber = stock.getProductNumber();
  21. }
  22. public void add(int value){
  23. this.value += value;
  24. }
  25. }
  26. public List<StockResponse> findStockResponsesDetailsAndTotal() {
  27. List<StockResponse> stocksResponses = new ArrayList();
  28. List<Stock> stocks = stockRepository.findAll();
  29. StockResponse total = new StockResponse(0, "Total");
  30. stocks.forEach(stock -> {
  31. total.add(stock.getValue());
  32. stocksResponses.add(new StockResponse(stock));
  33. });
  34. stocksResponses.add(total);
  35. return stocksResponses;
  36. }

into a "flux philosophy"
Here is my code so far:

  1. stockRepository.findAll()
  2. .map(StockResponse::convert)

I'm kinda lost about the "total", I know that I can do a reduce on the Flux but i'll lose all the details in doing so.

英文:

I'm new to webflux and I'm trying to transform this code :

  1. @Getter
  2. public class Stock {
  3. private int value;
  4. private String productNumber;
  5. public Stock(int value, String productNumber){
  6. this.value = value;
  7. this.productNumber = productNumber;
  8. }
  9. }
  10. @Getter
  11. public class StockResponse {
  12. private int value;
  13. private String productNumber;
  14. public StockResponse(int value, String productNumber){
  15. this.value = value;
  16. this.productNumber = productNumber;
  17. }
  18. public StockResponse(Stock stock){
  19. this.value = stock.getValue();
  20. this.productNumber = stock.getProductNumber();
  21. }
  22. public void add(int value){
  23. this.value += value;
  24. }
  25. }
  26. public List<StockResponse> findStockResponsesDetailsAndTotal() {
  27. List<StockResponse> stocksResponses = new ArrayList();
  28. List<Stock> stocks = stockRepository.findAll();
  29. StockResponse total = new StockResponse(0, "Total");
  30. stocks.forEach(stock -> {
  31. total.add(stock.getValue());
  32. stocksResponses.add(new StockResponse(stock));
  33. });
  34. stocksResponses.add(total);
  35. return stocksResponses;
  36. }

into a "flux phylosophy"
Here is my code so far :

  1. stockRepository.findAll()
  2. .map(StockResponse::convert)

I'm kinda lost about the "total", I know that I can do a reduce on the Flux but i'll lose all the details in doing so

答案1

得分: 1

I find it a bit odd to add the total result as the last element in the list instead of having a dedicated class containing the total on one side and the elements on the other).

But here's a code snippet anyway:

  1. Mono<List<StockResponse>> responses = findAll()
  2. .map(StockResponse::new).collectList().map(allStock -> {
  3. StockResponse total = new StockResponse(0, "Total");
  4. allStock.forEach(stock -> total.add(stock.value));
  5. List<StockResponse> result = new ArrayList<>(allStock);
  6. result.add(total);
  7. return result;
  8. });
英文:

I find it a bit odd to add the total result as the last element in the list instead of having a dedicated class containing the total on one side and the elements on the other).

But here's a code snippet anyway:

  1. Mono&lt;List&lt;StockResponse&gt;&gt; responses = findAll()
  2. .map(StockResponse::new).collectList().map(allStock -&gt; {
  3. StockResponse total = new StockResponse(0, &quot;Total&quot;);
  4. allStock.forEach(stock -&gt; total.add(stock.value));
  5. List&lt;StockResponse&gt; result = new ArrayList&lt;&gt;(allStock);
  6. result.add(total);
  7. return result;
  8. });

huangapple
  • 本文由 发表于 2023年6月26日 20:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定