Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. package io.everytrade.server.api.endpoint;
  2.  
  3. import io.everytrade.model.Portfolio;
  4. import io.everytrade.model.PortfolioBalance;
  5. import io.everytrade.model.PortfolioBalanceValue;
  6. import io.everytrade.model.PortfolioHistory;
  7. import io.everytrade.model.PortfolioResultValue;
  8. import io.everytrade.model.RawTransaction;
  9. import io.everytrade.server.api.endpoint.dto.PortfolioBalanceDTO;
  10. import io.everytrade.server.api.endpoint.dto.PortfolioHistoryDTO;
  11. import io.everytrade.server.api.endpoint.dto.PortfolioOpenDTO;
  12. import io.everytrade.server.api.endpoint.dto.PortfolioSoldDTO;
  13. import io.everytrade.server.auth.ResourceBase;
  14.  
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.ws.rs.Consumes;
  17. import javax.ws.rs.GET;
  18. import javax.ws.rs.Path;
  19. import javax.ws.rs.PathParam;
  20. import javax.ws.rs.Produces;
  21. import javax.ws.rs.QueryParam;
  22. import javax.ws.rs.core.Context;
  23. import javax.ws.rs.core.MediaType;
  24. import javax.ws.rs.core.Response;
  25. import java.time.Instant;
  26. import java.time.LocalDateTime;
  27. import java.util.ArrayList;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.List;
  31.  
  32.  
  33. @Path("/")
  34. public class BalancesEndpoint extends ResourceBase {
  35. @Context
  36. private HttpServletRequest request;
  37.  
  38. @GET
  39. @Path("portfolio/{id}/balances/")
  40. @Consumes(MediaType.APPLICATION_JSON)
  41. @Produces(MediaType.APPLICATION_JSON)
  42. public Response post(@PathParam("id") long portfolioId, @QueryParam("type") String type, @QueryParam("timestamp") Long timestamp) {
  43. /*if(type == null){
  44. return error("Missing type for balances request.");
  45. }*/
  46.  
  47. Portfolio portfolio = jpa.selectByID(Portfolio.class,portfolioId);
  48. if(portfolio == null){
  49. return error("Portfolio "+portfolioId+" not found");
  50. }
  51. if(portfolio.getOrganization().getId()!=getUser().getPerson().getOrganization().getId()){
  52. System.out.println("portfolio "+portfolio.getOrganization().getId()+"/"+getUser().getPerson().getOrganization().getId());
  53. return error(401);
  54. }
  55.  
  56.  
  57. Date d;
  58. if(timestamp == null){
  59. d = new Date();
  60. }else{
  61. d = new Date(timestamp);
  62. }
  63.  
  64. PortfolioHistory history = jpa.selectFirstHistoryBeforeWithPortfolio(d, portfolio);
  65.  
  66. if(history == null){
  67. return ok(new ArrayList<>());//error("No history for "+d+" and portfolio: "+portfolioId);
  68. }
  69. if(type == null){
  70. List<PortfolioBalanceDTO> result = new ArrayList<>();
  71. for (PortfolioBalance balance : jpa.selectPortfolioBalance(history)) {
  72. List<PortfolioBalanceValue> portfolioBalanceValues = jpa.selectBalanceValuesByBalance(balance);
  73. List<PortfolioResultValue> portfolioResultValues = jpa.selectResultValuesByBalance(balance);
  74.  
  75. result.add(new PortfolioBalanceDTO(balance,portfolioResultValues,portfolioBalanceValues));
  76. }
  77. return ok(result);
  78. }
  79. switch (type.toLowerCase()){
  80. case "open":
  81. //bought
  82. List<PortfolioOpenDTO> openResult = new ArrayList<>();
  83. for (PortfolioBalance balance : jpa.selectPortfolioBalance(history, RawTransaction.TRANSACTION_RECORD_TYPE_BUY)) {
  84. List<PortfolioBalanceValue> portfolioBalanceValues = jpa.selectBalanceValuesByBalance(balance);
  85.  
  86. PortfolioOpenDTO open = new PortfolioOpenDTO(balance,portfolioBalanceValues);
  87. openResult.add(open);
  88. }
  89. return ok(openResult);
  90.  
  91. case "sold":
  92. //sold
  93.  
  94. List<PortfolioSoldDTO> result = new ArrayList<>();
  95. for (PortfolioBalance balance : jpa.selectPortfolioBalance(history, RawTransaction.TRANSACTION_RECORD_TYPE_SELL)) {
  96. List<PortfolioResultValue> portfolioResultValues = jpa.selectResultValuesByBalance(balance);
  97.  
  98. PortfolioSoldDTO sold = new PortfolioSoldDTO(balance,portfolioResultValues);
  99. result.add(sold);
  100. }
  101. return ok(result);
  102. default:
  103. return error("Unknown type for balances: "+type);
  104. }
  105. }
  106.  
  107.  
  108. @GET
  109. @Path("portfolio/{id}/history/")
  110. @Consumes(MediaType.APPLICATION_JSON)
  111. @Produces(MediaType.APPLICATION_JSON)
  112. public Response getHistories(@PathParam("id") long portfolioId, @QueryParam("typeDISABLED") String type, @QueryParam("from") Long from, @QueryParam("to") Long to) {
  113. System.out.println("FROM: "+from+"/ TO: "+to);
  114. /*if(type == null){
  115. return error("Missing type for balances request.");
  116. }*/
  117.  
  118. Portfolio portfolio = jpa.selectByID(Portfolio.class,portfolioId);
  119. if(portfolio == null){
  120. return error("Portfolio "+portfolioId+" not found");
  121. }
  122. if(portfolio.getOrganization().getId()!=getUser().getPerson().getOrganization().getId()){
  123. System.out.println("portfolio "+portfolio.getOrganization().getId()+"/"+getUser().getPerson().getOrganization().getId());
  124. return error(401);
  125. }
  126.  
  127.  
  128. Date fromDate;
  129. Date toDate;
  130. if(from == null){
  131. Calendar now = Calendar.getInstance();
  132. now.add(Calendar.YEAR, -1);
  133. fromDate = now.getTime();
  134. }else{
  135. fromDate = new Date(from*1000);
  136. }
  137. if(to == null){
  138. toDate = new Date();
  139. }else{
  140. toDate = new Date(to*1000);
  141. }
  142.  
  143. List<PortfolioHistory> histories = jpa.selectHistoriesFromToWithPortfolio(fromDate,toDate, portfolio);
  144.  
  145. List<PortfolioHistoryDTO> historiesDTO = new ArrayList<>();
  146. for(PortfolioHistory history : histories) {
  147. if (type == null) {
  148. List<PortfolioBalanceDTO> balances = new ArrayList<>();
  149. for (PortfolioBalance balance : jpa.selectPortfolioBalance(history)) {
  150. List<PortfolioBalanceValue> portfolioBalanceValues = jpa.selectBalanceValuesByBalance(balance);
  151. List<PortfolioResultValue> portfolioResultValues = jpa.selectResultValuesByBalance(balance);
  152.  
  153. balances.add(new PortfolioBalanceDTO(balance, portfolioResultValues, portfolioBalanceValues));
  154. }
  155. PortfolioHistoryDTO newHistory = new PortfolioHistoryDTO(history.getAt(),balances);
  156. historiesDTO.add(newHistory);
  157. }
  158. }
  159. return ok(historiesDTO);
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement