Subversion Repositories SmartDukaan

Rev

Rev 28371 | Rev 30596 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
28371 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
30017 amit.gupta 3
import com.spice.profitmandi.common.solr.SolrService;
4
import com.spice.profitmandi.common.web.util.ResponseSender;
5
import com.spice.profitmandi.dao.entity.dtr.WebOffer;
6
import com.spice.profitmandi.dao.entity.dtr.WebOfferProduct;
7
import com.spice.profitmandi.dao.repository.dtr.WebOfferProductRepository;
8
import com.spice.profitmandi.dao.repository.dtr.WebOfferRepository;
28371 amit.gupta 9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
11
import org.json.JSONObject;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.ui.Model;
30017 amit.gupta 15
import org.springframework.web.bind.annotation.*;
28371 amit.gupta 16
 
30017 amit.gupta 17
import javax.servlet.http.HttpServletRequest;
18
import javax.transaction.Transactional;
19
import java.time.LocalDateTime;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Optional;
23
import java.util.stream.Collectors;
28371 amit.gupta 24
 
25
@Controller
26
@Transactional(rollbackOn = Throwable.class)
27
public class WebOffersController {
28
 
29
	private static final Logger LOGGER = LogManager.getLogger(WebOffersController.class);
30
 
31
 
32
	@Autowired
33
	ResponseSender<?> responseSender;
34
 
35
	@Autowired
36
	WebOfferRepository webOfferRepository;
37
 
38
	@Autowired
39
	WebOfferProductRepository webOfferProductRepository;
40
 
41
	@Autowired
42
	SolrService solrService;
43
 
44
	@RequestMapping(value = "/web-offers", method = RequestMethod.GET)
45
	public String getOfferListing(HttpServletRequest request, Model model) {
46
		List<WebOffer> webListing = webOfferRepository.selectAllWebListing(Optional.of(true));
47
		model.addAttribute("webListings", webListing);
48
		return "web-offer";
49
	}
50
 
51
	@RequestMapping(value = "/web-offer/add", method = RequestMethod.POST)
52
	public String addWebListing(HttpServletRequest request, @RequestBody WebOffer webOffer, Model model) {
53
		webOffer.setCreatedDate(LocalDateTime.now());
54
		webOffer.setDetailedText("");
55
		webOfferRepository.persist(webOffer);
56
		return getOfferListing(request, model);
57
	}
58
 
59
	@RequestMapping(value = "/web-offer/{webOfferId}", method = RequestMethod.GET)
60
	public String getWebProductListing(HttpServletRequest request, Model model, @PathVariable int webOfferId)
61
			throws Exception {
62
		List<WebOfferProduct> webOfferProducts = webOfferProductRepository.selectAllByWebOfferId(webOfferId);
63
		if (webOfferProducts.size() > 0) {
64
			Map<Integer, JSONObject> entityMap = solrService.getContentByCatalogIds(
65
					webOfferProducts.stream().map(x -> x.getEntityId()).collect(Collectors.toList()));
66
			webOfferProducts.stream()
67
					.forEach(x -> x.setProductName(entityMap.get(x.getEntityId()).getString("title_s")));
68
		}
69
 
70
		WebOffer webOffer = webOfferRepository.selectById(webOfferId);
71
		model.addAttribute("productListings", webOfferProducts);
72
		model.addAttribute("webListing", webOffer);
73
		return "web-offer-product";
74
	}
75
 
76
	@RequestMapping(value = "/web-offer-product/add", method = RequestMethod.POST)
77
	public String addWebOfferProduct(HttpServletRequest request,
78
			@RequestBody List<WebOfferProduct> webOfferProducts, Model model) throws Exception {
79
		webOfferProducts.stream().forEach(webOfferProduct -> {
80
			boolean exists = webOfferProductRepository.isExists(webOfferProduct.getWebOfferId(),
81
					webOfferProduct.getEntityId());
82
			if (!exists) {
83
				webOfferProductRepository.persist(webOfferProduct);
84
			}
85
 
86
		});
87
		return getWebProductListing(request, model, webOfferProducts.get(0).getWebOfferId());
88
	}
89
 
90
	@RequestMapping(value = "/web-offer-product/remove", method = RequestMethod.DELETE)
91
	public String addWebOfferProduct(HttpServletRequest request,
92
			@RequestParam int webOfferProductId, Model model) throws Exception {
93
		WebOfferProduct webOfferProduct = webOfferProductRepository.selectById(webOfferProductId);
94
		webOfferProductRepository.delete(webOfferProduct);
95
		return getWebProductListing(request, model, webOfferProduct.getWebOfferId());
96
	}
97
 
98
 
99
/*	@RequestMapping(value = "/web-listing/order/{webListingId}", method = RequestMethod.POST)
100
	public String orderWebProductListing(HttpServletRequest request, Model model, @PathVariable int webListingId,
101
			@RequestBody List<Integer> webListingProductIds) throws Exception {
102
		List<WebProductListing> productListings = webProductListingRepository.selectAllByWebListingId(webListingId);
103
 
104
		productListings.forEach(productListing -> {
105
			productListing.setRank(webListingProductIds.indexOf(productListing.getId()) + 1);
106
		});
107
		return getWebProductListing(request, model, webListingId);
108
	}
109
 
110
	@RequestMapping(value = "/web-listing/order-listing", method = RequestMethod.POST)
111
	public String orderWebListing(HttpServletRequest request, Model model, @RequestBody List<Integer> webListingIds)
112
			throws Exception {
113
		LOGGER.info("webListingId" + webListingIds);
114
		List<WebListing> webListings = webListingRepository.selectByIds(webListingIds);
115
		webListings.forEach(webListing -> {
116
			webListing.setRank(webListingIds.indexOf(webListing.getId()) + 1);
117
		});
118
		return getWebListing(request, model);
119
	}*/
120
 
121
 
122
 
123
}