Subversion Repositories SmartDukaan

Rev

Rev 21735 | Rev 21795 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21532 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
4
import java.time.ZoneId;
5
import java.util.ArrayList;
6
import java.util.List;
7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.HttpStatus;
14
import org.springframework.http.MediaType;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.stereotype.Controller;
21702 ashik.ali 17
import org.springframework.transaction.annotation.Transactional;
21532 kshitij.so 18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
20
 
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 23
import com.spice.profitmandi.common.model.ProfitMandiResponse;
24
import com.spice.profitmandi.common.model.ResponseStatus;
21735 ashik.ali 25
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
26
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
27
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
28
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
29
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
30
import com.spice.profitmandi.dao.repository.transaction.WalletRepository;
21532 kshitij.so 31
import com.spice.profitmandi.web.res.UserWalletHistoryResponse;
32
import com.spice.profitmandi.web.res.UserWalletResponse;
33
 
34
import io.swagger.annotations.ApiImplicitParam;
35
import io.swagger.annotations.ApiImplicitParams;
36
import io.swagger.annotations.ApiOperation;
37
 
38
@Controller
21702 ashik.ali 39
@Transactional
21532 kshitij.so 40
public class WalletController {
41
 
42
	private static final Logger log=LoggerFactory.getLogger(WalletController.class);
43
 
44
	@Autowired
45
	WalletRepository walletRepository;
46
 
47
	@Autowired
48
	UserAccountRepository userAccountRepository;
49
 
50
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
51
	@ApiImplicitParams({
52
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
53
				required = true, dataType = "string", paramType = "header")
54
	})
55
	@ApiOperation(value = "")
56
	public ResponseEntity<?> getMyWallet(HttpServletRequest request){
57
		int userId = (int)request.getAttribute("userId");
58
		UserAccounts userAccount;
59
		UserWallet wallet=null;
60
		try {
61
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
62
		} catch (ProfitMandiBusinessException e) {
63
			log.error("Unable to get user account ",e);
64
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
65
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
66
		}
67
 
68
		try {
69
			wallet = walletRepository.getWalletForUser(Integer.valueOf(userAccount.getAccount_key()));
70
			log.info("wallet "+wallet);
71
		} catch (NumberFormatException | ProfitMandiBusinessException e) {
72
			log.error("Unable to get userwallet ",e);
73
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
74
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
75
		}
76
		if (wallet == null){
77
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, new UserWalletResponse());
78
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
79
		}
80
 
81
		UserWalletResponse uwr = new UserWalletResponse();
82
		uwr.setAmount(wallet.getAmount());
83
		uwr.setRefundableAmount(wallet.getRefundable_amount());
84
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, uwr);
85
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
86
 
87
	}
88
 
89
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
90
	@ApiImplicitParams({
91
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
92
				required = true, dataType = "string", paramType = "header")
93
	})
94
	@ApiOperation(value = "")
95
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request){
96
		int userId = (int)request.getAttribute("userId");
97
		UserAccounts userAccount;
98
		List<UserWalletHistory> walletHistory;
99
		try {
100
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
101
		} catch (ProfitMandiBusinessException e) {
102
			log.error("Unable to get user account ",e);
103
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
104
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
105
		}
106
 
107
		try {
108
			walletHistory = walletRepository.getWalletHistoryForUser(Integer.valueOf(userAccount.getAccount_key()));
109
		} catch (NumberFormatException | ProfitMandiBusinessException e) {
110
			log.error("Unable to get wallet history ",e);
111
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, new ArrayList<UserWalletHistoryResponse>());
112
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
113
		}
114
 
115
		ArrayList<UserWalletHistoryResponse> walletHistoryResponse = new ArrayList<UserWalletHistoryResponse>();
116
 
117
		for (UserWalletHistory uwh : walletHistory){
118
			UserWalletHistoryResponse uwhr = new UserWalletHistoryResponse();
119
			uwhr.setAmount(uwh.getAmount());
120
			uwhr.setReference(uwh.getReference());
121
			try{
122
				uwhr.setReference_type(uwh.getReference_type().toString());
123
			}
124
			catch(Exception e){
125
				uwhr.setReference_type("");
126
			}
127
			try{
128
			uwhr.setSub_reference_type(uwh.getSub_reference_type().toString());
129
			}
130
			catch(Exception e){
131
				uwhr.setSub_reference_type("");
132
			}
133
			uwhr.setTimestamp(uwh.getTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
134
			uwhr.setDescription(uwh.getDescription());
135
			walletHistoryResponse.add(uwhr);
136
		}
137
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, walletHistoryResponse);
138
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
139
	}
140
 
141
}