Skip to content

Commit

Permalink
Add delete method for route repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
sandermvanvliet committed Oct 30, 2023
1 parent 8236ed4 commit 2f3c1b9
Show file tree
Hide file tree
Showing 20 changed files with 533 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .idea/.idea.RoadCaptain.Windows/.idea/avalonia.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/RoadCaptain.Adapters/HttpRouteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,10 @@ public async Task<RouteModel> StoreAsync(PlannedRoute plannedRoute, string? toke
return null;
}
}

public Task DeleteAsync(Uri routeUri)
{
throw new NotImplementedException();
}
}
}
5 changes: 5 additions & 0 deletions src/RoadCaptain.Adapters/LocalDirectoryRouteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,10 @@ protected virtual Task WriteAllTextAsync(string path, string serialized)
return null;
}
}

public Task DeleteAsync(Uri routeUri)
{
throw new NotImplementedException();
}
}
}
4 changes: 4 additions & 0 deletions src/RoadCaptain.Adapters/RebelRouteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public Task<RouteModel> StoreAsync(PlannedRoute plannedRoute, string? token, Lis

public string Name => "Zwift Insider - Rebel Routes";
public bool IsReadOnly => true;
public Task DeleteAsync(Uri routeUri)
{
throw new InvalidOperationException("Rebel Routes are baked into RoadCaptain and can't be deleted.");
}

private PlannedRoute? UpgradeIfNecessaryAndSerialize(string? routeModelSerialized)
{
Expand Down
5 changes: 5 additions & 0 deletions src/RoadCaptain.App.RouteBuilder/DelegateDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public async Task ShowWhatIsNewDialog(Release release)
return await InvokeIfNeededAsync(() => _decorated.ShowOpenRouteDialog());
}

public async Task<MessageBoxResult> ShowQuestionDialog(string title, string message)
{
return await InvokeIfNeededAsync(() => _decorated.ShowQuestionDialog(title, message));
}

public async Task<bool> ShowDefaultSportSelectionDialog(SportType sport)
{
return await InvokeIfNeededAsync(() => _decorated.ShowDefaultSportSelectionDialog(sport));
Expand Down
5 changes: 5 additions & 0 deletions src/RoadCaptain.App.RouteBuilder/DesignTimeWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public Task ShowWhatIsNewDialog(Release release)
throw new System.NotImplementedException();
}

public Task<MessageBoxResult> ShowQuestionDialog(string title, string message)
{
throw new System.NotImplementedException();
}

public Task<bool> ShowDefaultSportSelectionDialog(SportType sport)
{
throw new System.NotImplementedException();
Expand Down
1 change: 1 addition & 0 deletions src/RoadCaptain.App.RouteBuilder/IWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public interface IWindowService : RoadCaptain.App.Shared.IWindowService
Window? GetCurrentWindow();
Task<string?> ShowSaveFileDialog(string? previousLocation, string? suggestedFileName = null);
Task<(PlannedRoute? PlannedRoute, string? RouteFilePath)> ShowOpenRouteDialog();
Task<MessageBoxResult> ShowQuestionDialog(string title, string message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Immutable;
using RoadCaptain.UseCases;

namespace RoadCaptain.App.RouteBuilder.ViewModels
{
public class DesignTimeManageRoutesViewModel : ManageRoutesViewModel
{
public DesignTimeManageRoutesViewModel()
: base(new RetrieveRepositoryNamesUseCase(new[] { new StubRouteRepository() }), null!, new DeleteRouteUseCase(new []{new StubRouteRepository()}))
{
Repositories = new[]
{
"All",
"Local"
}.ToImmutableList();

Routes = new[]
{
new RoadCaptain.App.Shared.ViewModels.RouteViewModel(new RouteModel
{
Ascent = 123,
Descent = 75,
Distance = 105,
CreatorName = "Joe Bloegs",
World = "watopia",
Id = 1,
IsLoop = false,
Name = "Design time route 1",
RepositoryName = "Local",
ZwiftRouteName = "ZRName1"
}),

new RoadCaptain.App.Shared.ViewModels.RouteViewModel(new RouteModel
{
Ascent = 13,
Descent = 45,
Distance = 45,
CreatorName = "Joe Blogs",
World = "yorkshire",
Id = 2,
IsLoop = true,
Name = "Design time route 2",
RepositoryName = "Local",
ZwiftRouteName = "ZRName2"
}),

new RoadCaptain.App.Shared.ViewModels.RouteViewModel(new RouteModel
{
Ascent = 13,
Descent = 45,
Distance = 45,
CreatorName = "Joe Blogs",
World = "makuri_islands",
Id = 3,
IsLoop = true,
Name = "Design time route 3",
RepositoryName = "Local",
ZwiftRouteName = "ZRName3"
})
}.ToImmutableList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under Artistic License 2.0
// See LICENSE or https://choosealicense.com/licenses/artistic-2.0/

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
Expand Down Expand Up @@ -64,5 +65,9 @@ public Task<RouteModel> StoreAsync(PlannedRoute plannedRoute, string? token, Lis

public string Name => "Local";
public bool IsReadOnly => false;
public Task DeleteAsync(Uri routeUri)
{
throw new NotImplementedException();
}
}
}
108 changes: 108 additions & 0 deletions src/RoadCaptain.App.RouteBuilder/ViewModels/ManageRoutesViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Immutable;
using System.Threading.Tasks;
using ReactiveUI;
using RoadCaptain.App.Shared.Commands;
using RoadCaptain.App.Shared.Dialogs;
using RoadCaptain.Commands;
using RoadCaptain.UseCases;

namespace RoadCaptain.App.RouteBuilder.ViewModels
{
public class ManageRoutesViewModel : ViewModelBase
{
private ImmutableList<string>? _repositories;
private string? _selectedRepository;
private readonly RetrieveRepositoryNamesUseCase _retrieveRepositoryNamesUseCase;
private ImmutableList<Shared.ViewModels.RouteViewModel>? _routes;
private readonly IWindowService _windowService;
private readonly DeleteRouteUseCase _deleteRouteUseCase;

public ManageRoutesViewModel(RetrieveRepositoryNamesUseCase retrieveRepositoryNamesUseCase, IWindowService windowService, DeleteRouteUseCase deleteRouteUseCase)
{
_retrieveRepositoryNamesUseCase = retrieveRepositoryNamesUseCase;
_windowService = windowService;
_deleteRouteUseCase = deleteRouteUseCase;
}

public AsyncRelayCommand DeleteRouteCommand => new AsyncRelayCommand(
parameter => DeleteRouteAsync((parameter as Shared.ViewModels.RouteViewModel)!),
parameter => parameter is Shared.ViewModels.RouteViewModel);

private async Task<CommandResult> DeleteRouteAsync(Shared.ViewModels.RouteViewModel parameter)
{
var result = await _windowService.ShowQuestionDialog(
"Delete route?",
$"Are you sure you want to delete the route {parameter.Name}?");

if (result == MessageBoxResult.No)
{
return CommandResult.Aborted();
}

try
{
await _deleteRouteUseCase.ExecuteAsync(new DeleteRouteCommand(parameter.Uri, parameter.RepositoryName));
}
catch (Exception e)
{
return CommandResult.Failure($"Failed to delete route: {e.Message}");
}

return CommandResult.Success();
}

public Task InitializeAsync()
{
Repositories = _retrieveRepositoryNamesUseCase.Execute(new RetrieveRepositoryNamesCommand(RetrieveRepositoriesIntent.Manage)).ToImmutableList();

return Task.CompletedTask;
}

public ImmutableList<string> Repositories
{
get => _repositories ?? ImmutableList<string>.Empty;
set
{
if (value == _repositories)
{
return;
}

_repositories = value;
this.RaisePropertyChanged();
}
}

public string? SelectedRepository
{
get => _selectedRepository;
set
{
if (value == _selectedRepository)
{
return;
}

_selectedRepository = value;
this.RaisePropertyChanged();
}
}

public ImmutableList<Shared.ViewModels.RouteViewModel> Routes
{
get => _routes ?? ImmutableList<Shared.ViewModels.RouteViewModel>.Empty;
set
{
if (_routes != null && value == _routes)
{
return;
}

_routes = value;

this.RaisePropertyChanged();
}
}
}
}
Loading

0 comments on commit 2f3c1b9

Please sign in to comment.