jibai-kia / ICT2106_P1-6

0 stars 0 forks source link

Implementation of Reward View Model #50

Closed jibai-kia closed 1 year ago

jibai-kia commented 1 year ago

This thread provides updates related to Reward View Model

jibai-kia commented 1 year ago

RewardVM.cs

// RewardVM.cs
using System.Collections.ObjectModel;
using Prism.Mvvm;
using CleanBrightCompany.Domain;
namespace CleanBrightCompany.ViewModels
{
  public class RewardVM : BindableBase
  {
    private readonly IRewardManagement _management;
    private ObservableCollection<Reward> _rewards;

    public ObservableCollection<Reward> Rewards
    {
      get { return _rewards; }
      set { SetProperty(ref _rewards, value); }
    }

    public RewardVM(IRewardManagement management)
    {
      _management = management;
      Rewards = new ObservableCollection<Reward>(_management.GetAllRewards());
    }

    public void CreateReward(Reward reward)
    {
      _management.CreateReward(reward);
      Rewards.Add(reward);
    }

    public void UpdateReward(Reward reward)
    {
      _management.UpdateReward(reward);
    }

    public void DeleteReward(Reward reward)
    {
      _management.DeleteReward(reward);
      Rewards.Remove(reward);
    }
  }
}
jibai-kia commented 1 year ago

-

jibai-kia commented 1 year ago

Explanation

This is a C# class file for a RewardVM, which is a class that implements the logic for managing rewards in a rewards program. The class uses the Prism.Mvvm namespace and inherits from the BindableBase class, which is a base class for implementing the INotifyPropertyChanged interface, used for data binding in WPF and UWP applications.

The RewardVM class has a private field, _management, of type IRewardsManagement, which is an interface for a management that manages rewards data. The class also has a public property, Rewards, of type ObservableCollection, which is a collection of Reward objects. The Rewards property has a getter and a setter that uses the SetProperty method from the BindableBase class to set the field and raise the PropertyChanged event.

The class has a constructor that takes an IRewardsManagement object as a parameter and initializes the Rewards property with the result of calling the GetAllRewards method of the management. The class also has three public methods for creating, updating, and deleting rewards. These methods call the corresponding methods of the management and update the Rewards collection accordingly.

jibai-kia commented 1 year ago

-