docker files fixes from liaqat
This commit is contained in:
22
src/Managing.Core/AsyncLazy.cs
Normal file
22
src/Managing.Core/AsyncLazy.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Managing.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// http://blogs.msdn.com/b/pfxteam/archive/2011/01/15/asynclazy-lt-t-gt.aspx
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class AsyncLazy<T> : Lazy<Task<T>>
|
||||
{
|
||||
public AsyncLazy(Func<T> valueFactory) :
|
||||
base(() => Task.Factory.StartNew(valueFactory))
|
||||
{ }
|
||||
|
||||
public AsyncLazy(Func<Task<T>> taskFactory) :
|
||||
base(() => Task.Factory.StartNew(() => taskFactory()).Unwrap())
|
||||
{ }
|
||||
|
||||
public TaskAwaiter<T> GetAwaiter() { return Value.GetAwaiter(); }
|
||||
}
|
||||
|
||||
}
|
||||
10
src/Managing.Core/DateHelpers.cs
Normal file
10
src/Managing.Core/DateHelpers.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Managing.Core;
|
||||
|
||||
public class DateHelpers
|
||||
{
|
||||
public static DateTime GetFromUnixTimestamp(int unixTimestamp)
|
||||
{
|
||||
var dat_Time = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
|
||||
return dat_Time.AddSeconds(unixTimestamp);
|
||||
}
|
||||
}
|
||||
20
src/Managing.Core/Managing.Core.csproj
Normal file
20
src/Managing.Core/Managing.Core.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Exceptions\**" />
|
||||
<EmbeddedResource Remove="Exceptions\**" />
|
||||
<None Remove="Exceptions\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
src/Managing.Core/MathHelpers.cs
Normal file
25
src/Managing.Core/MathHelpers.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Managing.Core
|
||||
{
|
||||
public static class MathHelpers
|
||||
{
|
||||
public static int GetDecimalPlaces(decimal n)
|
||||
{
|
||||
n = Math.Abs(n); //make sure it is positive.
|
||||
n -= (int)n; //remove the integer part of the number.
|
||||
var decimalPlaces = 0;
|
||||
|
||||
while (n > 0)
|
||||
{
|
||||
decimalPlaces++;
|
||||
n *= 10;
|
||||
n -= (int)n;
|
||||
}
|
||||
return decimalPlaces;
|
||||
}
|
||||
|
||||
public static int GetDecimalPlaces(string n)
|
||||
{
|
||||
return n.Split('.')[1].Length - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Managing.Api.WorkersExceptions;
|
||||
|
||||
public class GlobalErrorHandlingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
public GlobalErrorHandlingMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
{
|
||||
HttpStatusCode status;
|
||||
var exceptionType = exception.GetType();
|
||||
|
||||
if (exceptionType == typeof(Exception))
|
||||
{
|
||||
status = HttpStatusCode.InternalServerError;
|
||||
}
|
||||
else if (exceptionType == typeof(NotImplementedException))
|
||||
{
|
||||
status = HttpStatusCode.NotImplemented;
|
||||
}
|
||||
else if (exceptionType == typeof(UnauthorizedAccessException))
|
||||
{
|
||||
status = HttpStatusCode.Unauthorized;
|
||||
}
|
||||
else if (exceptionType == typeof(ArgumentException))
|
||||
{
|
||||
status = HttpStatusCode.Unauthorized;
|
||||
}
|
||||
else if (exceptionType == typeof(KeyNotFoundException))
|
||||
{
|
||||
status = HttpStatusCode.Unauthorized;
|
||||
}
|
||||
else
|
||||
{
|
||||
status = HttpStatusCode.InternalServerError;
|
||||
}
|
||||
|
||||
var message = exception.Message;
|
||||
var stackTrace = exception.StackTrace;
|
||||
var exceptionResult = JsonSerializer.Serialize(new { error = message, stackTrace });
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
context.Response.StatusCode = (int)status;
|
||||
return context.Response.WriteAsync(exceptionResult);
|
||||
}
|
||||
}
|
||||
66
src/Managing.Core/MiscExtensions.cs
Normal file
66
src/Managing.Core/MiscExtensions.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
namespace Managing.Core
|
||||
{
|
||||
public static class MiscExtensions
|
||||
{
|
||||
// Ex: collection.TakeLast(5);
|
||||
//public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int N)
|
||||
//{
|
||||
// return source.Skip(Math.Max(0, source.Count() - N));
|
||||
//}
|
||||
|
||||
// foreach(IEnumerable<User> batch in users.Batch(1000))
|
||||
public static IEnumerable<IEnumerable<T>> Batch<T>(
|
||||
this IEnumerable<T> source, int size)
|
||||
{
|
||||
T[] bucket = null;
|
||||
var count = 0;
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
if (bucket == null)
|
||||
bucket = new T[size];
|
||||
|
||||
bucket[count++] = item;
|
||||
|
||||
if (count != size)
|
||||
continue;
|
||||
|
||||
yield return bucket.Select(x => x);
|
||||
|
||||
bucket = null;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
// Return the last bucket with all remaining elements
|
||||
if (bucket != null && count > 0)
|
||||
{
|
||||
Array.Resize(ref bucket, count);
|
||||
yield return bucket.Select(x => x);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddItem<T>(this List<T> list, T item)
|
||||
{
|
||||
if (!list.Contains(item))
|
||||
{
|
||||
list.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static T ParseEnum<T>(string value)
|
||||
{
|
||||
return (T)Enum.Parse(typeof(T), value, true);
|
||||
}
|
||||
|
||||
public static string Base64Encode(string plainText)
|
||||
{
|
||||
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
|
||||
return System.Convert.ToBase64String(plainTextBytes);
|
||||
}
|
||||
public static string Base64Decode(string base64EncodedData)
|
||||
{
|
||||
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
|
||||
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/Managing.Core/ValueObject.cs
Normal file
40
src/Managing.Core/ValueObject.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace Managing.Core
|
||||
{
|
||||
public abstract class ValueObject
|
||||
{
|
||||
protected static bool EqualOperator(ValueObject left, ValueObject right)
|
||||
{
|
||||
if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return ReferenceEquals(left, null) || left.Equals(right);
|
||||
}
|
||||
|
||||
protected static bool NotEqualOperator(ValueObject left, ValueObject right)
|
||||
{
|
||||
return !(EqualOperator(left, right));
|
||||
}
|
||||
|
||||
protected abstract IEnumerable<object> GetEqualityComponents();
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null || obj.GetType() != GetType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var other = (ValueObject)obj;
|
||||
|
||||
return this.GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return GetEqualityComponents()
|
||||
.Select(x => x != null ? x.GetHashCode() : 0)
|
||||
.Aggregate((x, y) => x ^ y);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user