213 lines
8.1 KiB
C#
Raw Blame History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using AutoMapper;
using gehGassi.Domain.Localization;
using gehGassi.Web.Helper;
using gehGassi.Web.Services;
using Microsoft.AspNetCore.Hosting;
using Newtonsoft.Json;
namespace gehGassi.Web.Mapper
{
#region UTC To Local
public class UtcToLocalDateTimeResolver : IMemberValueResolver<object, object, DateTime, DateTime>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public UtcToLocalDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public DateTime Resolve(object source, object destination, DateTime sourceMember, DateTime destMember, ResolutionContext context)
{
return TimeZoneHelper.ConvertToLocal(sourceMember, _currentTimeZoneService.GetAsync().Result);
}
}
public class UtcToLocalDateTimeStringResolver : IMemberValueResolver<object, object, DateTime, string>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public UtcToLocalDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public string Resolve(object source, object destination, DateTime sourceMember, string destMember, ResolutionContext context)
{
return TimeZoneHelper.ConvertToLocal(sourceMember, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture);
}
}
public class NullableUtcToLocalDateTimeResolver : IMemberValueResolver<object, object, DateTime?, DateTime?>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public NullableUtcToLocalDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public DateTime? Resolve(object source, object destination, DateTime? sourceMember, DateTime? destMember, ResolutionContext context)
{
if (sourceMember.HasValue)
return TimeZoneHelper.ConvertToLocal(sourceMember.Value, _currentTimeZoneService.GetAsync().Result);
return null;
}
}
public class NullableUtcToLocalDateTimeStringResolver : IMemberValueResolver<object, object, DateTime?, string>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public NullableUtcToLocalDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public string Resolve(object source, object destination, DateTime? sourceMember, string destMember, ResolutionContext context)
{
if (sourceMember.HasValue)
return TimeZoneHelper.ConvertToLocal(sourceMember.Value, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture);
return string.Empty;
}
}
#endregion
#region Local to UTC
public class LocalToUtcDateTimeResolver : IMemberValueResolver<object, object, DateTime, DateTime>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public LocalToUtcDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public DateTime Resolve(object source, object destination, DateTime sourceMember, DateTime destMember, ResolutionContext context)
{
return TimeZoneHelper.ConvertToUtc(sourceMember, _currentTimeZoneService.GetAsync().Result);
}
}
public class LocalToUtcDateTimeStringResolver : IMemberValueResolver<object, object, DateTime, string>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public LocalToUtcDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public string Resolve(object source, object destination, DateTime sourceMember, string destMember, ResolutionContext context)
{
return TimeZoneHelper.ConvertToUtc(sourceMember, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture);
}
}
public class NullableLocalToUtcDateTimeResolver : IMemberValueResolver<object, object, DateTime?, DateTime?>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public NullableLocalToUtcDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public DateTime? Resolve(object source, object destination, DateTime? sourceMember, DateTime? destMember, ResolutionContext context)
{
if (sourceMember.HasValue)
return TimeZoneHelper.ConvertToUtc(sourceMember.Value, _currentTimeZoneService.GetAsync().Result);
return null;
}
}
public class NullableLocalToUtcDateTimeStringResolver : IMemberValueResolver<object, object, DateTime?, string>
{
private readonly ICurrentTimeZoneService _currentTimeZoneService;
public NullableLocalToUtcDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService)
{
_currentTimeZoneService = currentTimeZoneService;
}
public string Resolve(object source, object destination, DateTime? sourceMember, string destMember, ResolutionContext context)
{
if (sourceMember.HasValue)
return TimeZoneHelper.ConvertToUtc(sourceMember.Value, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture);
return string.Empty;
}
}
#endregion
#region Documents / Files
public class FilenameToIconResolver : IMemberValueResolver<object, object, string, string>
{
private readonly IWebHostEnvironment _hostingEnvironment;
public FilenameToIconResolver(IWebHostEnvironment webHostEnvironment)
{
_hostingEnvironment = webHostEnvironment;
}
public string Resolve(object source, object destination, string sourceMember, string destMember, ResolutionContext context)
{
var fileName = sourceMember;
var result = "/images/FileExtensions/_blank.png";
if (!string.IsNullOrWhiteSpace(fileName))
{
fileName = fileName.Replace(@"\", "/");
var extension = fileName.Split('/').Last();
extension = extension.Split('.').Last();
var imageUrl = "/images/FileExtensions/" + extension + ".png";
var absolutePath = _hostingEnvironment.WebRootPath + imageUrl;
if (!System.IO.File.Exists(absolutePath))
{
imageUrl = "/images/FileExtensions/_blank.png";
}
result = imageUrl;
}
return result;
}
}
#endregion
#region Localized
/// <summary>
/// Resolver der aus einem ILocalizable die <20>bersetzung in der aktuellen Sprache zur<75>ckgibt
/// </summary>
public class LocalizedResolver : IMemberValueResolver<object, object, string, string>
{
public string Resolve(object source, object destination, string sourceMember, string destMember, ResolutionContext context)
{
if (source is ILocalizable helper)
{
try
{
var language = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.ToUpper();
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(sourceMember);
if (dictionary != null)
{
if (dictionary.ContainsKey(language))
return dictionary[language];
}
}
catch { }
}
return string.Empty;
}
}
#endregion
}