using System.Text.Json; using Core_Archi.Exceptions; namespace Core_Archi.Helpers { public class DataExtractor { public static string GetRequiredString( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new BadRequestException($"Missing required field '{key}'."); if (element.ValueKind != JsonValueKind.String) return ""; //throw new BadRequestException($"Field '{key}' must be a string."); var value = element.GetString(); if (string.IsNullOrWhiteSpace(value)) throw new BadRequestException($"Field '{key}' cannot be empty."); return value; } // ===================== INT ===================== public static int GetRequiredInt( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new BadRequestException($"Missing required field '{key}'."); if (element.TryGetInt32(out var value)) return value; throw new BadRequestException($"Field '{key}' must be a valid integer."); } // ===================== FLOAT ===================== public static float GetRequiredFloat( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new BadRequestException($"Missing required field '{key}'."); if (element.TryGetSingle(out var value)) return value; // Allow numeric strings: "12.5" if (element.ValueKind == JsonValueKind.String && float.TryParse(element.GetString(), out value)) { return value; } throw new BadRequestException($"Field '{key}' must be a valid float."); } // ===================== DECIMAL ===================== public static decimal GetRequiredDecimal( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new ArgumentException($"Missing required field '{key}'."); if (element.TryGetDecimal(out var value)) return value; // Allow numeric strings: "12.5" if (element.ValueKind == JsonValueKind.String && decimal.TryParse(element.GetString(), out value)) { return value; } throw new ArgumentException($"Field '{key}' must be a valid decimal."); } // ===================== BOOLEAN ===================== public static bool GetRequiredBool( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new BadRequestException($"Missing required field '{key}'."); if (element.ValueKind == JsonValueKind.True) return true; if (element.ValueKind == JsonValueKind.False) return false; // Allow string values: "true" / "false" if (element.ValueKind == JsonValueKind.String && bool.TryParse(element.GetString(), out var value)) { return value; } throw new BadRequestException($"Field '{key}' must be a valid boolean."); } // ===================== GUID ===================== public static Guid GetRequiredGuid( Dictionary data, string key) { var value = GetRequiredString(data, key); if (!Guid.TryParse(value, out var guid)) throw new BadRequestException($"Field '{key}' must be a valid GUID."); return guid; } // ===================== DATETIME ===================== public static DateTime GetRequiredDateTime( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new BadRequestException($"Missing required field '{key}'."); if (element.ValueKind == JsonValueKind.Null) throw new BadRequestException($"Field '{key}' cannot be null."); // ISO 8601 string if (element.ValueKind == JsonValueKind.String && DateTime.TryParse(element.GetString(), out var date)) { return date; } // Native JSON DateTime if (element.TryGetDateTime(out date)) { return date; } throw new BadRequestException($"Field '{key}' must be a valid DateTime."); } // ===================== DATEONLY ===================== public static DateOnly GetRequiredDateOnly( Dictionary data, string key) { if (!data.TryGetValue(key, out var element)) throw new BadRequestException($"Missing required field '{key}'."); if (element.ValueKind == JsonValueKind.Null) throw new BadRequestException($"Field '{key}' cannot be null."); DateTime date; // ISO 8601 string (e.g., "2025-02-25") if (element.ValueKind == JsonValueKind.String && DateTime.TryParse(element.GetString(), out date)) { return DateOnly.FromDateTime(date); } if (element.TryGetDateTime(out date)) { return DateOnly.FromDateTime(date); } throw new BadRequestException($"Field '{key}' must be a valid date."); } // ===================== USER ID FROM HttpContext ===================== public static Guid GetUserIdFromContext(HttpContext httpContext) { if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); var userIdFromContext = httpContext.Items["UserId"]?.ToString() ?? httpContext.User?.FindFirst("UserId")?.Value; if (string.IsNullOrWhiteSpace(userIdFromContext) || !Guid.TryParse(userIdFromContext, out var userId)) { throw new UnauthorizedAccessException("User is not authenticated. Please login and send cookie."); } return userId; } } }