This commit reorganizes namespaces from `DigitalData.Core.Abstractions` and `DigitalData.Core.DTO` to `DigitalData.Core.Application.Interfaces` and `DigitalData.Core.Application.DTO`, improving maintainability and clarity. Updated using directives across multiple files to reflect the new structure, ensuring functionality remains intact. Project references in `DigitalData.Core.API.csproj` have been consolidated to include the new Application project. Introduced new classes and interfaces such as `BaseDTO`, `CookieConsentSettings`, `DataResult`, `Notice`, and `Result` to enhance data transfer and service result handling. Updated `IRepository`, `ICRUDRepository`, and `IEntityMapper` interfaces to facilitate CRUD operations and entity mapping. Added extension methods in `Extensions.cs` to improve repository usability. New interfaces for HTTP client services have been added, enhancing external API call handling. Overall, these changes reflect a significant restructuring aimed at improving organization and preparing for future development.
55 lines
3.1 KiB
C#
55 lines
3.1 KiB
C#
using DigitalData.Core.Application.DTO;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text;
|
|
|
|
namespace DigitalData.Core.API
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for controllers in ASP.NET Core applications.
|
|
/// This class is designed to enhance the functionality of <see cref="Controller"/>, <see cref="ControllerBase"/>,
|
|
/// and their related results such as <see cref="ViewResult"/>.
|
|
/// </summary>
|
|
public static class ControllerExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds or updates a value in the ViewData dictionary of a ViewResult. This method supports fluent chaining,
|
|
/// enabling cleaner and more readable modifications to ViewData.
|
|
/// </summary>
|
|
/// <param name="viewResult">The ViewResult to which ViewData will be added or updated.</param>
|
|
/// <param name="index">The key in the ViewData dictionary where the value will be stored.</param>
|
|
/// <param name="value">The value to be stored in the ViewData dictionary.</param>
|
|
/// <returns>The same ViewResult object with updated ViewData, allowing for additional chained operations.</returns>
|
|
public static ViewResult WithData(this ViewResult viewResult, string index, object? value)
|
|
{
|
|
viewResult.ViewData[index] = value;
|
|
return viewResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an ObjectResult representing an internal server error (status code 500) with optional exception and message details.
|
|
/// </summary>
|
|
/// <param name="controllerBase">The ControllerBase instance representing the controller.</param>
|
|
/// <param name="ex">Optional. The exception that occurred, if any.</param>
|
|
/// <param name="message">Optional. A custom error message to include in the response.</param>
|
|
/// /// <param name="messageKey">Optional. A custom error message key to include in the response.</param>
|
|
/// <returns>An ObjectResult representing an internal server error (status code 500).</returns>
|
|
public static ObjectResult InnerServiceError(this ControllerBase controllerBase, Exception? ex = null, string? message = null)
|
|
{
|
|
var sb = new StringBuilder();
|
|
if (ex is not null)
|
|
sb.AppendLine(ex.Message);
|
|
if (message is not null)
|
|
sb.AppendLine(message);
|
|
|
|
return controllerBase.StatusCode(500, sb.Length > 0 ? sb.ToString() : null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an ObjectResult representing an internal server error (status code 500) with optional exception and message details.
|
|
/// </summary>
|
|
/// <param name="controllerBase">The ControllerBase instance representing the controller.</param>
|
|
/// <param name="result">Optional. A custom error resul to include in the response.</param>
|
|
/// <returns>An ObjectResult representing an internal server error (status code 500).</returns>
|
|
public static ObjectResult InnerServiceError(this ControllerBase controllerBase, Result? result = null) => controllerBase.StatusCode(500, result);
|
|
}
|
|
} |