roadie/Roadie.Api.Services/LookupService.cs

167 lines
5.8 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
2018-12-22 20:33:23 +00:00
using Roadie.Library;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
2019-11-17 14:10:17 +00:00
using Roadie.Library.Data.Context;
2018-12-22 20:33:23 +00:00
using Roadie.Library.Encoding;
2019-06-30 22:14:36 +00:00
using Roadie.Library.Enums;
2018-12-22 20:33:23 +00:00
using Roadie.Library.Models;
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using data = Roadie.Library.Data;
namespace Roadie.Api.Services
{
/// <summary>
2019-06-30 22:14:36 +00:00
/// Returns lookups (or dictionaries) of various allowable values for a given Type
2018-12-22 20:33:23 +00:00
/// </summary>
public class LookupService : ServiceBase, ILookupService
{
public const string CreditCategoriesCacheKey = "urn:creditCategories";
2018-12-22 20:33:23 +00:00
public LookupService(IRoadieSettings configuration,
2019-06-30 22:14:36 +00:00
IHttpEncoder httpEncoder,
IHttpContext httpContext,
2019-11-17 14:10:17 +00:00
IRoadieDbContext dbContext,
2019-06-30 22:14:36 +00:00
ICacheManager cacheManager,
ILogger<PlaylistService> logger)
2018-12-22 20:33:23 +00:00
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
{
}
2018-12-24 19:40:49 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> ArtistTypes()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(ArtistType)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2018-12-24 19:40:49 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> BandStatus()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(BandStatus)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2018-12-24 19:40:49 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> BookmarkTypes()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(BookmarkType)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2018-12-24 19:40:49 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> CollectionTypes()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(CollectionType)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2018-12-24 19:40:49 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> LibraryStatus()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(LibraryStatus)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2019-01-08 22:40:26 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> QueMessageTypes()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(QueMessageType)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2019-01-08 22:40:26 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> ReleaseTypes()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(ReleaseType)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
2019-01-08 22:40:26 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> RequestStatus()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(RequestStatus)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
public async Task<OperationResult<IEnumerable<DataToken>>> CreditCategories()
{
var sw = Stopwatch.StartNew();
var data = await CacheManager.GetAsync(CreditCategoriesCacheKey, async () =>
{
return (await DbContext.CreditCategory.ToListAsync()).Select(x => new DataToken
{
Value = x.RoadieId.ToString(),
Text = x.Name
}).ToArray();
}, CacheManagerBase.SystemCacheRegionUrn);
return new OperationResult<IEnumerable<DataToken>>
{
Data = data,
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
};
}
2019-01-08 22:40:26 +00:00
public Task<OperationResult<IEnumerable<DataToken>>> Status()
2018-12-22 20:33:23 +00:00
{
var sw = Stopwatch.StartNew();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<IEnumerable<DataToken>>
2018-12-22 20:33:23 +00:00
{
2019-06-30 22:14:36 +00:00
Data = EnumToDataTokens(typeof(Statuses)),
2018-12-22 20:33:23 +00:00
IsSuccess = true,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-12-22 20:33:23 +00:00
}
private IEnumerable<DataToken> EnumToDataTokens(Type ee)
{
var result = new List<DataToken>();
foreach (var ls in Enum.GetValues(ee))
result.Add(new DataToken
{
Text = ls.ToString(),
Value = ((short)ls).ToString()
});
return result.OrderBy(x => x.Text);
}
}
}