using System;
using System.Data;
using Dapper;
namespace GB5Shared.DateConverter
{
///
/// Dapper-side counterpart to DateOnlyJsonConverter — maps SQL datetime/date
/// columns to DateOnly properties and back. Without this, Dapper throws
/// "Error parsing column X (... - DateTime)" for any DateOnly-typed property.
///
public class DateOnlyTypeHandler : SqlMapper.TypeHandler
{
public override void SetValue(IDbDataParameter parameter, DateOnly value)
=> parameter.Value = value.ToDateTime(TimeOnly.MinValue);
public override DateOnly Parse(object value)
=> DateOnly.FromDateTime((DateTime)value);
}
public class NullableDateOnlyTypeHandler : SqlMapper.TypeHandler
{
public override void SetValue(IDbDataParameter parameter, DateOnly? value)
=> parameter.Value = value.HasValue
? value.Value.ToDateTime(TimeOnly.MinValue)
: (object)DBNull.Value;
public override DateOnly? Parse(object value)
=> value == null || value is DBNull ? null : DateOnly.FromDateTime((DateTime)value);
}
}