Subversion Repositories SmartDukaan

Rev

Rev 21740 | Rev 22037 | 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
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request){
95
		int userId = (int)request.getAttribute("userId");
96
		UserAccounts userAccount;
97
		List<UserWalletHistory> walletHistory;
98
		try {
99
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
100
		} catch (ProfitMandiBusinessException e) {
101
			log.error("Unable to get user account ",e);
102
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
103
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
104
		}
105
 
106
		try {
107
			walletHistory = walletRepository.getWalletHistoryForUser(Integer.valueOf(userAccount.getAccount_key()));
108
		} catch (NumberFormatException | ProfitMandiBusinessException e) {
109
			log.error("Unable to get wallet history ",e);
110
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, new ArrayList<UserWalletHistoryResponse>());
111
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
112
		}
113
 
114
		ArrayList<UserWalletHistoryResponse> walletHistoryResponse = new ArrayList<UserWalletHistoryResponse>();
115
 
116
		for (UserWalletHistory uwh : walletHistory){
117
			UserWalletHistoryResponse uwhr = new UserWalletHistoryResponse();
118
			uwhr.setAmount(uwh.getAmount());
119
			uwhr.setReference(uwh.getReference());
120
			try{
121
				uwhr.setReference_type(uwh.getReference_type().toString());
122
			}
123
			catch(Exception e){
124
				uwhr.setReference_type("");
125
			}
126
			uwhr.setTimestamp(uwh.getTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
127
			uwhr.setDescription(uwh.getDescription());
128
			walletHistoryResponse.add(uwhr);
129
		}
130
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, walletHistoryResponse);
131
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
132
	}
133
 
134
}