Files
GreadyPoang/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs

102 lines
2.5 KiB
C#

using Common.Library;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GreadyPoang.EntityLayer;
using System.Collections.ObjectModel;
namespace GreadyPoang.ViewModelLayer;
public partial class ParticipantViewModel : ObservableObject
{
#region Constructors
public ParticipantViewModel() : base()
{
}
public ParticipantViewModel(IRepository<Participant> repo, IMethodSharingService<Participant> sharingService) : base()
{
_Repository = repo;
_sharingService = sharingService;
ParticipantObject = new Participant();
ParticipantList = new ObservableCollection<Participant>();
IsSaveCommandEnabled = true;
}
#endregion
#region Private Variables
[ObservableProperty]
private Participant? participantObject;
[ObservableProperty]
private ObservableCollection<Participant> participantList;
private readonly IRepository<Participant>? _Repository;
private readonly IMethodSharingService<Participant> _sharingService;
#endregion
[ObservableProperty]
private bool isSaveCommandEnabled;
#region Get Method
public ObservableCollection<Participant> Get()
{
ParticipantList = _sharingService.Get();
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;
}
#endregion
[RelayCommand(CanExecute = nameof(IsSaveCommandEnabled))]
private async Task<bool> Save()
{
if (_Repository == null || ParticipantObject == null)
{
return false;
}
var tmpTask = _Repository.Save(ParticipantObject);
int tmp = tmpTask.GetAwaiter().GetResult();
if (tmp != -1)
{
ParticipantObject = new Participant();
this.Get();
await Shell.Current.GoToAsync("..");
}
return tmp != -1;
}
[RelayCommand]
private void DeleteAsync(Participant pp)
{
Console.WriteLine($"Valt från ViewModel: {pp.FullName}");
if (_Repository == null || pp == null || pp.ParticipantId <= 0)
{
return;
}
var tmpTask = _Repository.Delete(pp);
this.Get();
Shell.Current.GoToAsync("..");
}
}