public HttpResponseMessage add(DiscountCode post) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (Language.MasterPostExists(post.language_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist")); } else if (Currency.MasterPostExists(post.currency_code) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The currency does not exist")); } // Make sure that the data is valid post.id = AnnytabDataValidation.TruncateString(post.id, 50); post.discount_value = AnnytabDataValidation.TruncateDecimal(post.discount_value, 0, 9.999M); post.end_date = AnnytabDataValidation.TruncateDateTime(post.end_date); post.minimum_order_value = AnnytabDataValidation.TruncateDecimal(post.minimum_order_value, 0, 999999999999.99M); // Check if the discount code exists if (DiscountCode.MasterPostExists(post.id) == true) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The id already exists")); } // Add the post DiscountCode.Add(post); // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The post has been added")); } // End of the add method
public ActionResult edit(FormCollection collection) { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get the return url string returnUrl = collection["returnUrl"]; ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true) { ViewBag.AdminSession = true; } else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true) { ViewBag.AdminSession = true; ViewBag.AdminErrorCode = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return View("index"); } else { // Redirect the user to the start page return RedirectToAction("index", "admin_login"); } // Get all the form values string id = collection["txtId"]; Int32 language_id = Convert.ToInt32(collection["selectLanguage"]); string currency_code = collection["selectCurrency"]; decimal discount_value = 0; decimal.TryParse(collection["txtDiscountValue"].Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out discount_value); bool free_freight = Convert.ToBoolean(collection["cbFreeFreight"]); bool once_per_customer = Convert.ToBoolean(collection["cbOncePerCustomer"]); bool exclude_products_on_sale = Convert.ToBoolean(collection["cbExcludeProductsOnSale"]); DateTime end_date = Convert.ToDateTime(collection["txtEndDate"]); decimal minimum_order_value = 0; decimal.TryParse(collection["txtMinimumOrderValue"].Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out minimum_order_value); // Get translated texts KeyStringList tt = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); // Get the discount code DiscountCode discountCode = DiscountCode.GetOneById(id); bool postExists = true; // Check if the discount code exists if (discountCode == null) { // Create an empty discount code discountCode = new DiscountCode(); postExists = false; } // Update values discountCode.id = id; discountCode.language_id = language_id; discountCode.currency_code = currency_code; discountCode.discount_value = discount_value; discountCode.free_freight = free_freight; discountCode.once_per_customer = once_per_customer; discountCode.exclude_products_on_sale = exclude_products_on_sale; discountCode.end_date = AnnytabDataValidation.TruncateDateTime(end_date); discountCode.minimum_order_value = minimum_order_value; // Create a error message string errorMessage = string.Empty; if (discountCode.id.Length == 0 || discountCode.id.Length > 50) { errorMessage += "• " + String.Format(tt.Get("error_field_certain_length"), tt.Get("id"), "1", "50") + "<br/>"; } if (discountCode.language_id == 0) { errorMessage += "• " + String.Format(tt.Get("error_select_value"), tt.Get("language").ToLower()) + "<br/>"; } if (discountCode.currency_code == "") { errorMessage += "• " + String.Format(tt.Get("error_select_value"), tt.Get("currency").ToLower()) + "<br/>"; } if (discountCode.discount_value < 0 || discountCode.discount_value > 9.999M) { errorMessage += "• " + String.Format(tt.Get("error_field_range"), tt.Get("discount"), "9.999") + "<br/>"; } if (discountCode.minimum_order_value < 0 || discountCode.minimum_order_value > 999999999999.99M) { errorMessage += "• " + String.Format(tt.Get("error_field_range"), tt.Get("minimum_order_value"), "999 999 999 999.99") + "<br/>"; } // Check if there is errors if (errorMessage == string.Empty) { // Check if we should add or update the discount code if (postExists == false) { // Add the discount code DiscountCode.Add(discountCode); } else { // Update the discount code DiscountCode.Update(discountCode); } // Redirect the user to the list return Redirect("/admin_discount_codes" + returnUrl); } else { // Set form values ViewBag.ErrorMessage = errorMessage; ViewBag.TranslatedTexts = tt; ViewBag.Languages = Language.GetAll(currentDomain.back_end_language, "id", "ASC"); ViewBag.DiscountCode = discountCode; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("edit"); } } // End of the edit method