MovieViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Core;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.Toolkit.Mvvm.Input;
  4. using MyMoviesWPF.Models;
  5. using MyMoviesWPF.MVVM.ViewModel.Core;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. namespace MyMoviesWPF.MVVM.ViewModel
  13. {
  14. public class MovieViewModel : BaseViewModel
  15. {
  16. private ObservableCollection<ActorList> _actors;
  17. private RelayCommand _addToCart;
  18. private RelayCommand _cancel;
  19. private bool _addToCartEnability = true;
  20. private Order _orders;
  21. public string Name { get => Service.movie.Name; }
  22. public string Description { get => Service.movie.Description; }
  23. public Genre Genre { get => Service.db.Genres.Where(g => g.Idgenre == Service.movie.Idgenre).FirstOrDefault(); }
  24. public string TrailerURL { get => Service.movie.Trailer; }
  25. public string Year { get => Convert.ToString(Service.movie.ProductYear); }
  26. public string Languages { get => Service.movie.Languages; }
  27. public string Price { get => Convert.ToString(Service.movie.Price); }
  28. public ObservableCollection<ActorList> ActorsCollection
  29. {
  30. get => _actors;
  31. set
  32. {
  33. _actors = value;
  34. }
  35. }
  36. public RelayCommand Cancel
  37. {
  38. get
  39. {
  40. return _cancel
  41. ?? (_cancel = new RelayCommand(
  42. async () =>
  43. {
  44. Service.MainViewModel.UpdatePage(Service.catalogPage);
  45. }));
  46. }
  47. }
  48. public MovieViewModel()
  49. {
  50. ActorsCollection = new(Service.db.ActorLists.Where(o => o.ListNum == Service.movie.IdactorList).Include(q => q.IdactorNavigation));
  51. foreach(Order order in Service.db.Orders)
  52. {
  53. if(order.Idmovie == Service.movie.Idmovie && order.Iduser == Service.LoggedUser.Iduser && order.Status != "Отменен")
  54. {
  55. UpdateEnability(false);
  56. }
  57. }
  58. }
  59. public Order Orders
  60. {
  61. get => _orders;
  62. set
  63. {
  64. if (_orders == value)
  65. return;
  66. _orders = value;
  67. OnPropertyChanged("Orders");
  68. }
  69. }
  70. public bool AddToCartEnability
  71. {
  72. get => _addToCartEnability;
  73. set
  74. {
  75. if (_addToCartEnability == value)
  76. return;
  77. _addToCartEnability = value;
  78. OnPropertyChanged("AddToCartEnability");
  79. }
  80. }
  81. public RelayCommand AddToCart
  82. {
  83. get
  84. {
  85. return _addToCart
  86. ?? (_addToCart = new RelayCommand(
  87. async () =>
  88. {
  89. MessageBox.Show("Фильм успешно добавлен в корзину !", "Успех", MessageBoxButton.OK, MessageBoxImage.Information);
  90. Service.CartMoviesCollection.Add(Service.movie);
  91. Service.MainViewModel.UpdateCartStr();
  92. UpdateEnability();
  93. }));
  94. }
  95. }
  96. public void UpdateEnability()
  97. {
  98. if(_addToCartEnability == true)
  99. {
  100. _addToCartEnability = false;
  101. }
  102. else
  103. {
  104. _addToCartEnability = true;
  105. }
  106. OnPropertyChanged("AddToCartEnability");
  107. }
  108. public void UpdateEnability(bool value)
  109. {
  110. _addToCartEnability = value;
  111. OnPropertyChanged("AddToCartEnability");
  112. }
  113. }
  114. }