Subversion Repositories SmartDukaan

Rev

Rev 25170 | Rev 26807 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 25170 Rev 26806
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.time.temporal.ChronoField;
-
 
4
import java.util.ArrayList;
3
import java.util.List;
5
import java.util.List;
4
 
6
 
5
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpServletRequest;
6
 
8
 
7
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
10
import org.apache.logging.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.ResponseEntity;
12
import org.springframework.http.ResponseEntity;
11
import org.springframework.stereotype.Controller;
13
import org.springframework.stereotype.Controller;
12
import org.springframework.transaction.annotation.Transactional;
14
import org.springframework.transaction.annotation.Transactional;
-
 
15
import org.springframework.web.bind.annotation.RequestBody;
13
import org.springframework.web.bind.annotation.RequestMapping;
16
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
17
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RequestParam;
16
 
19
 
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
Line 19... Line 22...
19
import com.spice.profitmandi.common.model.CustomCustomer;
22
import com.spice.profitmandi.common.model.CustomCustomer;
20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21
import com.spice.profitmandi.common.web.util.ResponseSender;
24
import com.spice.profitmandi.common.web.util.ResponseSender;
22
import com.spice.profitmandi.dao.entity.fofo.Customer;
25
import com.spice.profitmandi.dao.entity.fofo.Customer;
23
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
26
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
-
 
27
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
24
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
28
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
25
 
29
 
26
@Controller
30
@Controller
27
@Transactional
31
@Transactional
28
public class CustomerController {
32
public class CustomerController {
Line 33... Line 37...
33
	private ResponseSender<?> responseSender;
37
	private ResponseSender<?> responseSender;
34
 
38
 
35
	@Autowired
39
	@Autowired
36
	private CustomerRepository customerRepository;
40
	private CustomerRepository customerRepository;
37
 
41
 
-
 
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
 
38
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
54
	@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
39
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
55
	public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
40
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
56
			@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
41
			throws ProfitMandiBusinessException {
57
			throws ProfitMandiBusinessException {
-
 
58
		CustomCustomer customCustomer = null;
42
		LOGGER.info("Request Received at url {}", request.getRequestURI());
59
		LOGGER.info("Request Received at url {}", request.getRequestURI());
-
 
60
		try {
43
		List<Customer> customers = customerRepository.selectAllByMobileNumber(mobileNumber);
61
			Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
44
		if (customers.size() > 0) {
-
 
45
			Customer customer = customers.get(0);
62
			customCustomer = new CustomCustomer();
46
			CustomCustomer customCustomer = new CustomCustomer();
63
			customCustomer.setCustomerId(customer.getId());
47
			customCustomer.setEmailId(customer.getEmailId());
64
			customCustomer.setEmailId(customer.getEmailId());
48
			customCustomer.setFirstName(customer.getFirstName());
65
			customCustomer.setFirstName(customer.getFirstName());
49
			customCustomer.setLastName(customer.getLastName());
66
			customCustomer.setLastName(customer.getLastName());
50
			customCustomer.setMobileNumber(customer.getMobileNumber());
67
			customCustomer.setMobileNumber(customer.getMobileNumber());
-
 
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);
51
			if (!customer.getCustomerAddress().isEmpty()) {
72
			if (!customer.getCustomerAddress().isEmpty()) {
52
				CustomerAddress customerAddress = customer.getCustomerAddress().get(0);
-
 
53
				CustomAddress customAddress = new CustomAddress();
73
				List<CustomAddress> customAddresses = new ArrayList<>();
54
				customAddress.setCity(customerAddress.getCity());
-
 
55
				customAddress.setCountry(customerAddress.getCountry());
-
 
56
				customAddress.setLandmark(customerAddress.getLandmark());
-
 
57
				customAddress.setLine1(customerAddress.getLine1());
74
				for (CustomerAddress customerAddress : customer.getCustomerAddress()) {
58
				customAddress.setLine2(customerAddress.getLine2());
75
					customAddresses.add(this.toCustomAddress(customerAddress));
59
				customAddress.setName(customerAddress.getName());
-
 
60
				customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
-
 
61
				customAddress.setPinCode(customerAddress.getPinCode());
-
 
62
				customAddress.setState(customerAddress.getState());
-
 
63
				customAddress.setId(customerAddress.getId());
76
				}
64
				customCustomer.setAddress(customAddress);
77
				customCustomer.setAddresses(customAddresses);
65
			}
78
			}
66
			return responseSender.ok(customCustomer);
-
 
67
		} else {
79
		} catch (Exception e) {
68
			return responseSender.ok("");
-
 
69
		}
80
		}
-
 
81
		return responseSender.ok(customCustomer);
-
 
82
	}
-
 
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;
70
	}
115
	}
71
}
116
}