Subversion Repositories SmartDukaan

Rev

Rev 26806 | Rev 26817 | Go to most recent revision | 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.time.temporal.ChronoField;
4
import java.util.ArrayList;
25170 amit.gupta 5
import java.util.List;
26807 amit.gupta 6
import java.util.stream.Collectors;
25170 amit.gupta 7
 
22354 ashik.ali 8
import javax.servlet.http.HttpServletRequest;
9
 
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;
26806 amit.gupta 28
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
22354 ashik.ali 29
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
30
 
31
@Controller
32
@Transactional
33
public class CustomerController {
25170 amit.gupta 34
 
23568 govind 35
	private static final Logger LOGGER = LogManager.getLogger(CustomerController.class);
25170 amit.gupta 36
 
22354 ashik.ali 37
	@Autowired
22927 ashik.ali 38
	private ResponseSender<?> responseSender;
25170 amit.gupta 39
 
22354 ashik.ali 40
	@Autowired
22927 ashik.ali 41
	private CustomerRepository customerRepository;
25170 amit.gupta 42
 
26806 amit.gupta 43
	@Autowired
44
	private CustomerAddressRepository customerAddressRepository;
45
 
46
	@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
47
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
48
			@RequestBody CustomAddress customAddress) {
49
		CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
50
		customerAddressRepository.persist(customerAddress);
51
		return responseSender.ok(this.toCustomAddress(customerAddress));
52
 
53
	}
54
 
22354 ashik.ali 55
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
25170 amit.gupta 56
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
57
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
58
			throws ProfitMandiBusinessException {
26806 amit.gupta 59
		CustomCustomer customCustomer = null;
22927 ashik.ali 60
		LOGGER.info("Request Received at url {}", request.getRequestURI());
26806 amit.gupta 61
		try {
62
			Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
63
			customCustomer = new CustomCustomer();
64
			customCustomer.setCustomerId(customer.getId());
24087 amit.gupta 65
			customCustomer.setEmailId(customer.getEmailId());
66
			customCustomer.setFirstName(customer.getFirstName());
67
			customCustomer.setLastName(customer.getLastName());
68
			customCustomer.setMobileNumber(customer.getMobileNumber());
26807 amit.gupta 69
			LOGGER.info(customer.getCustomerAddress());
70
			List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream()
71
					.sorted((CustomerAddress c1, CustomerAddress c2) -> {
72
						return c1.getCreateTimestamp().isBefore(c2.getCreateTimestamp()) ? 1 : -1;
73
					}).limit(5).collect(Collectors.toList());
74
			LOGGER.info(customerAddresses);
75
			if (!customerAddresses.isEmpty()) {
26806 amit.gupta 76
				List<CustomAddress> customAddresses = new ArrayList<>();
26807 amit.gupta 77
				for (CustomerAddress customerAddress : customerAddresses) {
26806 amit.gupta 78
					customAddresses.add(this.toCustomAddress(customerAddress));
79
				}
80
				customCustomer.setAddresses(customAddresses);
24087 amit.gupta 81
			}
26806 amit.gupta 82
		} catch (Exception e) {
26807 amit.gupta 83
			e.printStackTrace();
22354 ashik.ali 84
		}
26806 amit.gupta 85
		return responseSender.ok(customCustomer);
22354 ashik.ali 86
	}
26806 amit.gupta 87
 
88
	private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
89
		CustomAddress customAddress = new CustomAddress();
90
		customAddress.setCity(customerAddress.getCity());
91
		customAddress.setCountry(customerAddress.getCountry());
92
		customAddress.setLandmark(customerAddress.getLandmark());
93
		customAddress.setLine1(customerAddress.getLine1());
94
		customAddress.setLine2(customerAddress.getLine2());
95
		customAddress.setName(customerAddress.getName());
96
		customAddress.setLastName(customerAddress.getLastName());
97
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
98
		customAddress.setPinCode(customerAddress.getPinCode());
99
		customAddress.setState(customerAddress.getState());
100
		customAddress.setId(customerAddress.getId());
101
		return customAddress;
102
	}
103
 
104
	private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
105
		CustomerAddress customerAddress = new CustomerAddress();
106
		customerAddress.setCustomerId(customerId);
107
		customerAddress.setName(customAddress.getName());
108
		customerAddress.setLastName(customAddress.getLastName());
109
		customerAddress.setLine1(customAddress.getLine1());
110
		customerAddress.setLine2(customAddress.getLine2());
111
		customerAddress.setLandmark(customAddress.getLandmark());
112
		customerAddress.setCity(customAddress.getCity());
113
		customerAddress.setPinCode(customAddress.getPinCode());
114
		customerAddress.setState(customAddress.getState());
115
		customerAddress.setCountry(customAddress.getCountry());
116
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
117
 
118
		return customerAddress;
119
	}
22354 ashik.ali 120
}