Subversion Repositories SmartDukaan

Rev

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