Subversion Repositories SmartDukaan

Rev

Rev 26807 | Rev 26838 | 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
 
25170 amit.gupta 9
import org.apache.logging.log4j.LogManager;
23568 govind 10
import org.apache.logging.log4j.Logger;
22354 ashik.ali 11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.transaction.annotation.Transactional;
26806 amit.gupta 15
import org.springframework.web.bind.annotation.RequestBody;
22354 ashik.ali 16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestMethod;
18
import org.springframework.web.bind.annotation.RequestParam;
19
 
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.CustomAddress;
22
import com.spice.profitmandi.common.model.CustomCustomer;
23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24
import com.spice.profitmandi.common.web.util.ResponseSender;
25
import com.spice.profitmandi.dao.entity.fofo.Customer;
26
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
26806 amit.gupta 27
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
22354 ashik.ali 28
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
26817 amit.gupta 29
import com.spice.profitmandi.service.CustomerService;
22354 ashik.ali 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
26817 amit.gupta 44
	private CustomerService customerService;
45
 
46
	@Autowired
26806 amit.gupta 47
	private CustomerAddressRepository customerAddressRepository;
48
 
49
	@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
50
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
51
			@RequestBody CustomAddress customAddress) {
52
		CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
53
		customerAddressRepository.persist(customerAddress);
54
		return responseSender.ok(this.toCustomAddress(customerAddress));
55
 
56
	}
57
 
26817 amit.gupta 58
	@RequestMapping(value = "/customer/add", method = RequestMethod.POST)
59
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CustomCustomer customCustomer) {
60
		Customer customer = new Customer();
61
		customer.setEmailId(customCustomer.getEmailId());
62
		customer.setFirstName(customCustomer.getFirstName());
63
		customer.setLastName(customCustomer.getLastName());
64
		customer.setMobileNumber(customCustomer.getMobileNumber());
65
		customer = customerService.addCustomer(customer);
66
		customCustomer.setCustomerId(customer.getId());
67
		return responseSender.ok(customCustomer);
68
 
69
	}
70
 
22354 ashik.ali 71
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
25170 amit.gupta 72
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
73
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
74
			throws ProfitMandiBusinessException {
26806 amit.gupta 75
		CustomCustomer customCustomer = null;
22927 ashik.ali 76
		LOGGER.info("Request Received at url {}", request.getRequestURI());
26806 amit.gupta 77
		try {
78
			Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
79
			customCustomer = new CustomCustomer();
80
			customCustomer.setCustomerId(customer.getId());
24087 amit.gupta 81
			customCustomer.setEmailId(customer.getEmailId());
82
			customCustomer.setFirstName(customer.getFirstName());
83
			customCustomer.setLastName(customer.getLastName());
84
			customCustomer.setMobileNumber(customer.getMobileNumber());
26807 amit.gupta 85
			LOGGER.info(customer.getCustomerAddress());
86
			List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream()
87
					.sorted((CustomerAddress c1, CustomerAddress c2) -> {
88
						return c1.getCreateTimestamp().isBefore(c2.getCreateTimestamp()) ? 1 : -1;
89
					}).limit(5).collect(Collectors.toList());
90
			LOGGER.info(customerAddresses);
91
			if (!customerAddresses.isEmpty()) {
26806 amit.gupta 92
				List<CustomAddress> customAddresses = new ArrayList<>();
26807 amit.gupta 93
				for (CustomerAddress customerAddress : customerAddresses) {
26806 amit.gupta 94
					customAddresses.add(this.toCustomAddress(customerAddress));
95
				}
96
				customCustomer.setAddresses(customAddresses);
24087 amit.gupta 97
			}
26806 amit.gupta 98
		} catch (Exception e) {
26807 amit.gupta 99
			e.printStackTrace();
22354 ashik.ali 100
		}
26806 amit.gupta 101
		return responseSender.ok(customCustomer);
22354 ashik.ali 102
	}
26806 amit.gupta 103
 
104
	private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
105
		CustomAddress customAddress = new CustomAddress();
106
		customAddress.setCity(customerAddress.getCity());
107
		customAddress.setCountry(customerAddress.getCountry());
108
		customAddress.setLandmark(customerAddress.getLandmark());
109
		customAddress.setLine1(customerAddress.getLine1());
110
		customAddress.setLine2(customerAddress.getLine2());
111
		customAddress.setName(customerAddress.getName());
112
		customAddress.setLastName(customerAddress.getLastName());
113
		customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
114
		customAddress.setPinCode(customerAddress.getPinCode());
115
		customAddress.setState(customerAddress.getState());
116
		customAddress.setId(customerAddress.getId());
117
		return customAddress;
118
	}
119
 
120
	private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
121
		CustomerAddress customerAddress = new CustomerAddress();
122
		customerAddress.setCustomerId(customerId);
123
		customerAddress.setName(customAddress.getName());
124
		customerAddress.setLastName(customAddress.getLastName());
125
		customerAddress.setLine1(customAddress.getLine1());
126
		customerAddress.setLine2(customAddress.getLine2());
127
		customerAddress.setLandmark(customAddress.getLandmark());
128
		customerAddress.setCity(customAddress.getCity());
129
		customerAddress.setPinCode(customAddress.getPinCode());
130
		customerAddress.setState(customAddress.getState());
131
		customerAddress.setCountry(customAddress.getCountry());
132
		customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
133
 
134
		return customerAddress;
135
	}
22354 ashik.ali 136
}