Rev 25170 | Rev 26807 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.time.temporal.ChronoField;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.CustomAddress;import com.spice.profitmandi.common.model.CustomCustomer;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.fofo.Customer;import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;@Controller@Transactionalpublic class CustomerController {private static final Logger LOGGER = LogManager.getLogger(CustomerController.class);@Autowiredprivate ResponseSender<?> responseSender;@Autowiredprivate CustomerRepository customerRepository;@Autowiredprivate CustomerAddressRepository customerAddressRepository;@RequestMapping(value = "/customer/address", method = RequestMethod.POST)public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,@RequestBody CustomAddress customAddress) {CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);customerAddressRepository.persist(customerAddress);return responseSender.ok(this.toCustomAddress(customerAddress));}@RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)throws ProfitMandiBusinessException {CustomCustomer customCustomer = null;LOGGER.info("Request Received at url {}", request.getRequestURI());try {Customer customer = customerRepository.selectByMobileNumber(mobileNumber);customCustomer = new CustomCustomer();customCustomer.setCustomerId(customer.getId());customCustomer.setEmailId(customer.getEmailId());customCustomer.setFirstName(customer.getFirstName());customCustomer.setLastName(customer.getLastName());customCustomer.setMobileNumber(customer.getMobileNumber());customer.getCustomerAddress().stream().sorted((CustomerAddress c1, CustomerAddress c2) -> {return c1.getCreateTimestamp().get(ChronoField.INSTANT_SECONDS)- c2.getCreateTimestamp().get(ChronoField.INSTANT_SECONDS);}).limit(5);if (!customer.getCustomerAddress().isEmpty()) {List<CustomAddress> customAddresses = new ArrayList<>();for (CustomerAddress customerAddress : customer.getCustomerAddress()) {customAddresses.add(this.toCustomAddress(customerAddress));}customCustomer.setAddresses(customAddresses);}} catch (Exception e) {}return responseSender.ok(customCustomer);}private CustomAddress toCustomAddress(CustomerAddress customerAddress) {CustomAddress customAddress = new CustomAddress();customAddress.setCity(customerAddress.getCity());customAddress.setCountry(customerAddress.getCountry());customAddress.setLandmark(customerAddress.getLandmark());customAddress.setLine1(customerAddress.getLine1());customAddress.setLine2(customerAddress.getLine2());customAddress.setName(customerAddress.getName());customAddress.setLastName(customerAddress.getLastName());customAddress.setPhoneNumber(customerAddress.getPhoneNumber());customAddress.setPinCode(customerAddress.getPinCode());customAddress.setState(customerAddress.getState());customAddress.setId(customerAddress.getId());return customAddress;}private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {CustomerAddress customerAddress = new CustomerAddress();customerAddress.setCustomerId(customerId);customerAddress.setName(customAddress.getName());customerAddress.setLastName(customAddress.getLastName());customerAddress.setLine1(customAddress.getLine1());customerAddress.setLine2(customAddress.getLine2());customerAddress.setLandmark(customAddress.getLandmark());customerAddress.setCity(customAddress.getCity());customerAddress.setPinCode(customAddress.getPinCode());customerAddress.setState(customAddress.getState());customerAddress.setCountry(customAddress.getCountry());customerAddress.setPhoneNumber(customAddress.getPhoneNumber());return customerAddress;}}