Subversion Repositories SmartDukaan

Rev

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