Subversion Repositories SmartDukaan

Rev

Rev 29707 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22354 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
26806 amit.gupta 3
import java.util.ArrayList;
25170 amit.gupta 4
import java.util.List;
26807 amit.gupta 5
import java.util.stream.Collectors;
25170 amit.gupta 6
 
22354 ashik.ali 7
import javax.servlet.http.HttpServletRequest;
8
 
26838 amit.gupta 9
import org.apache.commons.lang3.StringUtils;
25170 amit.gupta 10
import org.apache.logging.log4j.LogManager;
23568 govind 11
import org.apache.logging.log4j.Logger;
22354 ashik.ali 12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.stereotype.Controller;
15
import org.springframework.transaction.annotation.Transactional;
26806 amit.gupta 16
import org.springframework.web.bind.annotation.RequestBody;
22354 ashik.ali 17
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestMethod;
19
import org.springframework.web.bind.annotation.RequestParam;
20
 
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.model.CustomAddress;
23
import com.spice.profitmandi.common.model.CustomCustomer;
24
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25
import com.spice.profitmandi.common.web.util.ResponseSender;
26
import com.spice.profitmandi.dao.entity.fofo.Customer;
27
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
29707 tejbeer 28
import com.spice.profitmandi.dao.repository.catalog.CustomerOfferRepository;
26806 amit.gupta 29
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
22354 ashik.ali 30
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
26817 amit.gupta 31
import com.spice.profitmandi.service.CustomerService;
22354 ashik.ali 32
 
33
@Controller
35458 amit 34
@Transactional(rollbackFor = Throwable.class)
22354 ashik.ali 35
public class CustomerController {
25170 amit.gupta 36
 
23568 govind 37
	private static final Logger LOGGER = LogManager.getLogger(CustomerController.class);
25170 amit.gupta 38
 
22354 ashik.ali 39
	@Autowired
22927 ashik.ali 40
	private ResponseSender<?> responseSender;
25170 amit.gupta 41
 
22354 ashik.ali 42
	@Autowired
22927 ashik.ali 43
	private CustomerRepository customerRepository;
25170 amit.gupta 44
 
26806 amit.gupta 45
	@Autowired
26817 amit.gupta 46
	private CustomerService customerService;
47
 
48
	@Autowired
26806 amit.gupta 49
	private CustomerAddressRepository customerAddressRepository;
50
 
51
	@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
52
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
53
			@RequestBody CustomAddress customAddress) {
54
		CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
55
		customerAddressRepository.persist(customerAddress);
56
		return responseSender.ok(this.toCustomAddress(customerAddress));
57
 
58
	}
59
 
29707 tejbeer 60
	@RequestMapping(value = "/customer/update/address", method = RequestMethod.POST)
61
	public ResponseEntity<?> updateAddress(HttpServletRequest request, @RequestBody CustomAddress customAddress)
62
			throws ProfitMandiBusinessException {
63
		CustomerAddress customerAddress = customerAddressRepository.selectById(customAddress.getId());
64
 
65
		customerAddress.setCity(customAddress.getCity());
66
		customerAddress.setCountry(customAddress.getCountry());
67
		customerAddress.setLandmark(customAddress.getLandmark());
68
		customerAddress.setLine1(customAddress.getLine1());
69
		customerAddress.setLine2(customAddress.getLine2());
70
		customerAddress.setName(customAddress.getName());
71
		customerAddress.setLastName(customAddress.getLastName());
72
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
73
		customerAddress.setPinCode(customAddress.getPinCode());
74
		customerAddress.setState(customAddress.getState());
75
		return responseSender.ok(this.toCustomAddress(customerAddress));
76
 
77
	}
78
 
26817 amit.gupta 79
	@RequestMapping(value = "/customer/add", method = RequestMethod.POST)
29707 tejbeer 80
	public ResponseEntity<?> addCustomer(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
81
			throws ProfitMandiBusinessException {
26817 amit.gupta 82
		Customer customer = new Customer();
29707 tejbeer 83
		if (StringUtils.isEmpty(customCustomer.getFirstName())) {
26838 amit.gupta 84
			throw new ProfitMandiBusinessException("First Name", "Empty", "First Name required");
85
		}
26839 amit.gupta 86
		customer.setEmailId(customCustomer.getEmailId());
26817 amit.gupta 87
		customer.setFirstName(customCustomer.getFirstName());
88
		customer.setLastName(customCustomer.getLastName());
89
		customer.setMobileNumber(customCustomer.getMobileNumber());
90
		customer = customerService.addCustomer(customer);
91
		customCustomer.setCustomerId(customer.getId());
92
		return responseSender.ok(customCustomer);
93
 
94
	}
29707 tejbeer 95
 
27693 amit.gupta 96
	@RequestMapping(value = "/customer/add-email", method = RequestMethod.POST)
29707 tejbeer 97
	public ResponseEntity<?> addEmail(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
98
			throws ProfitMandiBusinessException {
27693 amit.gupta 99
		String emailId = customCustomer.getEmailId();
100
		int customerId = customCustomer.getCustomerId();
101
		Customer customer = customerRepository.selectById(customerId);
102
		customer.setEmailId(emailId);
103
		return this.getCustomerByMobileNumber(request, customer.getMobileNumber());
29707 tejbeer 104
 
27693 amit.gupta 105
	}
26817 amit.gupta 106
 
22354 ashik.ali 107
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
25170 amit.gupta 108
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
109
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
110
			throws ProfitMandiBusinessException {
26806 amit.gupta 111
		CustomCustomer customCustomer = null;
22927 ashik.ali 112
		LOGGER.info("Request Received at url {}", request.getRequestURI());
26806 amit.gupta 113
		try {
114
			Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
115
			customCustomer = new CustomCustomer();
116
			customCustomer.setCustomerId(customer.getId());
24087 amit.gupta 117
			customCustomer.setEmailId(customer.getEmailId());
118
			customCustomer.setFirstName(customer.getFirstName());
119
			customCustomer.setLastName(customer.getLastName());
120
			customCustomer.setMobileNumber(customer.getMobileNumber());
26807 amit.gupta 121
			LOGGER.info(customer.getCustomerAddress());
122
			List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream()
123
					.sorted((CustomerAddress c1, CustomerAddress c2) -> {
124
						return c1.getCreateTimestamp().isBefore(c2.getCreateTimestamp()) ? 1 : -1;
125
					}).limit(5).collect(Collectors.toList());
126
			LOGGER.info(customerAddresses);
127
			if (!customerAddresses.isEmpty()) {
26806 amit.gupta 128
				List<CustomAddress> customAddresses = new ArrayList<>();
26807 amit.gupta 129
				for (CustomerAddress customerAddress : customerAddresses) {
26806 amit.gupta 130
					customAddresses.add(this.toCustomAddress(customerAddress));
131
				}
132
				customCustomer.setAddresses(customAddresses);
24087 amit.gupta 133
			}
26806 amit.gupta 134
		} catch (Exception e) {
26807 amit.gupta 135
			e.printStackTrace();
22354 ashik.ali 136
		}
26806 amit.gupta 137
		return responseSender.ok(customCustomer);
22354 ashik.ali 138
	}
26806 amit.gupta 139
 
140
	private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
141
		CustomAddress customAddress = new CustomAddress();
142
		customAddress.setCity(customerAddress.getCity());
143
		customAddress.setCountry(customerAddress.getCountry());
144
		customAddress.setLandmark(customerAddress.getLandmark());
145
		customAddress.setLine1(customerAddress.getLine1());
146
		customAddress.setLine2(customerAddress.getLine2());
147
		customAddress.setName(customerAddress.getName());
148
		customAddress.setLastName(customerAddress.getLastName());
149
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
150
		customAddress.setPinCode(customerAddress.getPinCode());
151
		customAddress.setState(customerAddress.getState());
152
		customAddress.setId(customerAddress.getId());
153
		return customAddress;
154
	}
155
 
156
	private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
157
		CustomerAddress customerAddress = new CustomerAddress();
158
		customerAddress.setCustomerId(customerId);
159
		customerAddress.setName(customAddress.getName());
160
		customerAddress.setLastName(customAddress.getLastName());
161
		customerAddress.setLine1(customAddress.getLine1());
162
		customerAddress.setLine2(customAddress.getLine2());
163
		customerAddress.setLandmark(customAddress.getLandmark());
164
		customerAddress.setCity(customAddress.getCity());
165
		customerAddress.setPinCode(customAddress.getPinCode());
166
		customerAddress.setState(customAddress.getState());
167
		customerAddress.setCountry(customAddress.getCountry());
168
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
169
 
170
		return customerAddress;
171
	}
22354 ashik.ali 172
}