105 lines
2.6 KiB
C#
105 lines
2.6 KiB
C#
using Common.Library;
|
|
using GreadyPoang.EntityLayer;
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
namespace GreadyPoang.ViewModelLayer;
|
|
|
|
public class ParticipantViewModel : ViewModelBase
|
|
{
|
|
#region Constructors
|
|
public ParticipantViewModel() : base()
|
|
{
|
|
|
|
}
|
|
|
|
public ParticipantViewModel(IRepository<Participant> repo) : base()
|
|
{
|
|
Repository = repo;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Variables
|
|
private Participant? _ParticipantObject = new();
|
|
private ObservableCollection<Participant> _ParticipantList = new();
|
|
private readonly IRepository<Participant>? Repository;
|
|
#endregion
|
|
|
|
#region public Properties
|
|
|
|
public Participant? ParticipantObject
|
|
{
|
|
get { return _ParticipantObject; }
|
|
set
|
|
{
|
|
_ParticipantObject = value;
|
|
RaisePropertyChanged(nameof(ParticipantObject));
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<Participant> ParticipantList
|
|
{
|
|
get { return _ParticipantList; }
|
|
set
|
|
{
|
|
_ParticipantList = value;
|
|
RaisePropertyChanged(nameof(ParticipantList));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Get Method
|
|
public ObservableCollection<Participant> Get()
|
|
{
|
|
if (Repository != null)
|
|
{
|
|
var participantsTask = Repository.Get();
|
|
var participants = participantsTask is Task<IEnumerable<Participant>> task
|
|
? task.GetAwaiter().GetResult()
|
|
: (IEnumerable<Participant>)participantsTask;
|
|
foreach (var participant in participants)
|
|
{
|
|
if (!_ParticipantList.Any(p => p.ParticipantId == participant.ParticipantId))
|
|
{
|
|
ParticipantList.Add(participant);
|
|
}
|
|
}
|
|
}
|
|
return ParticipantList;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Get(id) Method
|
|
public Participant? Get(int id)
|
|
{
|
|
try
|
|
{
|
|
ParticipantObject = Repository?.Get(id).GetAwaiter().GetResult();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
|
|
}
|
|
|
|
return ParticipantObject;
|
|
}
|
|
public virtual bool Save()
|
|
{
|
|
if (Repository == null || ParticipantObject == null)
|
|
{
|
|
return false;
|
|
}
|
|
var tmpTask = Repository.Save(ParticipantObject);
|
|
bool tmp = tmpTask.GetAwaiter().GetResult();
|
|
if (tmp)
|
|
{
|
|
ParticipantObject = new Participant();
|
|
this.Get();
|
|
}
|
|
return tmp;
|
|
}
|
|
#endregion
|
|
} |