init project
This commit is contained in:
23
Domain/AggregateModel/AppAggregate/App.cs
Normal file
23
Domain/AggregateModel/AppAggregate/App.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Domain.SeedWork;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Domain.AggregateModel.AppAggregate
|
||||
{
|
||||
public class App : IAggregateRoot
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string AppKey { get;private set; }
|
||||
public string BaseUrl { get; private set; }
|
||||
public string Remarks { get;private set; }
|
||||
private App() { }
|
||||
public App(string appKey,string remarks)
|
||||
{
|
||||
AppKey = appKey;
|
||||
Remarks = remarks;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Domain/AggregateModel/AppAggregate/IAppRepository.cs
Normal file
15
Domain/AggregateModel/AppAggregate/IAppRepository.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Domain.SeedWork;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Domain.AggregateModel.AppAggregate
|
||||
{
|
||||
public interface IAppRepository:IRepository<App>
|
||||
{
|
||||
void Add(App app);
|
||||
Task<App> GetAsync(int id);
|
||||
}
|
||||
}
|
||||
16
Domain/AggregateModel/LinkAggregate/ILinkRepository.cs
Normal file
16
Domain/AggregateModel/LinkAggregate/ILinkRepository.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Domain.SeedWork;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Domain.AggregateModel.LinkAggregate
|
||||
{
|
||||
public interface ILinkRepository:IRepository<Link>
|
||||
{
|
||||
void Add(Link link);
|
||||
|
||||
Task<Link> GetAsync(string shortCode);
|
||||
}
|
||||
}
|
||||
61
Domain/AggregateModel/LinkAggregate/Link.cs
Normal file
61
Domain/AggregateModel/LinkAggregate/Link.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Base62;
|
||||
using Domain.SeedWork;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Domain.AggregateModel.LinkAggregate
|
||||
{
|
||||
public class Link:IAggregateRoot
|
||||
{
|
||||
/// <summary>
|
||||
/// 非自增主键
|
||||
/// </summary>
|
||||
public string ShortCode { get; private set; }
|
||||
public string BaseUrl { get; private set; }
|
||||
public string SuffixUrl { get; private set; }
|
||||
public string FullUrl { get; private set; }
|
||||
public int AppId { get; private set; }
|
||||
public DateTime Time { get; private set; }
|
||||
|
||||
private Link()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建一条新的短链接
|
||||
/// </summary>
|
||||
/// <param name="baseUrl"></param>
|
||||
/// <param name="suffixUrl"></param>
|
||||
/// <param name="appId">分配的应用id</param>
|
||||
public Link(string baseUrl,string suffixUrl,int appId)
|
||||
{
|
||||
BaseUrl = baseUrl;
|
||||
SuffixUrl = suffixUrl;
|
||||
FullUrl = new Uri(new Uri(baseUrl),suffixUrl).ToString();
|
||||
AppId = appId;
|
||||
Time = DateTime.Now;
|
||||
CalculateShortCode();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据值计算短链字段
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void CalculateShortCode()
|
||||
{
|
||||
string code;
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(FullUrl));
|
||||
var res = new[] { BitConverter.ToInt64(result, 0) ,BitConverter.ToInt64(result,8)};
|
||||
//任意取其中一条即可
|
||||
code = res[new Random().Next(0,1)].ToBase62();
|
||||
}
|
||||
ShortCode = code;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user