OrderCartWindowModel.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using OOO_WriteAndClear.DBModels;
  2. using OOO_WriteAndClear.MVP.MVPInterfaces;
  3. using System.Windows;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OOO_WriteAndClear.MVP.Models
  10. {
  11. public class OrderCartWindowModel : IOrderCartWindowContract.IModel
  12. {
  13. public OrderCartWindowModel() { }
  14. public OrderFormerer.OrderFormerer OrderFormerer { get; set; }
  15. #region Реализация IOrderCartWindowContract.IModel
  16. public void AddProductUnitIntoOrder(string productArticle)
  17. {
  18. Product? product = GetProductByArticle(productArticle);
  19. if (product is null)
  20. return;
  21. OrderFormerer.AddProductUnitIntoOrder(product);
  22. }
  23. public void RemoveProductFromOrder(string productArticle)
  24. {
  25. Product? product = GetProductByArticle(productArticle);
  26. if (product is null)
  27. return;
  28. OrderFormerer.RemoveProductFromOrder(product);
  29. }
  30. public void RemoveProductUnitFromOrder(string productArticle)
  31. {
  32. Product? product = GetProductByArticle(productArticle);
  33. if (product is null)
  34. return;
  35. OrderFormerer.RemoveProductUnitFromOrder(product);
  36. }
  37. public ICollection<PickupPoint> GetAllPickupPoints()
  38. {
  39. using (VorobiewTradeContext db = new VorobiewTradeContext())
  40. {
  41. return db.PickupPoints.ToList();
  42. };
  43. }
  44. public void AddOrderInSystem(PickupPoint selectedPickupPoint)
  45. {
  46. OrderFormerer.Order.OrderPickupPoint = selectedPickupPoint;
  47. User currentUser = (User)Application.Current.Resources["CurrentUser"];
  48. if (currentUser.UserId == -1)
  49. OrderFormerer.Order.OrderCustomer = "";
  50. else
  51. OrderFormerer.Order.OrderCustomer = currentUser.ToString();
  52. Order AddingOrder = OrderFormerer.PrepareOrderForAdd();
  53. using (VorobiewTradeContext db = new VorobiewTradeContext())
  54. {
  55. db.Orders.Add(AddingOrder);
  56. db.SaveChanges();
  57. }
  58. OrderFormerer.Order.OrderId = AddingOrder.OrderId;
  59. }
  60. #endregion
  61. private Product? GetProductByArticle(string article)
  62. {
  63. using (VorobiewTradeContext db = new VorobiewTradeContext())
  64. return db.Products.Find(article);
  65. }
  66. }
  67. }