Files
ersatztv/ErsatzTV.Infrastructure/Data/Repositories/ConfigElementRepository.cs
T
Jason DoveandGitHub d4a2197dfa async fixes (#128)
* refactor local metadata provider

* resolve async warnings

* more async fixes
2021-04-03 11:01:20 -05:00

47 lines
1.5 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using LanguageExt;
using Microsoft.EntityFrameworkCore;
using static LanguageExt.Prelude;
namespace ErsatzTV.Infrastructure.Data.Repositories
{
public class ConfigElementRepository : IConfigElementRepository
{
private readonly TvContext _dbContext;
public ConfigElementRepository(TvContext dbContext) => _dbContext = dbContext;
public async Task<ConfigElement> Add(ConfigElement configElement)
{
await _dbContext.ConfigElements.AddAsync(configElement);
await _dbContext.SaveChangesAsync();
return configElement;
}
public Task<Option<ConfigElement>> Get(ConfigElementKey key) =>
_dbContext.ConfigElements
.OrderBy(ce => ce.Key)
.SingleOrDefaultAsync(ce => ce.Key == key.Key)
.Map(Optional);
public Task<Option<T>> GetValue<T>(ConfigElementKey key) =>
Get(key).MapT(ce => (T) Convert.ChangeType(ce.Value, typeof(T)));
public Task Update(ConfigElement configElement)
{
_dbContext.ConfigElements.Update(configElement);
return _dbContext.SaveChangesAsync();
}
public Task Delete(ConfigElement configElement)
{
_dbContext.ConfigElements.Remove(configElement);
return _dbContext.SaveChangesAsync();
}
}
}