2019-02-22 20:06:51 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-02-14 14:01:26 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-02-22 20:06:51 -05:00
|
|
|
|
using System.Linq;
|
2018-02-14 14:01:26 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Services
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
|
|
|
|
public class MetaService
|
|
|
|
|
{
|
2019-02-22 20:06:51 -05:00
|
|
|
|
private static List<Func<int, Task<List<ProfileMeta>>>> _metaActions = new List<Func<int, Task<List<ProfileMeta>>>>();
|
2018-02-14 14:01:26 -05:00
|
|
|
|
|
2019-02-22 20:06:51 -05:00
|
|
|
|
public async Task AddPersistentMeta(string metaKey, string metaValue, EFClient client)
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
2019-02-22 20:06:51 -05:00
|
|
|
|
using (var ctx = new DatabaseContext())
|
|
|
|
|
{
|
|
|
|
|
var existingMeta = await ctx.EFMeta.FirstOrDefaultAsync(_meta => _meta.ClientId == client.ClientId && _meta.Key == metaKey);
|
|
|
|
|
|
|
|
|
|
if (existingMeta != null)
|
|
|
|
|
{
|
|
|
|
|
existingMeta.Value = metaValue;
|
|
|
|
|
existingMeta.Updated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ctx.EFMeta.Add(new EFMeta()
|
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
Created = DateTime.UtcNow,
|
|
|
|
|
Key = metaKey,
|
|
|
|
|
Value = metaValue
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 20:37:53 -05:00
|
|
|
|
public async Task<EFMeta> GetPersistentMeta(string metaKey, EFClient client)
|
2019-02-22 20:06:51 -05:00
|
|
|
|
{
|
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking:true))
|
|
|
|
|
{
|
2019-02-22 20:37:53 -05:00
|
|
|
|
return await ctx.EFMeta
|
2019-02-22 20:06:51 -05:00
|
|
|
|
.Where(_meta => _meta.Key == metaKey)
|
|
|
|
|
.Where(_meta => _meta.ClientId == client.ClientId)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
2018-02-14 14:01:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 20:06:51 -05:00
|
|
|
|
public static void AddRuntimeMeta(Func<int, Task<List<ProfileMeta>>> metaAction)
|
|
|
|
|
{
|
|
|
|
|
_metaActions.Add(metaAction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<List<ProfileMeta>> GetRuntimeMeta(int clientId)
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
|
|
|
|
var meta = new List<ProfileMeta>();
|
2019-02-22 20:06:51 -05:00
|
|
|
|
|
|
|
|
|
foreach (var action in _metaActions)
|
|
|
|
|
{
|
2018-02-14 14:01:26 -05:00
|
|
|
|
meta.AddRange(await action(clientId));
|
2019-02-22 20:06:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 14:01:26 -05:00
|
|
|
|
return meta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|