gamenight/FrontendAvalonia/FrontendAvalonia/ViewModels/AddGamenightViewModel.cs

86 lines
2.0 KiB
C#

using System;
using System.Runtime.InteropServices.JavaScript;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using FrontendAvalonia.Models;
using FrontendAvalonia.Services.GamenightApi;
using ReactiveUI;
namespace FrontendAvalonia.ViewModels;
public class AddGamenightViewModel : ViewModelBase
{
private IGamenight Gamenight { get; }
private GamenightModel GamenightModel { get; }
private bool _expanded;
public bool Expanded
{
get => _expanded;
set => this.RaiseAndSetIfChanged(ref _expanded, value);
}
public AddGamenightViewModel(IGamenight gamenight, GamenightModel gamenightModel)
{
Gamenight = gamenight;
GamenightModel = gamenightModel;
StartDate = DateTime.Now.Date;
EndDate = DateTime.Now.Date;
AddCommand = ReactiveCommand.CreateFromTask(AddGamenight);
}
private async Task AddGamenight(CancellationToken ct)
{
var body = new Gamenight
{
Name = Name,
Datetime = (StartDate + StartTime).ToString("u").Replace(" ", "T"),
};
await Gamenight.PostGamenight(body, GamenightModel.UserToken, ct);
}
private string _name = string.Empty;
public string Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
private DateTimeOffset _startDate;
public DateTimeOffset StartDate
{
get => _startDate;
set => this.RaiseAndSetIfChanged(ref _startDate, value);
}
private TimeSpan _startTime;
public TimeSpan StartTime
{
get => _startTime;
set => this.RaiseAndSetIfChanged(ref _startTime, value);
}
private DateTimeOffset _endDate;
public DateTimeOffset EndDate
{
get => _endDate;
set => this.RaiseAndSetIfChanged(ref _endDate, value);
}
private TimeSpan _endTime;
public TimeSpan EndTime
{
get => _endTime;
set => this.RaiseAndSetIfChanged(ref _endTime, value);
}
public ICommand AddCommand { get; }
}