Knocks/BackEnd/Knoks.Core/Logic/Managers/UidManager.cs

37 lines
1.4 KiB
C#

using HashidsNet;
using Knoks.Core.Logic.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
namespace Knoks.Core.Logic.Managers
{
public class UidManager : IUidManager
{
private static readonly string salt = "rtgk320oSegb32v4rgbf";
//private static readonly Hashids hashids = new Hashids("rtgk320oSegb32v4rgbf", 8);
public string ToUid(long? value, Type type = null, string cause = null)
{
Hashids hashids = new Hashids($"{salt}{type?.Name.ToString()}{cause}", 8);
return value == null ? null : hashids.EncodeLong(value.Value);
}
public long? ToValue(string uid, Type type = null, string cause = null)
{
Hashids hashids = new Hashids($"{salt}{type?.Name.ToString()}{cause}", 8);
return uid == null ? null : (long?)hashids.DecodeLong(uid)[0];
}
public string ToIntUid(int? value, Type type = null, string cause = null)
{
Hashids hashids = new Hashids($"{salt}{type?.Name.ToString()}{cause}", 8);
return value == null ? null : hashids.EncodeLong(value.Value);
}
public int? ToIntValue(string uid, Type type = null, string cause = null)
{
Hashids hashids = new Hashids($"{salt}{type?.Name.ToString()}{cause}", 8);
return uid == null ? null : (int?)hashids.Decode(uid)[0];
}
}
}