Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1981 varun.gupt 1
package in.shop2020.serving.controllers;
2
 
3
import org.apache.log4j.Logger;
4
import org.apache.struts2.convention.annotation.Result;
5
import org.apache.struts2.convention.annotation.Results;
6
 
2752 varun.gupt 7
import in.shop2020.utils.DataLogger;
8
import in.shop2020.datalogger.EventType;
1981 varun.gupt 9
import in.shop2020.model.v1.user.PromotionException;
10
import in.shop2020.model.v1.user.UserContextService;
11
import in.shop2020.model.v1.user.PromotionService;
12
import in.shop2020.serving.controllers.BaseController;
3126 rajveer 13
import in.shop2020.thrift.clients.UserClient;
14
import in.shop2020.thrift.clients.PromotionClient;
1981 varun.gupt 15
 
2145 chandransh 16
@SuppressWarnings("serial")
1981 varun.gupt 17
@Results({
18
    @Result(name = "redirect", type = "redirectAction", params = {"actionName" , "cart"})
19
})
20
public class PromotionController extends BaseController {
21
 
22
    public PromotionController() {
23
        super();
24
    }
25
    private static Logger log = Logger.getLogger(Class.class);
26
 
27
    public String create()  {
28
        String action = request.getParameter("action");
29
        log.info("Action is: " + action);
30
 
3126 rajveer 31
        UserClient userServiceClient = null;
32
        PromotionClient promotionServiceClient = null;
1981 varun.gupt 33
 
34
        try {
35
            long cartId = userinfo.getCartId();
36
 
37
            if(action == null || action.isEmpty())  {
38
                addActionError("Invalid Request Action");
39
                return "redirect";
40
            }
2752 varun.gupt 41
            String couponCode = request.getParameter("coupon_code");
42
 
1981 varun.gupt 43
            if (action.equals("applycoupon"))   {
44
                if (couponCode == null || couponCode.isEmpty()) {
45
                    addActionError("Coupon Code field cannot be left empty");
46
                    return "redirect";
47
                }
3126 rajveer 48
                promotionServiceClient = new PromotionClient();
1981 varun.gupt 49
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
2145 chandransh 50
                promotionClient.applyCoupon(couponCode, cartId);
2752 varun.gupt 51
                DataLogger.logData(EventType.COUPON_APPLIED, session.getId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
1981 varun.gupt 52
            }
53
            else if (action.equals("removecoupon"))    {
3126 rajveer 54
                userServiceClient = new UserClient();
1981 varun.gupt 55
                UserContextService.Client userClient = userServiceClient.getClient();
56
                userClient.removeCoupon(cartId);
2752 varun.gupt 57
                DataLogger.logData(EventType.COUPON_REMOVED, session.getId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
1981 varun.gupt 58
            }
59
        } catch (PromotionException e) {
2949 chandransh 60
            log.error("Unable to apply or remove coupon", e);
1981 varun.gupt 61
            addActionError(e.getMessage());
62
        } catch (Exception e) {
2949 chandransh 63
            log.error("Unable to apply or remove coupon", e);
1981 varun.gupt 64
        }
65
        return "redirect";
66
    }
67
}