Rev 21532 | Rev 21730 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.time.LocalDateTime;import java.time.ZoneId;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.UserAccounts;import com.spice.profitmandi.dao.entity.UserWallet;import com.spice.profitmandi.dao.entity.UserWalletHistory;import com.spice.profitmandi.dao.enumuration.AccountType;import com.spice.profitmandi.dao.repository.UserAccountRepository;import com.spice.profitmandi.dao.repository.WalletRepository;import com.spice.profitmandi.web.model.ProfitMandiResponse;import com.spice.profitmandi.web.model.ResponseStatus;import com.spice.profitmandi.web.res.UserWalletHistoryResponse;import com.spice.profitmandi.web.res.UserWalletResponse;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;@Controller@Transactionalpublic class WalletController {private static final Logger log=LoggerFactory.getLogger(WalletController.class);@AutowiredWalletRepository walletRepository;@AutowiredUserAccountRepository userAccountRepository;@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",required = true, dataType = "string", paramType = "header")})@ApiOperation(value = "")public ResponseEntity<?> getMyWallet(HttpServletRequest request){int userId = (int)request.getAttribute("userId");UserAccounts userAccount;UserWallet wallet=null;try {userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);} catch (ProfitMandiBusinessException e) {log.error("Unable to get user account ",e);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);}try {wallet = walletRepository.getWalletForUser(Integer.valueOf(userAccount.getAccount_key()));log.info("wallet "+wallet);} catch (NumberFormatException | ProfitMandiBusinessException e) {log.error("Unable to get userwallet ",e);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);}if (wallet == null){final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, new UserWalletResponse());return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}UserWalletResponse uwr = new UserWalletResponse();uwr.setAmount(wallet.getAmount());uwr.setRefundableAmount(wallet.getRefundable_amount());final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, uwr);return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",required = true, dataType = "string", paramType = "header")})@ApiOperation(value = "")public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request){int userId = (int)request.getAttribute("userId");UserAccounts userAccount;List<UserWalletHistory> walletHistory;try {userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);} catch (ProfitMandiBusinessException e) {log.error("Unable to get user account ",e);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);}try {walletHistory = walletRepository.getWalletHistoryForUser(Integer.valueOf(userAccount.getAccount_key()));} catch (NumberFormatException | ProfitMandiBusinessException e) {log.error("Unable to get wallet history ",e);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, new ArrayList<UserWalletHistoryResponse>());return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);}ArrayList<UserWalletHistoryResponse> walletHistoryResponse = new ArrayList<UserWalletHistoryResponse>();for (UserWalletHistory uwh : walletHistory){UserWalletHistoryResponse uwhr = new UserWalletHistoryResponse();uwhr.setAmount(uwh.getAmount());uwhr.setReference(uwh.getReference());try{uwhr.setReference_type(uwh.getReference_type().toString());}catch(Exception e){uwhr.setReference_type("");}try{uwhr.setSub_reference_type(uwh.getSub_reference_type().toString());}catch(Exception e){uwhr.setSub_reference_type("");}uwhr.setTimestamp(uwh.getTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());uwhr.setDescription(uwh.getDescription());walletHistoryResponse.add(uwhr);}final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, walletHistoryResponse);return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}}