public HttpResponseMessage add(OrderGiftCard post) { // Check for errors if (post == null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The post is null"); } else if (Order.MasterPostExists(post.order_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The order does not exist"); } else if (GiftCard.MasterPostExists(post.gift_card_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The gift card does not exist"); } // Make sure that the data is valid post.gift_card_id = AnnytabDataValidation.TruncateString(post.gift_card_id, 50); post.amount = AnnytabDataValidation.TruncateDecimal(post.amount, 0, 999999999999M); // Check if the order gift card exists if (OrderGiftCard.GetOneById(post.order_id, post.gift_card_id) != null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The post already exists"); } // Add the post OrderGiftCard.Add(post); // Return the success response return Request.CreateResponse<string>(HttpStatusCode.OK, "The post has been added"); } // End of the add method
public HttpResponseMessage update(OrderGiftCard post) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (Order.MasterPostExists(post.order_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The order does not exist")); } else if (GiftCard.MasterPostExists(post.gift_card_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The gift card does not exist")); } // Make sure that the data is valid post.gift_card_id = AnnytabDataValidation.TruncateString(post.gift_card_id, 50); post.amount = AnnytabDataValidation.TruncateDecimal(post.amount, 0, 999999999999M); // Get the saved post OrderGiftCard savedPost = OrderGiftCard.GetOneById(post.order_id, post.gift_card_id); // Check if the post exists if (savedPost == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The record does not exist")); } // Update the post OrderGiftCard.Update(post); // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The update was successful")); } // End of the update method
public List <OrderGiftCard> get_by_gift_card_id(string id = "", string sortField = "", string sortOrder = "") { // Create the list to return List <OrderGiftCard> posts = OrderGiftCard.GetByGiftCardId(id, sortField, sortOrder); // Return the list return(posts); } // End of the get_by_gift_card_id method
public List <OrderGiftCard> get_all() { // Create the list to return List <OrderGiftCard> posts = OrderGiftCard.GetAll(); // Return the list return(posts); } // End of the get_all method
public List <OrderGiftCard> get_by_order_id(Int32 id = 0, string sortField = "", string sortOrder = "") { // Create the list to return List <OrderGiftCard> posts = OrderGiftCard.GetByOrderId(id, sortField, sortOrder); // Return the list return(posts); } // End of the get_by_order_id method
public OrderGiftCard get_by_id(Int32 id = 0, string giftCardId = "") { // Create the post to return OrderGiftCard post = OrderGiftCard.GetOneById(id, giftCardId); // Return the post return(post); } // End of the get_by_id method
} // End of the Update method #endregion #region Get methods /// <summary> /// Get one order gift card on id /// </summary> /// <param name="orderId">An order id</param> /// <param name="giftCardId">A gift card id</param> /// <returns>A reference to a order gift card post</returns> public static OrderGiftCard GetOneById(Int32 orderId, string giftCardId) { // Create the post to return OrderGiftCard post = null; // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "SELECT * FROM dbo.orders_gift_cards WHERE order_id = @order_id AND gift_card_id = @gift_card_id;"; // The using block is used to call dispose automatically even if there are an exception. using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there are an exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@order_id", orderId); cmd.Parameters.AddWithValue("@gift_card_id", giftCardId); // Create a reader SqlDataReader reader = null; // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases. try { // Open the connection. cn.Open(); // Fill the reader with one row of data. reader = cmd.ExecuteReader(); // Loop through the reader as long as there is something to read and add values while (reader.Read()) { post = new OrderGiftCard(reader); } } catch (Exception e) { throw e; } finally { // Call Close when done reading to avoid memory leakage. if (reader != null) reader.Close(); } } } // Return the post return post; } // End of the GetOneById method
public ActionResult orders(string id = "", string returnUrl = "") { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get query parameters ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor", "Translator" }) == 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 the gift card GiftCard giftCard = GiftCard.GetOneById(id); // Check if the gift card is null if (giftCard == null) { return(RedirectToAction("index")); } // Set form data ViewBag.CurrentDomain = currentDomain; ViewBag.GiftCard = giftCard; ViewBag.OrderGiftCards = OrderGiftCard.GetByGiftCardId(giftCard.id, "order_id", "ASC"); ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); ViewBag.CultureInfo = Tools.GetCultureInfo(Language.GetOneById(currentDomain.back_end_language)); ViewBag.ReturnUrl = returnUrl; // Return the view return(View()); } // End of the orders method
public HttpResponseMessage delete(Int32 id = 0, string giftCardId = "") { // Create an error code variable Int32 errorCode = 0; // Delete the post errorCode = OrderGiftCard.DeleteOnId(id, giftCardId); // Check if there is an error if (errorCode != 0) { return(Request.CreateResponse <string>(HttpStatusCode.Conflict, "Foreign key constraint")); } // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The delete was successful")); } // End of the delete method
} // End of the constructor #endregion #region Insert methods /// <summary> /// Add one order gift card /// </summary> /// <param name="post">A reference to a order gift card post</param> public static void Add(OrderGiftCard post) { // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "INSERT INTO dbo.orders_gift_cards (order_id, gift_card_id, amount) " + "VALUES (@order_id, @gift_card_id, @amount);"; // The using block is used to call dispose automatically even if there is a exception. using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there is a exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@order_id", post.order_id); cmd.Parameters.AddWithValue("@gift_card_id", post.gift_card_id); cmd.Parameters.AddWithValue("@amount", post.amount); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases try { // Open the connection cn.Open(); // Execute the insert cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } } } } // End of the Add method
public HttpResponseMessage update(OrderGiftCard post) { // Check for errors if (post == null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The post is null"); } else if (Order.MasterPostExists(post.order_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The order does not exist"); } else if (GiftCard.MasterPostExists(post.gift_card_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The gift card does not exist"); } // Make sure that the data is valid post.gift_card_id = AnnytabDataValidation.TruncateString(post.gift_card_id, 50); post.amount = AnnytabDataValidation.TruncateDecimal(post.amount, 0, 999999999999M); // Get the saved post OrderGiftCard savedPost = OrderGiftCard.GetOneById(post.order_id, post.gift_card_id); // Check if the post exists if (savedPost == null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The record does not exist"); } // Update the post OrderGiftCard.Update(post); // Return the success response return Request.CreateResponse<string>(HttpStatusCode.OK, "The update was successful"); } // End of the update method
} // End of the Add method #endregion #region Update methods /// <summary> /// Update a order gift card post /// </summary> /// <param name="post">A reference to a order gift card post</param> public static void Update(OrderGiftCard post) { // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "UPDATE dbo.orders_gift_cards SET amount = @amount WHERE order_id = @order_id AND gift_card_id = @gift_card_id;"; // The using block is used to call dispose automatically even if there are an exception using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there are an exception using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@order_id", post.order_id); cmd.Parameters.AddWithValue("@gift_card_id", post.gift_card_id); cmd.Parameters.AddWithValue("@amount", post.amount); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases. try { // Open the connection. cn.Open(); // Execute the update cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } } } } // End of the Update method
} // End of the RenderOrderConfirmationView method /// <summary> /// Process gift cards and return the total amount of processed gift cards /// </summary> /// <param name="orderId">The id of the order</param> /// <param name="orderTotalSum">The total amount for the order</param> /// <returns>The total amount of processed gift cards</returns> private decimal ProcessGiftCards(Int32 orderId, decimal orderTotalSum) { // Create the decimal to return decimal gift_cards_amount = 0; // Get all of the gift cards List<GiftCard> giftCards = (List<GiftCard>)Session["GiftCards"]; // Make sure that the list not is null if (giftCards != null) { // Loop the list of gift cards for (int i = 0; i < giftCards.Count; i++) { // Create an order gift card post OrderGiftCard orderGiftCard = new OrderGiftCard(); orderGiftCard.order_id = orderId; orderGiftCard.gift_card_id = giftCards[i].id; // Calculate the remaining difference decimal diff = orderTotalSum - gift_cards_amount; // Check if the gift card amount is greater than the order sum or not if (giftCards[i].amount <= diff) { // Update the gift card gift_cards_amount += giftCards[i].amount; orderGiftCard.amount = giftCards[i].amount; giftCards[i].amount = 0; GiftCard.Update(giftCards[i]); // Add the order gift card OrderGiftCard.Add(orderGiftCard); } else { // Update the gift card giftCards[i].amount -= diff; GiftCard.Update(giftCards[i]); gift_cards_amount += diff; // Add the order gift card orderGiftCard.amount = diff; OrderGiftCard.Add(orderGiftCard); // Break out from the loop break; } } } // Return the amount for gift cards return gift_cards_amount; } // End of the ProcessGiftCards method