public ActionResult Edit(TextMessage textMessage) { using (var context = new DatabaseContext()) { if (!ModelState.IsValid) return View(CreateTuple(context, textMessage)); try { context.Entry(textMessage).State = EntityState.Modified; context.SaveChanges(); return RedirectToAction("WithTextMessages", "Client", new { id = textMessage.ClientId }); } catch { return View(CreateTuple(context, textMessage)); } } }
/// <summary>GET action of the CREATE functionality</summary> /// <returns>View with new object</returns> public ActionResult Create(int clientId) { using (var context = new DatabaseContext()) { var client = context.Clients .Include(x => x.Manager) .First(x => x.Id == clientId); var textMessage = new TextMessage { ClientId = clientId, DeliveryDate = DateTime.Now, DeliveryStatus = DeliveryStatusType.Failure }; return View(new Tuple<Manager, Client, TextMessage>(client.Manager, client, textMessage)); } }
/// <summary>Creates POC object from its properties</summary> /// <returns>POC object</returns> private static TextMessage ParseEntity(string id, string clientId, string deliveryDate, string description, string deliveryStatus) { var textMessage = new TextMessage(); int no; if (!int.TryParse(id, out no)) throw new Exception("Incorrect ID during instantiating of Text Message"); textMessage.Id = no; int clientNo; if (!int.TryParse(clientId, out clientNo)) throw new Exception("Incorrect Client ID during instantiating of Text Message with ID = " + textMessage.Id); textMessage.ClientId = clientNo; if (!string.IsNullOrWhiteSpace(deliveryDate)) { DateTime date; if (!DateTime.TryParse(deliveryDate, out date)) throw new Exception("Incorrect Delivery Date during instantiating of Text Message with ID = " + textMessage.Id); textMessage.DeliveryDate = date; } textMessage.Description = WebUtility.HtmlDecode( (string.IsNullOrWhiteSpace(description) ? string.Empty : description).Trim()); switch (deliveryStatus) { case "Доставлено": textMessage.DeliveryStatus = DeliveryStatusType.Received; break; case "Отправлено": textMessage.DeliveryStatus = DeliveryStatusType.Sent; break; case "Ошибка!": textMessage.DeliveryStatus = DeliveryStatusType.Failure; break; default: throw new Exception("Incorrect Delivery Status during instantiating of Text Message with ID = " + textMessage.Id); } return textMessage; }
/// <summary>Creates view model</summary> /// <returns>View model</returns> private static Tuple<Manager, Client, TextMessage> CreateTuple(DatabaseContext context, TextMessage textMessage) { textMessage.Client = context.Clients .Include(x => x.Manager) .First(x => x.Id == textMessage.ClientId); return new Tuple<Manager, Client, TextMessage>(textMessage.Client.Manager, textMessage.Client, textMessage); }