Subversion Repositories SmartDukaan

Rev

Rev 21795 | Rev 22605 | 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.common.model.ProfitMandiResponse;
import com.spice.profitmandi.common.model.ResponseStatus;
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.transaction.WalletRepository;
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
@Transactional(rollbackFor=Throwable.class)
public class WalletController {

        private static final Logger log=LoggerFactory.getLogger(WalletController.class);

        @Autowired
        WalletRepository walletRepository;

        @Autowired
        UserAccountRepository 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")
        })
        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("");
                        }
                        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);
        }

}