139 lines
5.4 KiB
C#
139 lines
5.4 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
|
|
namespace DigitalData.Core.DTO
|
|
{
|
|
public static class DTOExtensions
|
|
{
|
|
public static T Message<T>(this T result, string message) where T : Result
|
|
{
|
|
result.Messages.Add(message);
|
|
return result;
|
|
}
|
|
|
|
public static T Message<T>(this T result, params string[] messages) where T : Result
|
|
{
|
|
result.Messages.AddRange(messages);
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, Notice notice) where T : Result
|
|
{
|
|
result.Notices.Add(notice);
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, params Notice[] notices) where T : Result
|
|
{
|
|
result.Notices.AddRange(notices);
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, LogLevel level, params Enum[] flags) where T : Result
|
|
{
|
|
var notices = flags.Select(flag => new Notice()
|
|
{
|
|
Flag = flag,
|
|
Level = level
|
|
});
|
|
result.Notices.AddRange(notices);
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, LogLevel level, Enum flag, params string[] messages) where T : Result
|
|
{
|
|
result.Notices.Add(new Notice()
|
|
{
|
|
Flag = flag,
|
|
Level = level,
|
|
Messages = messages.ToList()
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, LogLevel level, params string[] messages) where T : Result
|
|
{
|
|
result.Notices.Add(new Notice()
|
|
{
|
|
Flag = null,
|
|
Level = level,
|
|
Messages = messages.ToList()
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public static I Then<I>(this Result result, Func<I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> ThenAsync<I>(this Result result, Func<Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static I Then<T, I>(this DataResult<T> result, Func<T, I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> ThenAsync<T, I>(this DataResult<T> result, Func<T, Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> ThenAsync<I>(this Task<Result> tResult, Func<I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
Result result = await tResult;
|
|
return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> ThenAsync<I>(this Task<Result> tResult, Func<Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
Result result = await tResult;
|
|
return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> ThenAsync<T, I>(this Task<DataResult<T>> tResult, Func<T, I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
DataResult<T> result = await tResult;
|
|
return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> ThenAsync<T, I>(this Task<DataResult<T>> tResult, Func<T, Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
{
|
|
DataResult<T> result = await tResult;
|
|
return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static string Join<T>(this IEnumerable<T> values, string start = "", string seperator = ". ", string end = ".")
|
|
=> new StringBuilder(start).Append(string.Join(seperator, values)).Append(end).ToString();
|
|
|
|
public static void LogNotice(this ILogger logger, IEnumerable<Notice> notices, string start = ": ", string seperator = ". ", string end = ".\n")
|
|
{
|
|
foreach(LogLevel level in Enum.GetValues(typeof(LogLevel)))
|
|
{
|
|
var logNotices = notices.Where(n => n.Level == level);
|
|
|
|
if (!logNotices.Any())
|
|
continue;
|
|
|
|
var sb = new StringBuilder();
|
|
foreach(Notice notice in logNotices)
|
|
{
|
|
if (notice.Flag is not null)
|
|
sb.Append(notice.Flag);
|
|
|
|
if (notice.Messages.Any())
|
|
sb.Append(start).Append(string.Join(seperator, notice.Messages)).AppendLine(end);
|
|
else sb.Append(end);
|
|
}
|
|
logger.Log(level, sb.ToString());
|
|
}
|
|
}
|
|
|
|
public static void LogNotice(this ILogger logger, Result result, string start = ": ", string seperator = ". ", string end = ".\n")
|
|
=> logger.LogNotice(notices: result.Notices, start: start, seperator: seperator, end: end);
|
|
}
|
|
} |