Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import in.shop2020.model.v1.user.PromotionException;
import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.model.v1.user.PromotionService;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.thrift.clients.PromotionServiceClient;

@SuppressWarnings("serial")
@Results({
    @Result(name = "redirect", type = "redirectAction", params = {"actionName" , "cart"})
})
public class PromotionController extends BaseController {

    public PromotionController() {
        super();
    }
    private static Logger log = Logger.getLogger(Class.class);
    
    public String create()  {
        String action = request.getParameter("action");
        log.info("Action is: " + action);
        
        UserContextServiceClient userServiceClient = null;
        PromotionServiceClient promotionServiceClient = null;
        
        try {
            long cartId = userinfo.getCartId();

            if(action == null || action.isEmpty())  {
                addActionError("Invalid Request Action");
                return "redirect";
            }
            if (action.equals("applycoupon"))   {
                String couponCode = request.getParameter("coupon_code");
                
                if (couponCode == null || couponCode.isEmpty()) {
                    addActionError("Coupon Code field cannot be left empty");
                    return "redirect";
                }
                promotionServiceClient = new PromotionServiceClient();
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
                promotionClient.applyCoupon(couponCode, cartId);
            }
            else if (action.equals("removecoupon"))    {
                userServiceClient = new UserContextServiceClient();
                UserContextService.Client userClient = userServiceClient.getClient();
                userClient.removeCoupon(cartId);
            }
        } catch (PromotionException e) {
            addActionError(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect";
    }
}