init project

This commit is contained in:
2021-02-23 09:51:50 +08:00
commit f7384b9afc
35 changed files with 938 additions and 0 deletions

View 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;
}
}
}

View 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);
}
}

View 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);
}
}

View 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;
}
}
}