mirror of https://github.com/ErsatzTV/ErsatzTV.git
10 changed files with 100 additions and 13 deletions
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using MediatR; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Commands |
||||
{ |
||||
public record SaveConfigElementByKey(ConfigElementKey Key, string Value) : IRequest<LanguageExt.Unit>; |
||||
} |
||||
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using LanguageExt; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Commands |
||||
{ |
||||
public class SaveConfigElementByKeyHandler : MediatR.IRequestHandler<SaveConfigElementByKey, Unit> |
||||
{ |
||||
private readonly IConfigElementRepository _configElementRepository; |
||||
|
||||
public SaveConfigElementByKeyHandler(IConfigElementRepository configElementRepository) => |
||||
_configElementRepository = configElementRepository; |
||||
|
||||
public async Task<Unit> Handle(SaveConfigElementByKey request, CancellationToken cancellationToken) |
||||
{ |
||||
Option<ConfigElement> maybeElement = await _configElementRepository.Get(request.Key); |
||||
await maybeElement.Match( |
||||
ce => |
||||
{ |
||||
ce.Value = request.Value; |
||||
return _configElementRepository.Update(ce); |
||||
}, |
||||
() => |
||||
{ |
||||
var ce = new ConfigElement { Key = request.Key.Key, Value = request.Value }; |
||||
return _configElementRepository.Add(ce); |
||||
}); |
||||
|
||||
return Unit.Default; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
namespace ErsatzTV.Application.Configuration |
||||
{ |
||||
public record ConfigElementViewModel(string Key, string Value); |
||||
} |
||||
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
namespace ErsatzTV.Application.Configuration |
||||
{ |
||||
internal static class Mapper |
||||
{ |
||||
internal static ConfigElementViewModel ProjectToViewModel(Core.Domain.ConfigElement element) => |
||||
new(element.Key, element.Value); |
||||
} |
||||
} |
||||
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Queries |
||||
{ |
||||
public record GetConfigElementByKey(ConfigElementKey Key) : IRequest<Option<ConfigElementViewModel>>; |
||||
} |
||||
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
using static ErsatzTV.Application.Configuration.Mapper; |
||||
|
||||
namespace ErsatzTV.Application.Configuration.Queries |
||||
{ |
||||
public class GetConfigElementByKeyHandler : IRequestHandler<GetConfigElementByKey, Option<ConfigElementViewModel>> |
||||
{ |
||||
private readonly IConfigElementRepository _configElementRepository; |
||||
|
||||
public GetConfigElementByKeyHandler(IConfigElementRepository configElementRepository) => |
||||
_configElementRepository = configElementRepository; |
||||
|
||||
public Task<Option<ConfigElementViewModel>> Handle( |
||||
GetConfigElementByKey request, |
||||
CancellationToken cancellationToken) => |
||||
_configElementRepository.Get(request.Key).MapT(ProjectToViewModel); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue