public static void AddPromotion(EditPromotionDTO promotion) { try { var con = new SqlConnection(GetConnection()); var cmd = new SqlCommand { Connection = con, CommandType = CommandType.Text, CommandText = "INSERT INTO [dbo].[promotion](codeID,rate, price, hidden) " + "VALUES (@codeId,@rate, @price, @hidden)" }; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@codeId", promotion.CodeId); cmd.Parameters.AddWithValue("@rate", promotion.Rate); cmd.Parameters.AddWithValue("@price", promotion.Price); cmd.Parameters.AddWithValue("@hidden", promotion.Hidden); if (con.State != ConnectionState.Closed) return; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } catch (SqlException ex) { var msg = "Insert errors"; msg += ex.Message; throw new Exception(msg); } }
public ActionResult GetPromotionDetail(string promotionCode) { var promotion = PromoManagement.GetAllPromotionList().Single(x=>x.CodeId == promotionCode); var promotionDetail = new EditPromotionDTO { CodeId = promotion.CodeId, Price = promotion.Price, Rate = promotion.Rate, IsCreating = false, Hidden = promotion.Hidden }; return Json(new { promotionDetail }, JsonRequestBehavior.AllowGet); }
public ActionResult CreateUpdatePromotion(EditPromotionDTO dto) { if (dto == null) { throw new NullReferenceException("DTO cannot be null"); } var validationIds = new List<string>(); var validationMessage = Validate(dto, out validationIds); if (!validationMessage.Any()) { if (dto.IsCreating) { PromoManagement.AddPromotion(dto); } else { PromoManagement.UpdatePromotion(dto); } var promotions = PromoManagement.GetAllPromotionList(); var totalProductPage = (int)Math.Ceiling(((double)promotions.Count() / (double)pageSize)); return Json(new { promotions = ApplyPaging(promotions, 1), passed = !validationMessage.Any(), validationIds, validationMessage = string.Join("</br>", validationMessage), totalProductPage }); } return Json(new { passed = !validationMessage.Any(), validationIds, validationMessage = string.Join("</br>", validationMessage) }); }
public static void UpdatePromotion(EditPromotionDTO promotion) { try { var con = new SqlConnection(GetConnection()); var cmd = new SqlCommand { Connection = con, CommandType = CommandType.Text, CommandText = "Update [dbo].[promotion] SET rate = @rate, price = @price, hidden=@hidden WHERE codeId = @codeId" }; cmd.Parameters.AddWithValue("@rate", promotion.Rate); cmd.Parameters.AddWithValue("@price", promotion.Price); cmd.Parameters.AddWithValue("@hidden", promotion.Hidden); cmd.Parameters.AddWithValue("@codeId", promotion.CodeId); if (con.State != ConnectionState.Closed) return; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } catch (SqlException ex) { var msg = "Update errors"; msg += ex.Message; throw new Exception(msg); } }
public List<string> Validate(EditPromotionDTO promotion, out List<string> invalidIds) { var validationMessage = new List<string>(); var validationId = new List<string>(); if (promotion.IsCreating && string.IsNullOrEmpty(promotion.CodeId)) { validationMessage.Add("Code is empty."); validationId.Add("PromotionCodeId"); } if (promotion.Price == default(double) && promotion.Rate == default(int)) { validationMessage.Add("Price and Rate are both empty. At least one must be set."); validationId.Add("PromotionPrice"); validationId.Add("PromotionRate"); } if (promotion.Price != default(double) && promotion.Rate != default(int)) { validationMessage.Add("Price and Rate cannot be set at the same time."); validationId.Add("PromotionPrice"); validationId.Add("PromotionRate"); } invalidIds = validationId; return validationMessage; }