Ejemplo n.º 1
0
 internal int ApplyPromoCode(PromoCodeReq objReq)
 {
     try
     {
         using (var db = new WizzDataContext())
         {
             tblPromoCode promoCodeData = new tblPromoCode();
             long fkUserId = Convert.ToInt64(objReq.userId);
             promoCodeData = db.tblPromoCodes.Where(p => p.promoCode == objReq.promocode && p.isDelete == false).FirstOrDefault();
             if (promoCodeData == null)
                 return 0;
             else if (DateTime.UtcNow.IsBetween(promoCodeData.validFrom.Value, promoCodeData.validTo.Value) || promoCodeData.isActive == false)
                 return 3;
             else if (db.tblPromoHistories.Any(d => d.fkUserId == fkUserId && d.fkPromoCodeId == promoCodeData.pkPromoCodeId))
                 return 2;
             else if (db.tblPromoHistories.Count(x => x.fkPromoCodeId == promoCodeData.pkPromoCodeId) > promoCodeData.usageCount)
                 return 3;
             else
             {
                 tblPromoHistory objHistory = new tblPromoHistory();
                 objHistory.fkPromoCodeId = promoCodeData.pkPromoCodeId;
                 objHistory.createdDate = DateTime.UtcNow;
                 objHistory.fkUserId = Convert.ToInt64(objReq.userId);
                 db.tblPromoHistories.InsertOnSubmit(objHistory);
                 //  tblUser userDetails = new tblUser();
                 var userDetails = db.tblUsers.Where(u => u.pkUserId == fkUserId).FirstOrDefault();
                 userDetails.credits += Convert.ToInt32(promoCodeData.discountPercentage);
                 db.SubmitChanges();
                 return 1;
             }
         }
     }
     catch (Exception e)
     {
         return 0;
     }
 }
        public Response<string> ApplyPromoCode(PromoCodeReq objReq)
        {
            Response<string> response = new Response<string>();
            List<string> objResp = new List<string>();
            try
            {
                PromoCodeMethods objDBMethod = new PromoCodeMethods();

                if (CheckRequestIsvalidornot(this.Request))
                {
                    if (ModelState.IsValid)
                    {
                        switch (objDBMethod.ApplyPromoCode(objReq))
                        {
                            case 0:
                                response.Create(false, "Invalid Promo code", Messages.AppVersion, objResp);
                                break;
                            case 1:
                                response.Create(true, "Promo code applied successfully", Messages.AppVersion, objResp);
                                break;
                            case 2:
                                response.Create(false, "Sorry you have already used this promo code !", Messages.AppVersion, objResp);
                                break;
                            case 3:
                                response.Create(false, "Sorry this promo code is expired!", Messages.AppVersion, objResp);
                                break;
                            default:
                                break;
                        }

                    }
                    else
                        response.Create(false, ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage, Messages.AppVersion, objResp);
                }
                else
                    response.Create(false, Messages.FormatMessage(Messages.InvalidReq), Messages.AppVersion, objResp);

            }
            catch (Exception ex)
            {
                object session = new JavaScriptSerializer().Serialize(objReq);
                LogManager.Error("Error occured while Processing Webservice request :{0}", ex, session, ex.Message);
                response.Create(false, Messages.FormatMessage(Messages.ErrorOccure), Messages.AppVersion, objResp);
            }
            finally
            {
            }
            return response;
        }