| 37212 |
amit |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
4 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
5 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.catalog.Catalog;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.catalog.ModelHotDeal;
|
|
|
8 |
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
|
|
|
9 |
import com.spice.profitmandi.service.catalog.BrandsService;
|
| 37246 |
amit |
10 |
import com.spice.profitmandi.service.catalog.HotDealAttributes;
|
| 37212 |
amit |
11 |
import com.spice.profitmandi.service.catalog.ModelHotDealService;
|
|
|
12 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
13 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
14 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
15 |
import org.slf4j.Logger;
|
|
|
16 |
import org.slf4j.LoggerFactory;
|
|
|
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18 |
import org.springframework.stereotype.Controller;
|
| 37229 |
amit |
19 |
import org.springframework.transaction.annotation.Transactional;
|
| 37212 |
amit |
20 |
import org.springframework.ui.Model;
|
|
|
21 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
22 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
24 |
|
|
|
25 |
import javax.servlet.http.HttpServletRequest;
|
|
|
26 |
import java.time.LocalDate;
|
|
|
27 |
import java.util.HashMap;
|
|
|
28 |
import java.util.List;
|
|
|
29 |
import java.util.Map;
|
|
|
30 |
import java.util.Set;
|
|
|
31 |
import java.util.stream.Collectors;
|
|
|
32 |
|
|
|
33 |
@Controller
|
| 37229 |
amit |
34 |
@Transactional(rollbackFor = Throwable.class)
|
| 37212 |
amit |
35 |
public class HotDealController {
|
|
|
36 |
|
|
|
37 |
private static final Logger LOGGER = LoggerFactory.getLogger(HotDealController.class);
|
|
|
38 |
private static final int MOBILE_BRAND_CATEGORY_ID = 3;
|
|
|
39 |
|
|
|
40 |
@Autowired
|
|
|
41 |
private ModelHotDealService modelHotDealService;
|
|
|
42 |
|
|
|
43 |
@Autowired
|
|
|
44 |
private BrandsService brandsService;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
|
|
47 |
private CatalogRepository catalogRepository;
|
|
|
48 |
|
|
|
49 |
@Autowired
|
|
|
50 |
private CookiesProcessor cookiesProcessor;
|
|
|
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
private MVCResponseSender mvcResponseSender;
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
private ObjectMapper objectMapper;
|
|
|
57 |
|
|
|
58 |
@RequestMapping(value = "/manageHotDeals", method = RequestMethod.GET)
|
|
|
59 |
public String manageHotDeals(HttpServletRequest request, Model model) throws Exception {
|
|
|
60 |
model.addAttribute("brands", brandsService.getBrandsToDisplay(MOBILE_BRAND_CATEGORY_ID));
|
|
|
61 |
model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
|
|
|
62 |
return "hot-deals";
|
|
|
63 |
}
|
|
|
64 |
|
| 37256 |
amit |
65 |
/** All deals across brands: server-side searched + paginated table fragment. */
|
|
|
66 |
@RequestMapping(value = "/hotDeals/all", method = RequestMethod.GET)
|
|
|
67 |
public String getAllHotDeals(@RequestParam(defaultValue = "1") int page,
|
|
|
68 |
@RequestParam(required = false, defaultValue = "") String q,
|
|
|
69 |
Model model) throws Exception {
|
|
|
70 |
int pageSize = 20;
|
|
|
71 |
long total = modelHotDealService.countDeals(q);
|
|
|
72 |
int pages = (int) Math.max(1, (total + pageSize - 1) / pageSize);
|
|
|
73 |
page = Math.min(Math.max(1, page), pages);
|
|
|
74 |
model.addAttribute("deals", modelHotDealService.searchDeals(q, page, pageSize));
|
|
|
75 |
model.addAttribute("total", total);
|
|
|
76 |
model.addAttribute("page", page);
|
|
|
77 |
model.addAttribute("pages", pages);
|
| 37212 |
amit |
78 |
return "hot-deals-table";
|
|
|
79 |
}
|
|
|
80 |
|
| 37256 |
amit |
81 |
/** Active-slot usage for a brand, e.g. "3 / 15". */
|
|
|
82 |
@RequestMapping(value = "/hotDeals/slots", method = RequestMethod.GET)
|
|
|
83 |
public String getSlots(@RequestParam int brandId, Model model) throws Exception {
|
|
|
84 |
model.addAttribute("response1", modelHotDealService.countActive(brandId)
|
|
|
85 |
+ " / " + ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
|
|
|
86 |
return "response";
|
|
|
87 |
}
|
|
|
88 |
|
| 37212 |
amit |
89 |
@RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
|
|
|
90 |
public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {
|
|
|
91 |
List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId,
|
|
|
92 |
ProfitMandiConstants.MOBILE_CATEGORY_ID);
|
|
|
93 |
Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream()
|
|
|
94 |
.filter(deal -> !"EXPIRED".equals(deal.getStatus()))
|
|
|
95 |
.map(ModelHotDeal::getCatalogItemId)
|
|
|
96 |
.collect(Collectors.toSet());
|
|
|
97 |
List<Map<String, Object>> models = catalogs.stream()
|
|
|
98 |
.filter(catalog -> !activeIds.contains(catalog.getId()))
|
|
|
99 |
.map(catalog -> {
|
|
|
100 |
Map<String, Object> entry = new HashMap<>();
|
|
|
101 |
entry.put("catalogId", catalog.getId());
|
|
|
102 |
entry.put("description", catalog.getDescription());
|
|
|
103 |
return entry;
|
|
|
104 |
})
|
|
|
105 |
.collect(Collectors.toList());
|
|
|
106 |
model.addAttribute("response1", objectMapper.writeValueAsString(models));
|
|
|
107 |
return "response";
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
@RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)
|
|
|
111 |
public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,
|
|
|
112 |
@RequestParam int catalogItemId, @RequestParam String startDate,
|
| 37246 |
amit |
113 |
@RequestParam String endDate,
|
|
|
114 |
@RequestParam(required = false) Integer warrantyMonths,
|
|
|
115 |
@RequestParam(required = false) String condition,
|
|
|
116 |
@RequestParam(required = false) Boolean activated,
|
|
|
117 |
@RequestParam(required = false) Boolean financeMapping,
|
|
|
118 |
@RequestParam(required = false) Boolean affordability,
|
|
|
119 |
Model model) throws Exception {
|
|
|
120 |
// required=false so a missing field reaches the service's mandatory-field
|
|
|
121 |
// validation and comes back as a readable message instead of a Spring 400
|
| 37212 |
amit |
122 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
123 |
try {
|
| 37246 |
amit |
124 |
HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
|
|
|
125 |
activated, financeMapping, affordability);
|
| 37212 |
amit |
126 |
modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),
|
| 37246 |
amit |
127 |
LocalDate.parse(endDate), attributes, loginDetails.getEmailId());
|
| 37212 |
amit |
128 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
129 |
} catch (ProfitMandiBusinessException e) {
|
|
|
130 |
LOGGER.warn("Hot deal add rejected: {}", e.getMessage());
|
|
|
131 |
model.addAttribute("response1", e.getMessage());
|
|
|
132 |
}
|
|
|
133 |
return "response";
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
@RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)
|
|
|
137 |
public String updateHotDeal(HttpServletRequest request, @RequestParam int id,
|
|
|
138 |
@RequestParam String startDate, @RequestParam String endDate,
|
| 37246 |
amit |
139 |
@RequestParam(required = false) Integer warrantyMonths,
|
|
|
140 |
@RequestParam(required = false) String condition,
|
|
|
141 |
@RequestParam(required = false) Boolean activated,
|
|
|
142 |
@RequestParam(required = false) Boolean financeMapping,
|
|
|
143 |
@RequestParam(required = false) Boolean affordability,
|
| 37212 |
amit |
144 |
Model model) throws Exception {
|
|
|
145 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
146 |
try {
|
| 37246 |
amit |
147 |
HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
|
|
|
148 |
activated, financeMapping, affordability);
|
|
|
149 |
modelHotDealService.updateDeal(id, LocalDate.parse(startDate), LocalDate.parse(endDate),
|
|
|
150 |
attributes, loginDetails.getEmailId());
|
| 37212 |
amit |
151 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
152 |
} catch (ProfitMandiBusinessException e) {
|
|
|
153 |
LOGGER.warn("Hot deal update rejected: {}", e.getMessage());
|
|
|
154 |
model.addAttribute("response1", e.getMessage());
|
|
|
155 |
}
|
|
|
156 |
return "response";
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
@RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
|
|
|
160 |
public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
|
|
|
161 |
try {
|
|
|
162 |
modelHotDealService.removeDeal(id);
|
|
|
163 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
164 |
} catch (ProfitMandiBusinessException e) {
|
|
|
165 |
LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
|
|
|
166 |
model.addAttribute("response1", e.getMessage());
|
|
|
167 |
}
|
|
|
168 |
return "response";
|
|
|
169 |
}
|
|
|
170 |
}
|