MovieViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 (Service.LoggedUser == null)
  54. {
  55. UpdateEnability(false);
  56. }
  57. else if(order.Idmovie == Service.movie.Idmovie && order.Iduser == Service.LoggedUser.Iduser && order.Status != "Отменен")
  58. {
  59. UpdateEnability(false);
  60. }
  61. else
  62. {
  63. UpdateEnability(true);
  64. }
  65. }
  66. }
  67. public Order Orders
  68. {
  69. get => _orders;
  70. set
  71. {
  72. if (_orders == value)
  73. return;
  74. _orders = value;
  75. OnPropertyChanged("Orders");
  76. }
  77. }
  78. public bool AddToCartEnability
  79. {
  80. get => _addToCartEnability;
  81. set
  82. {
  83. if (_addToCartEnability == value)
  84. return;
  85. _addToCartEnability = value;
  86. OnPropertyChanged("AddToCartEnability");
  87. }
  88. }
  89. public RelayCommand AddToCart
  90. {
  91. get
  92. {
  93. return _addToCart
  94. ?? (_addToCart = new RelayCommand(
  95. async () =>
  96. {
  97. MessageBox.Show("Фильм успешно добавлен в корзину !", "Успех", MessageBoxButton.OK, MessageBoxImage.Information);
  98. Service.CartMoviesCollection.Add(Service.movie);
  99. Service.MainViewModel.UpdateCartStr();
  100. UpdateEnability();
  101. }));
  102. }
  103. }
  104. public void UpdateEnability()
  105. {
  106. if(_addToCartEnability == true)
  107. {
  108. _addToCartEnability = false;
  109. }
  110. else
  111. {
  112. _addToCartEnability = true;
  113. }
  114. OnPropertyChanged("AddToCartEnability");
  115. }
  116. public void UpdateEnability(bool value)
  117. {
  118. _addToCartEnability = value;
  119. OnPropertyChanged("AddToCartEnability");
  120. }
  121. }
  122. }