Subversion Repositories SmartDukaan

Rev

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