Subversion Repositories SmartDukaan

Rev

Rev 26839 | Rev 29707 | 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)
27693 amit.gupta 60
	public ResponseEntity<?> addCustomer(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
	}
27693 amit.gupta 74
	@RequestMapping(value = "/customer/add-email", method = RequestMethod.POST)
75
	public ResponseEntity<?> addEmail(HttpServletRequest request, @RequestBody CustomCustomer customCustomer) throws ProfitMandiBusinessException {
76
		String emailId = customCustomer.getEmailId();
77
		int customerId = customCustomer.getCustomerId();
78
		Customer customer = customerRepository.selectById(customerId);
79
		customer.setEmailId(emailId);
80
		return this.getCustomerByMobileNumber(request, customer.getMobileNumber());
81
 
82
	}
26817 amit.gupta 83
 
22354 ashik.ali 84
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
25170 amit.gupta 85
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
86
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
87
			throws ProfitMandiBusinessException {
26806 amit.gupta 88
		CustomCustomer customCustomer = null;
22927 ashik.ali 89
		LOGGER.info("Request Received at url {}", request.getRequestURI());
26806 amit.gupta 90
		try {
91
			Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
92
			customCustomer = new CustomCustomer();
93
			customCustomer.setCustomerId(customer.getId());
24087 amit.gupta 94
			customCustomer.setEmailId(customer.getEmailId());
95
			customCustomer.setFirstName(customer.getFirstName());
96
			customCustomer.setLastName(customer.getLastName());
97
			customCustomer.setMobileNumber(customer.getMobileNumber());
26807 amit.gupta 98
			LOGGER.info(customer.getCustomerAddress());
99
			List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream()
100
					.sorted((CustomerAddress c1, CustomerAddress c2) -> {
101
						return c1.getCreateTimestamp().isBefore(c2.getCreateTimestamp()) ? 1 : -1;
102
					}).limit(5).collect(Collectors.toList());
103
			LOGGER.info(customerAddresses);
104
			if (!customerAddresses.isEmpty()) {
26806 amit.gupta 105
				List<CustomAddress> customAddresses = new ArrayList<>();
26807 amit.gupta 106
				for (CustomerAddress customerAddress : customerAddresses) {
26806 amit.gupta 107
					customAddresses.add(this.toCustomAddress(customerAddress));
108
				}
109
				customCustomer.setAddresses(customAddresses);
24087 amit.gupta 110
			}
26806 amit.gupta 111
		} catch (Exception e) {
26807 amit.gupta 112
			e.printStackTrace();
22354 ashik.ali 113
		}
26806 amit.gupta 114
		return responseSender.ok(customCustomer);
22354 ashik.ali 115
	}
26806 amit.gupta 116
 
117
	private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
118
		CustomAddress customAddress = new CustomAddress();
119
		customAddress.setCity(customerAddress.getCity());
120
		customAddress.setCountry(customerAddress.getCountry());
121
		customAddress.setLandmark(customerAddress.getLandmark());
122
		customAddress.setLine1(customerAddress.getLine1());
123
		customAddress.setLine2(customerAddress.getLine2());
124
		customAddress.setName(customerAddress.getName());
125
		customAddress.setLastName(customerAddress.getLastName());
126
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
127
		customAddress.setPinCode(customerAddress.getPinCode());
128
		customAddress.setState(customerAddress.getState());
129
		customAddress.setId(customerAddress.getId());
130
		return customAddress;
131
	}
132
 
133
	private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
134
		CustomerAddress customerAddress = new CustomerAddress();
135
		customerAddress.setCustomerId(customerId);
136
		customerAddress.setName(customAddress.getName());
137
		customerAddress.setLastName(customAddress.getLastName());
138
		customerAddress.setLine1(customAddress.getLine1());
139
		customerAddress.setLine2(customAddress.getLine2());
140
		customerAddress.setLandmark(customAddress.getLandmark());
141
		customerAddress.setCity(customAddress.getCity());
142
		customerAddress.setPinCode(customAddress.getPinCode());
143
		customerAddress.setState(customAddress.getState());
144
		customerAddress.setCountry(customAddress.getCountry());
145
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
146
 
147
		return customerAddress;
148
	}
22354 ashik.ali 149
}