refactor: Projektdateien migriert. Cloud-NuGet-Pakete durch lokale NuGet-Projekte ersetzt.

This commit is contained in:
Developer 02
2024-08-01 18:44:39 +02:00
parent 0d82f7af6f
commit 62ddd4873f
206 changed files with 10927 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace HRD.WebApi.Helpers
{
public static class AttributeReader
{
//Get DB Table Name
public static string GetTableName<T>(DbContext context) where T : class
{
// We need dbcontext to access the models
var models = context.Model;
// Get all the entity types information
var entityTypes = models.GetEntityTypes();
// T is Name of class
var entityTypeOfT = entityTypes.First(t => t.ClrType == typeof(T));
var tableNameAnnotation = entityTypeOfT.GetAnnotation("Relational:TableName");
var TableName = tableNameAnnotation.Value.ToString();
return TableName;
}
}
}

View File

@@ -0,0 +1,24 @@
using Microsoft.Extensions.DependencyInjection;
namespace HRD.WebApi.Helpers
{
public static class CorsExtension
{
public static void AddCustomCors(this IServiceCollection services, string policyName)
{
services.AddCors(options =>
{
options.AddPolicy(policyName,
builder =>
{
builder
.AllowCredentials()
//.AllowAnyOrigin()
.SetIsOriginAllowed(origin => true)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
}
}
}

View File

@@ -0,0 +1,39 @@
using HRD.AppLogger;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
namespace HRD.WebApi.Helpers
{
public static class ExceptionExtension
{
public static void AddCustomExceptionHandling(this IApplicationBuilder app)
{
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "application/json";
var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
if (errorFeature != null)
{
HttpErrorDetails customError = new HttpErrorDetails
{
URI = context.Request.Path.ToUriComponent(),
Status = context.Response.StatusCode,
Detail = errorFeature.Error.Message
};
ILoggerManager logger = new LoggerManager();
logger.LogException(errorFeature.Error, null, customError.URI);
await context.Response.WriteAsync(customError.ToJsonString()).ConfigureAwait(false);
}
});
});
}
}
}

View File

@@ -0,0 +1,69 @@
using HRD.AppLogger;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace HRD.WebApi.Helpers
{
public class HttpErrorDetails : ValidationProblemDetails
{
public HttpErrorDetails()
{
}
public HttpErrorDetails(ActionContext context)
{
Title = "Invalid arguments to the API";
Detail = "The inputs supplied to the API are invalid";
Status = StatusCodes.Status400BadRequest;
Type = context.HttpContext.TraceIdentifier;
URI = context.HttpContext.Request.Path.ToUriComponent();
ConstructErrorMessages(context);
}
public string URI { get; set; }
public string ToJsonString()
{
return JsonConvert.Serialize(this);
}
private void ConstructErrorMessages(ActionContext context)
{
foreach (var keyModelStatePair in context.ModelState)
{
var key = keyModelStatePair.Key;
var errors = keyModelStatePair.Value.Errors;
if (errors != null && errors.Count > 0)
{
ILoggerManager logger = new LoggerManager();
if (errors.Count == 1)
{
var errorMessage = GetErrorMessage(errors[0]);
base.Errors.Add(key, new[] { errorMessage });
logger.LogError(errorMessage, null, URI);
Detail = errorMessage;
}
else
{
var errorMessages = new string[errors.Count];
for (var i = 0; i < errors.Count; i++)
{
errorMessages[i] = GetErrorMessage(errors[i]);
logger.LogError(errorMessages[i], null, URI);
if (i == 0) { Detail = errorMessages[i]; }
}
base.Errors.Add(key, errorMessages);
}
}
}
}
private string GetErrorMessage(ModelError error)
{
return string.IsNullOrEmpty(error.ErrorMessage) ?
"The input was not valid." : error.ErrorMessage;
}
}
}

View File

@@ -0,0 +1,42 @@
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
namespace HRD.WebApi.Helpers
{
public static class JsonConvert
{
public static string Serialize<T>(T obj)
{
using (var stream = new MemoryStream())
{
GetSerializer<T>().WriteObject(stream, obj);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
public static T Deserialize<T>(string json)
{
using (var stream = new MemoryStream())
{
using (var writer = new StreamWriter(stream))
{
writer.Write(json);
writer.Flush();
stream.Position = 0;
return (T)GetSerializer<T>().ReadObject(stream);
}
}
}
private static DataContractJsonSerializer GetSerializer<T>()
{
var settings = new DataContractJsonSerializerSettings
{
UseSimpleDictionaryFormat = true
};
return new DataContractJsonSerializer(typeof(T), settings);
}
}
}

View File

@@ -0,0 +1,40 @@
using HRD.WebApi.DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Data.Common;
namespace HRD.WebApi.Helpers
{
public static class ServerInfoHelper
{
public static ServerInfo GetServerInfo(WebApiBaseContext context)
{
ServerInfo serverInfo = new ServerInfo();
{
serverInfo.DatabaseName = context?.Database?.GetDbConnection()?.Database; ;
serverInfo.DatabaseServer = context?.Database?.GetDbConnection()?.DataSource;
serverInfo.Message = "Online";
using DbConnection connection = context.Database.GetDbConnection();
try
{
connection.Open();
serverInfo.DatabaseStatus = "Online";
serverInfo.Status = StatusCodes.Status200OK;
}
catch (Exception ex)
{
serverInfo.DatabaseStatus = "Offline";
serverInfo.Status = StatusCodes.Status500InternalServerError;
serverInfo.Message = ex.Message;
}
}
serverInfo.Server = Environment.MachineName;
serverInfo.Version = WebApiConfig.AssemblyVersion;
serverInfo.IsLive = WebApiConfig.IsLive;
serverInfo.ClientVersion = WebApiConfig.ClientVersion;
return serverInfo;
}
}
}