using Microsoft.Extensions.Logging; using System.Text; namespace DigitalData.Core.DTO { public static class DTOExtensions { public static T Message(this T result, string message, int? index = null) where T : Result { if(index is null) result.Messages.Add(message); else result.Messages.Insert(index.Value, message); return result; } public static T Message(this T result, int? index = null, params string[] messages) where T : Result { if(index is null) result.Messages.AddRange(messages); else result.Messages.InsertRange(index.Value, messages); return result; } public static T Message(this T result, IEnumerable messages, int? index = null) where T : Result { if(index is null) result.Messages.AddRange(messages); else result.Messages.InsertRange(index.Value, messages); return result; } public static T Notice(this T result, Notice notice) where T : Result { result.Notices.Add(notice); return result; } public static T Notice(this T result, IEnumerable notices) where T : Result { result.Notices.AddRange(notices); return result; } public static T Notice(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(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(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 bool HasFlag(this IEnumerable notices, Enum flag) => notices.Any(n => n.Flag?.ToString() == flag.ToString()); public static bool HasAnyFlag(this IEnumerable notices, params Enum[] flags) => flags.Any(f => notices.HasFlag(f)); public static I Then(this Result result, Func Success, Func, List, I> Fail) { return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices); } public static async Task ThenAsync(this Result result, Func> SuccessAsync, Func, List, I> Fail) { return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices); } public static I Then(this DataResult result, Func Success, Func, List, I> Fail) { return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices); } public static async Task ThenAsync(this DataResult result, Func> SuccessAsync, Func, List, I> Fail) { return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices); } public static async Task ThenAsync(this Task tResult, Func Success, Func, List, I> Fail) { Result result = await tResult; return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices); } public static async Task ThenAsync(this Task tResult, Func> SuccessAsync, Func, List, I> Fail) { Result result = await tResult; return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices); } public static async Task ThenAsync(this Task> tResult, Func Success, Func, List, I> Fail) { DataResult result = await tResult; return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices); } public static async Task ThenAsync(this Task> tResult, Func> SuccessAsync, Func, List, I> Fail) { DataResult result = await tResult; return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices); } public static string Join(this IEnumerable 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 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); public static bool IsRight(this DataResult bResult) => bResult.Data; public static bool IsWrong(this DataResult bResult) => !bResult.Data; } }