Subversion Repositories SmartDukaan

Rev

Rev 37212 | 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;
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
 
64
    @RequestMapping(value = "/hotDeals", method = RequestMethod.GET)
65
    public String getHotDeals(@RequestParam int brandId, Model model) throws Exception {
66
        model.addAttribute("deals", modelHotDealService.getDealsForBrand(brandId));
67
        model.addAttribute("activeCount", modelHotDealService.countActive(brandId));
68
        model.addAttribute("maxDeals", ModelHotDealService.MAX_ACTIVE_DEALS_PER_BRAND);
69
        return "hot-deals-table";
70
    }
71
 
72
    @RequestMapping(value = "/hotDeals/models", method = RequestMethod.GET)
73
    public String getModelsForBrand(@RequestParam int brandId, Model model) throws Exception {
74
        List<Catalog> catalogs = catalogRepository.selectAllByBrandId(brandId,
75
                ProfitMandiConstants.MOBILE_CATEGORY_ID);
76
        Set<Integer> activeIds = modelHotDealService.getDealsForBrand(brandId).stream()
77
                .filter(deal -> !"EXPIRED".equals(deal.getStatus()))
78
                .map(ModelHotDeal::getCatalogItemId)
79
                .collect(Collectors.toSet());
80
        List<Map<String, Object>> models = catalogs.stream()
81
                .filter(catalog -> !activeIds.contains(catalog.getId()))
82
                .map(catalog -> {
83
                    Map<String, Object> entry = new HashMap<>();
84
                    entry.put("catalogId", catalog.getId());
85
                    entry.put("description", catalog.getDescription());
86
                    return entry;
87
                })
88
                .collect(Collectors.toList());
89
        model.addAttribute("response1", objectMapper.writeValueAsString(models));
90
        return "response";
91
    }
92
 
93
    @RequestMapping(value = "/hotDeals/add", method = RequestMethod.POST)
94
    public String addHotDeal(HttpServletRequest request, @RequestParam int brandId,
95
                             @RequestParam int catalogItemId, @RequestParam String startDate,
96
                             @RequestParam String endDate, Model model) throws Exception {
97
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
98
        try {
99
            modelHotDealService.addDeal(brandId, catalogItemId, LocalDate.parse(startDate),
100
                    LocalDate.parse(endDate), loginDetails.getEmailId());
101
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
102
        } catch (ProfitMandiBusinessException e) {
103
            LOGGER.warn("Hot deal add rejected: {}", e.getMessage());
104
            model.addAttribute("response1", e.getMessage());
105
        }
106
        return "response";
107
    }
108
 
109
    @RequestMapping(value = "/hotDeals/update", method = RequestMethod.POST)
110
    public String updateHotDeal(HttpServletRequest request, @RequestParam int id,
111
                                @RequestParam String startDate, @RequestParam String endDate,
112
                                Model model) throws Exception {
113
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
114
        try {
115
            modelHotDealService.updateDates(id, LocalDate.parse(startDate), LocalDate.parse(endDate),
116
                    loginDetails.getEmailId());
117
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
118
        } catch (ProfitMandiBusinessException e) {
119
            LOGGER.warn("Hot deal update rejected: {}", e.getMessage());
120
            model.addAttribute("response1", e.getMessage());
121
        }
122
        return "response";
123
    }
124
 
125
    @RequestMapping(value = "/hotDeals/remove", method = RequestMethod.POST)
126
    public String removeHotDeal(@RequestParam int id, Model model) throws Exception {
127
        try {
128
            modelHotDealService.removeDeal(id);
129
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
130
        } catch (ProfitMandiBusinessException e) {
131
            LOGGER.warn("Hot deal remove rejected: {}", e.getMessage());
132
            model.addAttribute("response1", e.getMessage());
133
        }
134
        return "response";
135
    }
136
}