MovieViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 RelayCommand _update;
  20. private bool _addToCartEnability = true;
  21. private Order _orders;
  22. public string Name { get => Service.movie.Name; }
  23. public string Description { get => Service.movie.Description; }
  24. public Genre Genre { get => Service.db.Genres.Where(g => g.Idgenre == Service.movie.Idgenre).FirstOrDefault(); }
  25. public string TrailerURL { get => Service.movie.Trailer; }
  26. public string Year { get => Convert.ToString(Service.movie.ProductYear); }
  27. public string Languages { get => Service.movie.Languages; }
  28. public string Price { get => Convert.ToString(Service.movie.Price); }
  29. public ObservableCollection<ActorList> ActorsCollection
  30. {
  31. get => _actors;
  32. set
  33. {
  34. _actors = value;
  35. }
  36. }
  37. public RelayCommand Update
  38. {
  39. get
  40. {
  41. return _update
  42. ?? (_update = new RelayCommand(
  43. async () =>
  44. {
  45. UpdateEnability();
  46. }));
  47. }
  48. }
  49. public RelayCommand Cancel
  50. {
  51. get
  52. {
  53. return _cancel
  54. ?? (_cancel = new RelayCommand(
  55. async () =>
  56. {
  57. Service.MainViewModel.UpdatePage(Service.catalogPage);
  58. }));
  59. }
  60. }
  61. public MovieViewModel()
  62. {
  63. ActorsCollection = new(Service.db.ActorLists.Where(o => o.ListNum == Service.movie.IdactorList).Include(q => q.IdactorNavigation));
  64. foreach(Order order in Service.db.Orders)
  65. {
  66. if (Service.LoggedUser == null)
  67. {
  68. UpdateEnability(false);
  69. }
  70. else if(order.Idmovie == Service.movie.Idmovie && order.Iduser == Service.LoggedUser.Iduser && order.Status != "Отменен")
  71. {
  72. UpdateEnability(false);
  73. }
  74. }
  75. }
  76. public Order Orders
  77. {
  78. get => _orders;
  79. set
  80. {
  81. if (_orders == value)
  82. return;
  83. _orders = value;
  84. OnPropertyChanged("Orders");
  85. }
  86. }
  87. public bool AddToCartEnability
  88. {
  89. get => _addToCartEnability;
  90. set
  91. {
  92. if (_addToCartEnability == value)
  93. return;
  94. _addToCartEnability = value;
  95. OnPropertyChanged("AddToCartEnability");
  96. }
  97. }
  98. public RelayCommand AddToCart
  99. {
  100. get
  101. {
  102. return _addToCart
  103. ?? (_addToCart = new RelayCommand(
  104. async () =>
  105. {
  106. MessageBox.Show("Фильм успешно добавлен в корзину !", "Успех", MessageBoxButton.OK, MessageBoxImage.Information);
  107. Service.CartMoviesCollection.Add(Service.movie);
  108. Service.MainViewModel.UpdateCartStr();
  109. UpdateEnability();
  110. }));
  111. }
  112. }
  113. public void UpdateEnability()
  114. {
  115. if(_addToCartEnability == true)
  116. {
  117. _addToCartEnability = false;
  118. }
  119. else
  120. {
  121. _addToCartEnability = true;
  122. }
  123. OnPropertyChanged("AddToCartEnability");
  124. }
  125. public void UpdateEnability(bool value)
  126. {
  127. _addToCartEnability = value;
  128. OnPropertyChanged("AddToCartEnability");
  129. }
  130. }
  131. }