OrdersUserControlModel.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Microsoft.EntityFrameworkCore;
  2. using OOO_WriteAndClear.DBModels;
  3. using OOO_WriteAndClear.MVP.Models.ProductsUserControlModel;
  4. using OOO_WriteAndClear.MVP.MVPInterfaces;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace OOO_WriteAndClear.MVP.Models
  11. {
  12. public class OrdersUserControlModel : IOrdersUserControlContract.Model
  13. {
  14. public OrdersUserControlModel()
  15. {
  16. _orders = new List<Order>();
  17. _ordersFromDB = new List<Order>();
  18. }
  19. public ICollection<Order> Orders => _orders;
  20. public void UpdateOrdersFromDB()
  21. {
  22. using(VorobiewTradeContext db = new VorobiewTradeContext())
  23. {
  24. _ordersFromDB = db.Orders.Include(a => a.OrderProducts)
  25. .ThenInclude(a => a.ProductArticleNumberNavigation)
  26. .Include(a => a.OrderPickupPoint)
  27. .Include(a => a.OrderStatus)
  28. .ToList();
  29. }
  30. }
  31. public void FilterAndSorting(FilterByDiscountParameters filterParams, string sortingParams)
  32. {
  33. _orders = SortOrders(FiltOrders(_ordersFromDB, filterParams), sortingParams);
  34. }
  35. private ICollection<Order> _orders;
  36. private ICollection<Order> _ordersFromDB;
  37. private ICollection<Order> FiltOrders(ICollection<Order> orders, FilterByDiscountParameters parameters)
  38. {
  39. return orders.Where(a =>
  40. decimal.ToDouble(a.OrderDiscountPercent * 100) >= parameters.From
  41. && decimal.ToDouble(a.OrderDiscountPercent * 100) < parameters.Before
  42. ).ToList();
  43. }
  44. private ICollection<Order> SortOrders(ICollection<Order> orders, string sortVarients)
  45. {
  46. switch(sortVarients)
  47. {
  48. case "По возрастанию":
  49. return orders.OrderBy(a => a.OrderCost).ToList();
  50. case "По убыванию":
  51. return orders.OrderByDescending(a => a.OrderCost).ToList();
  52. default:
  53. return orders;
  54. }
  55. }
  56. }
  57. }