Subversion Repositories SmartDukaan

Rev

Rev 22555 | Rev 22609 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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