Retail_manager/RMWPFUserInterface/ViewModels/LoginViewModel.cs
s459315 f0bea75284 Add Saving Cart to the DB
All the changes necessary to save shopping cart to DB
2022-07-25 17:02:36 +02:00

112 lines
2.6 KiB
C#

using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFUserInterface.EventModels;
using RMWPFUserInterface.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RMWPFUserInterface.ViewModels
{
public class LoginViewModel : Screen
{
private string _userName ="test@test";
private string _password = "Test1234.";
private IAPIHelper _apiHelper;
private IEventAggregator _events;
public LoginViewModel(IAPIHelper apiHelper, IEventAggregator events)
{
_apiHelper = apiHelper;
_events = events;
}
public string UserName
{
get { return _userName; }
set
{
_userName = value;
NotifyOfPropertyChange(() => UserName);
NotifyOfPropertyChange(() => CanLogIn);
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
NotifyOfPropertyChange(() => Password);
NotifyOfPropertyChange(() => CanLogIn);
}
}
public bool IsErrorVisible
{
get
{
bool output = false;
if (ErrorMessage?.Length > 0)
{
output = true;
}
return output;
}
}
private string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set
{
_errorMessage = value;
NotifyOfPropertyChange(() => IsErrorVisible);
NotifyOfPropertyChange(() => ErrorMessage);
}
}
public bool CanLogIn
{
get{
bool output = false;
if (UserName?.Length > 0 && Password?.Length > 0)
{
output = true;
}
return output;
}
}
public async Task LogIn()
{
try
{
ErrorMessage = "";
var result = await _apiHelper.Authenticate(UserName, Password);
// Capture more information
await _apiHelper.GetLogedInUserInfo(result.Access_Token);
await _events.PublishOnUIThreadAsync(new LogOnEvent());
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
}
}
}