sync full source from private myoffice
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
namespace MyOffice.Core;
|
||||
|
||||
public class Error
|
||||
{
|
||||
private Exception? _exception;
|
||||
|
||||
public Error(string message)
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
|
||||
public Error(Exception exception): this(exception.GetType().Name)
|
||||
{
|
||||
_exception = exception;
|
||||
}
|
||||
|
||||
public Error(string message, Exception exception): this(message)
|
||||
{
|
||||
_exception = exception;
|
||||
}
|
||||
|
||||
public string Message { get; private set;}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
namespace MyOffice.Core;
|
||||
|
||||
public enum GeneralExecStatus
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
forbidden
|
||||
}
|
||||
|
||||
public class Exec<TResult> : Exec<TResult, GeneralExecStatus>
|
||||
where TResult : class
|
||||
{
|
||||
public Exec(TResult result, GeneralExecStatus status) : base(result, status)
|
||||
{
|
||||
}
|
||||
|
||||
public Exec(GeneralExecStatus status) : base(status)
|
||||
{
|
||||
}
|
||||
|
||||
public Exec() : base(GeneralExecStatus.success)
|
||||
{
|
||||
}
|
||||
|
||||
public new Exec<TResult> Set(GeneralExecStatus status)
|
||||
{
|
||||
Status = status;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public new Exec<TResult> Set(TResult? result)
|
||||
{
|
||||
Result = result;
|
||||
Status = result == null ? GeneralExecStatus.failure : GeneralExecStatus.success;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public class Exec<TResult, TStatus>
|
||||
where TResult : class
|
||||
where TStatus : Enum
|
||||
{
|
||||
public static Exec<TResult, TStatus> Start(TResult result, TStatus status)
|
||||
{
|
||||
return new Exec<TResult, TStatus>(result, status);
|
||||
}
|
||||
|
||||
public static Exec<TResult, TStatus> Start(TStatus status)
|
||||
{
|
||||
return new Exec<TResult, TStatus>(status);
|
||||
}
|
||||
|
||||
public static Exec<TResult, GeneralExecStatus> StartSuccess()
|
||||
{
|
||||
return new Exec<TResult, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
}
|
||||
|
||||
public static Exec<TResult, GeneralExecStatus> StartFailure()
|
||||
{
|
||||
return new Exec<TResult, GeneralExecStatus>(GeneralExecStatus.failure);
|
||||
}
|
||||
|
||||
public static Exec<TResult, GeneralExecStatus> StartNotFound()
|
||||
{
|
||||
return new Exec<TResult, GeneralExecStatus>(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
public Exec(TResult result, TStatus status)
|
||||
{
|
||||
Result = result;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public Exec(TStatus status)
|
||||
{
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public TResult? Result { get; internal set; }
|
||||
public TStatus Status { get; internal set; }
|
||||
|
||||
public Exec<TResult, TStatus> Set(TResult result, TStatus status)
|
||||
{
|
||||
Result = result;
|
||||
Status = status;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Exec<TResult, TStatus> Set(TStatus status)
|
||||
{
|
||||
Status = status;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Exec<TResult, TStatus> Set(TResult result)
|
||||
{
|
||||
Result = result;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace MyOffice.Core.Extensions;
|
||||
|
||||
public static class BoolExtensions
|
||||
{
|
||||
public static string ToLowerCase(this bool value)
|
||||
{
|
||||
return value.ToString().ToLower();
|
||||
}
|
||||
|
||||
public static string? ToLowerCase(this bool? value)
|
||||
{
|
||||
return value == null ? null : value.ToString()?.ToLower();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace MyOffice.Core.Extensions;
|
||||
|
||||
using System.Security.Claims;
|
||||
|
||||
public static class ClaimsExtensions
|
||||
{
|
||||
public static string? GetValue(this IEnumerable<Claim> claims, string type)
|
||||
{
|
||||
return claims.FirstOrDefault(x => x.Type.Equals(type, StringComparison.OrdinalIgnoreCase))?.Value;
|
||||
}
|
||||
|
||||
public static string? GetValue(this IEnumerable<Claim> claims, string[] types)
|
||||
{
|
||||
return claims.FirstOrDefault(x => types.Any(z => x.Type.Equals(z, StringComparison.OrdinalIgnoreCase)))?.Value;
|
||||
}
|
||||
|
||||
public static string? GetEmail(this IEnumerable<Claim> claims)
|
||||
{
|
||||
return GetValue(claims, new[] { ClaimTypes.Email, "email" });
|
||||
}
|
||||
|
||||
public static string? GetNameIdentifier(this IEnumerable<Claim> claims)
|
||||
{
|
||||
return GetValue(claims, ClaimTypes.NameIdentifier);
|
||||
}
|
||||
|
||||
public static string? GetSID(this IEnumerable<Claim> claims)
|
||||
{
|
||||
return GetValue(claims, "sid");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace MyOffice.Core.Extensions;
|
||||
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
public static DateTime StartOfDay(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.Date;
|
||||
}
|
||||
|
||||
public static DateTime EndOfDay(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
public static DateTime ToUtc(this DateTime dateTime)
|
||||
{
|
||||
return new DateTime(dateTime.Ticks, DateTimeKind.Utc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace MyOffice.Core.Extensions;
|
||||
|
||||
public static class GuidExtensions
|
||||
{
|
||||
public static bool EqualsString(this Guid guid, string str)
|
||||
{
|
||||
return guid.ToString().EqualsIgnoreCase(str);
|
||||
}
|
||||
|
||||
public static string ToShort(this Guid guid)
|
||||
{
|
||||
return guid.ToString("N");
|
||||
}
|
||||
|
||||
public static string? ToShort(this Guid? guid)
|
||||
{
|
||||
return guid?.ToString("N");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
namespace MyOffice.Core.Extensions;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsMissing(this string? str)
|
||||
{
|
||||
return str == null || string.IsNullOrEmpty(str) || string.IsNullOrEmpty(str.Trim());
|
||||
}
|
||||
|
||||
public static bool IsPresent(this string? str)
|
||||
{
|
||||
return !IsMissing(str);
|
||||
}
|
||||
|
||||
public static string? NullIfEmpty(this string? str)
|
||||
{
|
||||
return IsMissing(str) ? null : str;
|
||||
}
|
||||
|
||||
public static bool EqualsIgnoreCase(this string? str, string? value)
|
||||
{
|
||||
return str != null && str.Equals(value, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public static string? SafeTrim(this string? str)
|
||||
{
|
||||
return str == null ? str : str!.Trim();
|
||||
}
|
||||
|
||||
public static string? MaxOf(this string? str, int maxLength)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (str.Length < maxLength)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return str.Substring(0, maxLength);
|
||||
}
|
||||
|
||||
public static bool AsBool(this string? str, bool defaultValue = false)
|
||||
{
|
||||
return true.ToString().Equals(str, StringComparison.OrdinalIgnoreCase) || defaultValue;
|
||||
}
|
||||
|
||||
public static Guid AsGuid(this string str)
|
||||
{
|
||||
return Guid.Parse(str);
|
||||
}
|
||||
|
||||
public static Guid? AsGuidNull(this string? str)
|
||||
{
|
||||
if (Guid.TryParse(str, out var guid))
|
||||
{
|
||||
return guid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace MyOffice.Core.Helpers
|
||||
{
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public static class JsonHelper
|
||||
{
|
||||
public static string ToJson(this object entity, JsonSerializerOptions? options = null)
|
||||
{
|
||||
options ??= new JsonSerializerOptions
|
||||
{
|
||||
MaxDepth = 0,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
||||
WriteIndented = true,
|
||||
};
|
||||
return JsonSerializer.Serialize(entity, options);
|
||||
}
|
||||
}
|
||||
|
||||
/*public class CustomIgnoreReferenceHandler : ReferenceHandler
|
||||
{
|
||||
//public CustomIgnoreReferenceHandler() => HandlingStrategy = ReferenceHandlingStrategy.IgnoreCycles;
|
||||
public CustomIgnoreReferenceHandler()
|
||||
{
|
||||
new HandlingStrategy { } = true;
|
||||
}
|
||||
|
||||
public override ReferenceResolver CreateResolver() => new IgnoreReferenceResolver();
|
||||
}*/
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace MyOffice.Core.Helpers;
|
||||
|
||||
using MyOffice.Core.Extensions;
|
||||
|
||||
public class RandomizationHelper
|
||||
{
|
||||
public static string Generate(int length)
|
||||
{
|
||||
var result = "";
|
||||
|
||||
while(result.Length < length)
|
||||
{
|
||||
result += Guid.NewGuid().ToShort();
|
||||
}
|
||||
|
||||
return result.Substring(0, length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace MyOffice.Core.Identity;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
public interface IExternalProviderValidator
|
||||
{
|
||||
string Provider { get; }
|
||||
public bool IsConfigured { get; }
|
||||
|
||||
Task<ExternalProviderValidatorResult> ValidateAsync(string token);
|
||||
}
|
||||
|
||||
public class ExternalProviderValidatorResult
|
||||
{
|
||||
public static ExternalProviderValidatorResult Failed()
|
||||
{
|
||||
return new ExternalProviderValidatorResult();
|
||||
}
|
||||
|
||||
public static ExternalProviderValidatorResult Success(string email, bool emailVerified, string externalId,
|
||||
string? fullName)
|
||||
{
|
||||
return new ExternalProviderValidatorResult(email, emailVerified, externalId, fullName);
|
||||
}
|
||||
|
||||
public ExternalProviderValidatorResult()
|
||||
{
|
||||
IsSuccessed = false;
|
||||
}
|
||||
|
||||
public ExternalProviderValidatorResult(string email, bool emailVerified, string externalId, string? fullName) :
|
||||
this()
|
||||
{
|
||||
Email = email;
|
||||
EmailVerified = emailVerified;
|
||||
IsSuccessed = true;
|
||||
ExternalId = externalId;
|
||||
FullName = fullName;
|
||||
}
|
||||
|
||||
public bool IsSuccessed { get; }
|
||||
public string? Email { get; }
|
||||
public bool EmailVerified { get; }
|
||||
public string? ExternalId { get; }
|
||||
public string? FullName { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MyOffice.Core;
|
||||
|
||||
public interface IDataModel
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MyOffice.Core;
|
||||
|
||||
public interface IDataModelDto<TSource> where TSource : IDataModel
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user