mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.5 KiB
46 lines
1.5 KiB
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 async Task Update(ConfigElement configElement) |
|
{ |
|
_dbContext.ConfigElements.Update(configElement); |
|
await _dbContext.SaveChangesAsync(); |
|
} |
|
|
|
public async Task Delete(ConfigElement configElement) |
|
{ |
|
_dbContext.ConfigElements.Remove(configElement); |
|
await _dbContext.SaveChangesAsync(); |
|
} |
|
} |
|
}
|
|
|