Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
import com.spice.profitmandi.dao.model.TodayOfferModel;
import com.spice.profitmandi.dao.model.UserCart;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.service.catalog.BrandsService;
import com.spice.profitmandi.service.offers.TodayOfferService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class TodayOfferController {

    private static final Logger logger = LogManager.getLogger(TodayOfferController.class);

    @Autowired
    private ResponseSender responseSender;

    @Autowired
    private BrandsService brandsService;

    @Autowired
    private TodayOfferService todayOfferService;

    @Autowired
    private UserAccountRepository userAccountRepository;

    @RequestMapping(value = "/todayOffers/brands", method = RequestMethod.GET)
    public ResponseEntity<?> getTodayOfferBrands(HttpServletRequest request) throws ProfitMandiBusinessException {
        List<String> brands = new ArrayList<>(Arrays.asList("Vivo","Oppo","Samsung","Xiaomi","Realme","Apple","Itel","Motorola","Poco","Tecno"));
        return responseSender.ok(brands);
    }

    @RequestMapping(value = "/todayOffers", method = RequestMethod.GET)
    public ResponseEntity<?> getTodayOffers(HttpServletRequest request, @RequestParam String brand) throws ProfitMandiBusinessException {
        int userId = (int) request.getAttribute("userId");
        UserCart uc = userAccountRepository.getUserCart(userId);
        int fofoId = uc.getUserId();
        logger.info("todayOffers fofoId: {}", fofoId);
        List<TodayOfferModel> todayOfferModels = todayOfferService.findAllTodayOffer(brand, fofoId);
        List<TodayOfferModel> groupedOffers = todayOfferService.groupSameOffers(todayOfferModels);
        logger.info("groupedOffers size: {}", groupedOffers.size());
        return responseSender.ok(groupedOffers);
    }
}