Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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;
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;
18
import org.springframework.ui.Model;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMethod;
21
import org.springframework.web.bind.annotation.RequestParam;
22
 
23
import javax.servlet.http.HttpServletRequest;
24
import java.time.LocalDate;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.Set;
29
import java.util.stream.Collectors;
30
 
31
@Controller
32
public class HotDealController {
33
 
34
    private static final Logger LOGGER = LoggerFactory.getLogger(HotDealController.class);
35
    private static final int MOBILE_BRAND_CATEGORY_ID = 3;
36
 
37
    @Autowired
38
    private ModelHotDealService modelHotDealService;
39
 
40
    @Autowired
41
    private BrandsService brandsService;
42
 
43
    @Autowired
44
    private CatalogRepository catalogRepository;
45
 
46
    @Autowired
47
    private CookiesProcessor cookiesProcessor;
48
 
49
    @Autowired
50
    private MVCResponseSender mvcResponseSender;
51
 
52
    @Autowired
53
    private ObjectMapper objectMapper;
54
 
55
    @RequestMapping(value = "/manageHotDeals", method = RequestMethod.GET)
56
    public String manageHotDeals(HttpServletRequest request, Model model) throws Exception {
57
        model.addAttribute("brands", brandsService.getBrandsToDisplay(MOBILE_BRAND_CATEGORY_ID));
58
        model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
59
        return "hot-deals";
60
    }
61
 
62
    @RequestMapping(value = "/hotDeals", method = RequestMethod.GET)
63
    public String getHotDeals(@RequestParam int brandId, Model model) throws Exception {
64
        model.addAttribute("deals", modelHotDealService.getDealsForBrand(brandId));
65
        model.addAttribute("activeCount", modelHotDealService.countActive(brandId));
66
        model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
67
        return "hot-deals-table";
68
    }
69
 
70
    @RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
71
    public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {
72
        List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId,
73
                ProfitMandiConstants.MOBILE_CATEGORY_ID);
74
        Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream()
75
                .filter(deal -> !"EXPIRED".equals(deal.getStatus()))
76
                .map(ModelHotDeal::getCatalogItemId)
77
                .collect(Collectors.toSet());
78
        List<Map<String, Object>> models = catalogs.stream()
79
                .filter(catalog -> !activeIds.contains(catalog.getId()))
80
                .map(catalog -> {
81
                    Map<String, Object> entry = new HashMap<>();
82
                    entry.put("catalogId", catalog.getId());
83
                    entry.put("description", catalog.getDescription());
84
                    return entry;
85
                })
86
                .collect(Collectors.toList());
87
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
88
        return "response";
89
    }
90
 
91
    @RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)
92
    public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,
93
                             @RequestParam int catalogItemId, @RequestParam String startDate,
94
                             @RequestParam String endDate, Model model) throws Exception {
95
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
96
        try {
97
            modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),
98
                    LocalDate.parse(endDate), loginDetails.getEmailId());
99
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
100
        } catch (ProfitMandiBusinessException e) {
101
            LOGGER.warn("Hot deal add rejected: {}", e.getMessage());
102
            model.addAttribute("response1", e.getMessage());
103
        }
104
        return "response";
105
    }
106
 
107
    @RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)
108
    public String updateHotDeal(HttpServletRequest request, @RequestParam int id,
109
                                @RequestParam String startDate, @RequestParam String endDate,
110
                                Model model) throws Exception {
111
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
112
        try {
113
            modelHotDealService.updateDates(id, LocalDate.parse(startDate), LocalDate.parse(endDate),
114
                    loginDetails.getEmailId());
115
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
116
        } catch (ProfitMandiBusinessException e) {
117
            LOGGER.warn("Hot deal update rejected: {}", e.getMessage());
118
            model.addAttribute("response1", e.getMessage());
119
        }
120
        return "response";
121
    }
122
 
123
    @RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
124
    public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
125
        try {
126
            modelHotDealService.removeDeal(id);
127
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
128
        } catch (ProfitMandiBusinessException e) {
129
            LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
130
            model.addAttribute("response1", e.getMessage());
131
        }
132
        return "response";
133
    }
134
}