Subversion Repositories SmartDukaan

Rev

Rev 37229 | Rev 37256 | 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.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
 
65
    @RequestMapping(value = "/hotDeals", method = RequestMethod.GET)
66
    public String getHotDeals(@RequestParam int brandId, Model model) throws Exception {
67
        model.addAttribute("deals", modelHotDealService.getDealsForBrand(brandId));
68
        model.addAttribute("activeCount", modelHotDealService.countActive(brandId));
69
        model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
70
        return "hot-deals-table";
71
    }
72
 
73
    @RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
74
    public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {
75
        List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId,
76
                ProfitMandiConstants.MOBILE_CATEGORY_ID);
77
        Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream()
78
                .filter(deal -> !"EXPIRED".equals(deal.getStatus()))
79
                .map(ModelHotDeal::getCatalogItemId)
80
                .collect(Collectors.toSet());
81
        List<Map<String, Object>> models = catalogs.stream()
82
                .filter(catalog -> !activeIds.contains(catalog.getId()))
83
                .map(catalog -> {
84
                    Map<String, Object> entry = new HashMap<>();
85
                    entry.put("catalogId", catalog.getId());
86
                    entry.put("description", catalog.getDescription());
87
                    return entry;
88
                })
89
                .collect(Collectors.toList());
90
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
91
        return "response";
92
    }
93
 
94
    @RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)
95
    public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,
96
                             @RequestParam int catalogItemId, @RequestParam String startDate,
37246 amit 97
                             @RequestParam String endDate,
98
                             @RequestParam(required = false) Integer warrantyMonths,
99
                             @RequestParam(required = false) String condition,
100
                             @RequestParam(required = false) Boolean activated,
101
                             @RequestParam(required = false) Boolean financeMapping,
102
                             @RequestParam(required = false) Boolean affordability,
103
                             Model model) throws Exception {
104
        // required=false so a missing field reaches the service's mandatory-field
105
        // validation and comes back as a readable message instead of a Spring 400
37212 amit 106
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
107
        try {
37246 amit 108
            HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
109
                    activated, financeMapping, affordability);
37212 amit 110
            modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),
37246 amit 111
                    LocalDate.parse(endDate), attributes, loginDetails.getEmailId());
37212 amit 112
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
113
        } catch (ProfitMandiBusinessException e) {
114
            LOGGER.warn("Hot deal add rejected: {}", e.getMessage());
115
            model.addAttribute("response1", e.getMessage());
116
        }
117
        return "response";
118
    }
119
 
120
    @RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)
121
    public String updateHotDeal(HttpServletRequest request, @RequestParam int id,
122
                                @RequestParam String startDate, @RequestParam String endDate,
37246 amit 123
                                @RequestParam(required = false) Integer warrantyMonths,
124
                                @RequestParam(required = false) String condition,
125
                                @RequestParam(required = false) Boolean activated,
126
                                @RequestParam(required = false) Boolean financeMapping,
127
                                @RequestParam(required = false) Boolean affordability,
37212 amit 128
                                Model model) throws Exception {
129
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
130
        try {
37246 amit 131
            HotDealAttributes attributes = new HotDealAttributes(warrantyMonths, condition,
132
                    activated, financeMapping, affordability);
133
            modelHotDealService.updateDeal(id, LocalDate.parse(startDate), LocalDate.parse(endDate),
134
                    attributes, loginDetails.getEmailId());
37212 amit 135
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
136
        } catch (ProfitMandiBusinessException e) {
137
            LOGGER.warn("Hot deal update rejected: {}", e.getMessage());
138
            model.addAttribute("response1", e.getMessage());
139
        }
140
        return "response";
141
    }
142
 
143
    @RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
144
    public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
145
        try {
146
            modelHotDealService.removeDeal(id);
147
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
148
        } catch (ProfitMandiBusinessException e) {
149
            LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
150
            model.addAttribute("response1", e.getMessage());
151
        }
152
        return "response";
153
    }
154
}