57 lines
2.0 KiB
C#
57 lines
2.0 KiB
C#
using PARR.Constants.Shortcodes;
|
||
using System.Text.Encodings.Web;
|
||
using System.Text.Json;
|
||
using System.Text.Unicode;
|
||
|
||
namespace PARR.DAL.Extensions
|
||
{
|
||
public static class GeneralExtesions
|
||
{
|
||
/// <summary>
|
||
/// Преобразует объект в Json, с учетом Cyrillic
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
public static string? ToJson(this object value)
|
||
{
|
||
if (value == null)
|
||
return null;
|
||
|
||
try
|
||
{
|
||
return JsonSerializer.Serialize(value, new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All, UnicodeRanges.Cyrillic) });
|
||
}
|
||
catch// (Exception ex)
|
||
{
|
||
// todo: писать лог??? или и так пойдет
|
||
return null;
|
||
}
|
||
}
|
||
|
||
|
||
public static DateTimeOffset EndOfDay(this DateTimeOffset date)
|
||
{
|
||
return new DateTimeOffset(date.Year, date.Month, date.Day, 23, 59, 59, 999, new TimeSpan(0));
|
||
}
|
||
|
||
public static DateTimeOffset StartOfDay(this DateTimeOffset date)
|
||
{
|
||
return new DateTimeOffset(date.Year, date.Month, date.Day, 0, 0, 0, 0, new TimeSpan(0));
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sourceString">Исходная строка, включающая переменные части вида %ШАБЛОН%</param>
|
||
/// <param name="shortcode">Код шаблона из PARR.Constants.ShortcodeEnum.ShortcodeEnum</param>
|
||
/// <param name="shortcodeValue">Итоговое строковое значение</param>
|
||
/// <returns></returns>
|
||
public static string ApplyShortcode(this string sourceString, ShortcodeEnum shortcode, string shortcodeValue)
|
||
{
|
||
var shortcodeName = Shortcodes.GetShortcodeName(shortcode);
|
||
|
||
return sourceString.Replace(shortcodeName, shortcodeValue);
|
||
}
|
||
}
|
||
}
|