DeutschLernen/GermanApp/Shared/Models/ApiResponse.cs
Lasse Rune Hansen 4ee2b2568a feat(backend/infra): add health checks, CORS, Serilog logging, and exception middleware
- Add Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore package
- Add Serilog.AspNetCore and Serilog.Sinks.Console packages
- Configure Serilog with bootstrap logger and configuration
- Add Health Checks endpoint at /health with DbContext check
- Configure CORS with AllowAll policy for development
- Create ApiResponse, ApiErrorResponse, and PaginatedResponse models
- Create ExceptionMiddleware for global error handling
- Restructure Program.cs with try-catch-finally for proper logging

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-05-31 19:52:00 +02:00

47 lines
1.5 KiB
C#

namespace GermanApp.Shared.Models;
/// <summary>
/// Base API response wrapper for consistent API responses.
/// </summary>
/// <typeparam name="T">The data type of the response</typeparam>
public record ApiResponse<T>(T Data, string Message = "Success", bool Success = true)
{
/// <summary>
/// Creates a successful response.
/// </summary>
public static ApiResponse<T> Ok(T data, string message = "Success") =>
new ApiResponse<T>(data, message, true);
}
/// <summary>
/// API error response for consistent error handling.
/// </summary>
public record ApiErrorResponse(string Message, int StatusCode, string? Details = null)
{
public bool Success => false;
/// <summary>
/// Creates an error response.
/// </summary>
public static ApiErrorResponse Error(string message, int statusCode, string? details = null) =>
new ApiErrorResponse(message, statusCode, details);
}
/// <summary>
/// Paginated API response.
/// </summary>
/// <typeparam name="T">The data type of the items</typeparam>
public record PaginatedResponse<T>(IEnumerable<T> Items, int PageNumber, int PageSize, int TotalCount, int TotalPages)
{
/// <summary>
/// Creates a paginated response.
/// </summary>
public static PaginatedResponse<T> Create(IEnumerable<T> items, int pageNumber, int pageSize, int totalCount) =>
new PaginatedResponse<T>(
items,
pageNumber,
pageSize,
totalCount,
(int)Math.Ceiling(totalCount / (double)pageSize)
);
}