Subversion Repositories SmartDukaan

Rev

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

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