using DigitalData.Core.DTO; using Microsoft.AspNetCore.Mvc; using System.Text; namespace DigitalData.Core.API { /// /// Provides extension methods for controllers in ASP.NET Core applications. /// This class is designed to enhance the functionality of , , /// and their related results such as . /// public static class ControllerExtensions { /// /// 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. /// /// The ViewResult to which ViewData will be added or updated. /// The key in the ViewData dictionary where the value will be stored. /// The value to be stored in the ViewData dictionary. /// The same ViewResult object with updated ViewData, allowing for additional chained operations. public static ViewResult WithData(this ViewResult viewResult, string index, object value) { viewResult.ViewData[index] = value; return viewResult; } /// /// Returns an ObjectResult representing an internal server error (status code 500) with optional exception and message details. /// /// The ControllerBase instance representing the controller. /// Optional. The exception that occurred, if any. /// Optional. A custom error message to include in the response. /// /// Optional. A custom error message key to include in the response. /// An ObjectResult representing an internal server error (status code 500). 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); } /// /// Returns an ObjectResult representing an internal server error (status code 500) with optional exception and message details. /// /// The ControllerBase instance representing the controller. /// Optional. A custom error resul to include in the response. /// An ObjectResult representing an internal server error (status code 500). public static ObjectResult InnerServiceError(this ControllerBase controllerBase, Result? result = null) => controllerBase.StatusCode(500, result); } }