demo_trade/ViewModels/LoginViewModel.cs
2024-10-04 15:41:04 +03:00

64 lines
2.0 KiB
C#

using demo_trade.Data.Repository;
using demo_trade.Domain;
using demo_trade.Models;
using ReactiveUI;
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo_trade.ViewModels
{
public class LoginViewModel: ViewModelBase, IRoutableViewModel
{
private LoginUseCase _loginUseCase;
private string _errorMessage = string.Empty;
public string ErrorMessage { get => _errorMessage; set => this.RaiseAndSetIfChanged(ref _errorMessage, value); }
private UserLogin _userLogin = new();
public UserLogin UserLogin {
get => _userLogin;
set => this.RaiseAndSetIfChanged(ref _userLogin, value);
}
public ReactiveCommand<Unit, Unit> LoginCommand { get;}
public ReactiveCommand<Unit, Unit> GuestCommand { get; }
public string? UrlPathSegment { get; } = Guid.NewGuid().ToString();
public IScreen HostScreen { get; }
public LoginViewModel(IScreen screen, ILoginRepository loginRepository) {
_loginUseCase = new LoginUseCase(loginRepository);
HostScreen = screen;
LoginCommand = ReactiveCommand.Create(() =>
{
try
{
var userLogin = _loginUseCase.AutorizationByLoginAndPassword(_userLogin);
SQLProductRepository sqlProductRepository = new SQLProductRepository();
HostScreen.Router.Navigate.Execute(new ClientProductViewModel(HostScreen, sqlProductRepository, userLogin));
}
catch (Exception ex) {
ErrorMessage = ex.Message;
}
}
);
GuestCommand = ReactiveCommand.Create(() => {
SQLProductRepository sqlProductRepository = new SQLProductRepository();
HostScreen.Router.Navigate.Execute(new GuestProductViewModel(HostScreen, sqlProductRepository));
});
}
}
}