init project
This commit is contained in:
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
.vs/
|
||||||
|
Domain/bin/
|
||||||
|
Domain/obj/
|
||||||
|
Infrastructure/bin/
|
||||||
|
Infrastructure/obj/
|
||||||
|
QRCodeService/bin/
|
||||||
|
QRCodeService/obj/
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Domain/Domain.csproj
Normal file
12
Domain/Domain.csproj
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Base62-Net" Version="1.2.157201" />
|
||||||
|
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
92
Domain/SeedWork/Entity.cs
Normal file
92
Domain/SeedWork/Entity.cs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
using MediatR;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Domain.SeedWork
|
||||||
|
{
|
||||||
|
public abstract class Entity
|
||||||
|
{
|
||||||
|
int? _requestedHashCode;
|
||||||
|
int _Id;
|
||||||
|
public virtual int Id
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _Id;
|
||||||
|
}
|
||||||
|
protected set
|
||||||
|
{
|
||||||
|
_Id = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<INotification> _domainEvents;
|
||||||
|
public IReadOnlyCollection<INotification> DomainEvents => _domainEvents?.AsReadOnly();
|
||||||
|
|
||||||
|
public void AddDomainEvent(INotification eventItem)
|
||||||
|
{
|
||||||
|
_domainEvents = _domainEvents ?? new List<INotification>();
|
||||||
|
_domainEvents.Add(eventItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveDomainEvent(INotification eventItem)
|
||||||
|
{
|
||||||
|
_domainEvents?.Remove(eventItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearDomainEvents()
|
||||||
|
{
|
||||||
|
_domainEvents?.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsTransient()
|
||||||
|
{
|
||||||
|
return Id == default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
if (obj == null || !(obj is Entity))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (ReferenceEquals(this, obj))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (this.GetType() != obj.GetType())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Entity item = (Entity)obj;
|
||||||
|
|
||||||
|
if (item.IsTransient() || this.IsTransient())
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return item.Id == this.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
if (!IsTransient())
|
||||||
|
{
|
||||||
|
if (!_requestedHashCode.HasValue)
|
||||||
|
_requestedHashCode = this.Id.GetHashCode() ^ 31; // XOR for random distribution (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx)
|
||||||
|
|
||||||
|
return _requestedHashCode.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return base.GetHashCode();
|
||||||
|
|
||||||
|
}
|
||||||
|
public static bool operator ==(Entity left, Entity right)
|
||||||
|
{
|
||||||
|
if (Object.Equals(left, null))
|
||||||
|
return (Object.Equals(right, null)) ? true : false;
|
||||||
|
else
|
||||||
|
return left.Equals(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(Entity left, Entity right)
|
||||||
|
{
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
74
Domain/SeedWork/Enumeration.cs
Normal file
74
Domain/SeedWork/Enumeration.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Domain.SeedWork
|
||||||
|
{
|
||||||
|
public abstract class Enumeration : IComparable
|
||||||
|
{
|
||||||
|
public string Name { get; private set; }
|
||||||
|
|
||||||
|
public int Id { get; private set; }
|
||||||
|
|
||||||
|
protected Enumeration(int id, string name)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => Name;
|
||||||
|
|
||||||
|
public static IEnumerable<T> GetAll<T>() where T : Enumeration
|
||||||
|
{
|
||||||
|
var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
|
||||||
|
|
||||||
|
return fields.Select(f => f.GetValue(null)).Cast<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
var otherValue = obj as Enumeration;
|
||||||
|
|
||||||
|
if (otherValue == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var typeMatches = GetType().Equals(obj.GetType());
|
||||||
|
var valueMatches = Id.Equals(otherValue.Id);
|
||||||
|
|
||||||
|
return typeMatches && valueMatches;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode() => Id.GetHashCode();
|
||||||
|
|
||||||
|
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
|
||||||
|
{
|
||||||
|
var absoluteDifference = Math.Abs(firstValue.Id - secondValue.Id);
|
||||||
|
return absoluteDifference;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T FromValue<T>(int value) where T : Enumeration
|
||||||
|
{
|
||||||
|
var matchingItem = Parse<T, int>(value, "value", item => item.Id == value);
|
||||||
|
return matchingItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T FromDisplayName<T>(string displayName) where T : Enumeration
|
||||||
|
{
|
||||||
|
var matchingItem = Parse<T, string>(displayName, "display name", item => item.Name == displayName);
|
||||||
|
return matchingItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static T Parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration
|
||||||
|
{
|
||||||
|
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
|
||||||
|
|
||||||
|
if (matchingItem == null)
|
||||||
|
throw new InvalidOperationException($"'{value}' is not a valid {description} in {typeof(T)}");
|
||||||
|
|
||||||
|
return matchingItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
Domain/SeedWork/IAggregateRoot.cs
Normal file
6
Domain/SeedWork/IAggregateRoot.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Domain.SeedWork
|
||||||
|
{
|
||||||
|
public interface IAggregateRoot
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
7
Domain/SeedWork/IRepository.cs
Normal file
7
Domain/SeedWork/IRepository.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Domain.SeedWork
|
||||||
|
{
|
||||||
|
public interface IRepository<T> where T : IAggregateRoot
|
||||||
|
{
|
||||||
|
IUnitOfWork UnitOfWork { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Domain/SeedWork/IUnitOfWork.cs
Normal file
12
Domain/SeedWork/IUnitOfWork.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Domain.SeedWork
|
||||||
|
{
|
||||||
|
public interface IUnitOfWork : IDisposable
|
||||||
|
{
|
||||||
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken));
|
||||||
|
Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken));
|
||||||
|
}
|
||||||
|
}
|
||||||
48
Domain/SeedWork/ValueObject.cs
Normal file
48
Domain/SeedWork/ValueObject.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Domain.SeedWork
|
||||||
|
{
|
||||||
|
public abstract class ValueObject
|
||||||
|
{
|
||||||
|
protected static bool EqualOperator(ValueObject left, ValueObject right)
|
||||||
|
{
|
||||||
|
if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ReferenceEquals(left, null) || left.Equals(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static bool NotEqualOperator(ValueObject left, ValueObject right)
|
||||||
|
{
|
||||||
|
return !(EqualOperator(left, right));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract IEnumerable<object> GetEqualityComponents();
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
if (obj == null || obj.GetType() != GetType())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var other = (ValueObject)obj;
|
||||||
|
|
||||||
|
return this.GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return GetEqualityComponents()
|
||||||
|
.Select(x => x != null ? x.GetHashCode() : 0)
|
||||||
|
.Aggregate((x, y) => x ^ y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueObject GetCopy()
|
||||||
|
{
|
||||||
|
return this.MemberwiseClone() as ValueObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Infrastructure/Infrastructure.csproj
Normal file
11
Infrastructure/Infrastructure.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Domain\Domain.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
25
Infrastructure/Repositories/AppRepository.cs
Normal file
25
Infrastructure/Repositories/AppRepository.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using Domain.AggregateModel.AppAggregate;
|
||||||
|
using Domain.SeedWork;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Infrastructure.Repositories
|
||||||
|
{
|
||||||
|
public class AppRepository : IAppRepository
|
||||||
|
{
|
||||||
|
public IUnitOfWork UnitOfWork => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public void Add(App app)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<App> GetAsync(int id)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Infrastructure/Repositories/LinkRepository.cs
Normal file
25
Infrastructure/Repositories/LinkRepository.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using Domain.AggregateModel.LinkAggregate;
|
||||||
|
using Domain.SeedWork;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Infrastructure.Repositories
|
||||||
|
{
|
||||||
|
public class LinkRepository : ILinkRepository
|
||||||
|
{
|
||||||
|
public IUnitOfWork UnitOfWork => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public void Add(Link link)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Link> GetAsync(string shortCode)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
QRCodeService.sln
Normal file
37
QRCodeService.sln
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.30804.86
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCodeService", "QRCodeService\QRCodeService.csproj", "{CF4EF394-9502-4720-8E32-30132C5A7B40}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csproj", "{FB39ABD1-A7B6-479D-9552-D81D8360E48B}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "Infrastructure\Infrastructure.csproj", "{3A7EB8BC-4099-461F-9E2D-D34AEC5FC056}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{CF4EF394-9502-4720-8E32-30132C5A7B40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{CF4EF394-9502-4720-8E32-30132C5A7B40}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{CF4EF394-9502-4720-8E32-30132C5A7B40}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{CF4EF394-9502-4720-8E32-30132C5A7B40}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FB39ABD1-A7B6-479D-9552-D81D8360E48B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FB39ABD1-A7B6-479D-9552-D81D8360E48B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FB39ABD1-A7B6-479D-9552-D81D8360E48B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FB39ABD1-A7B6-479D-9552-D81D8360E48B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{3A7EB8BC-4099-461F-9E2D-D34AEC5FC056}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{3A7EB8BC-4099-461F-9E2D-D34AEC5FC056}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{3A7EB8BC-4099-461F-9E2D-D34AEC5FC056}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{3A7EB8BC-4099-461F-9E2D-D34AEC5FC056}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {BF393BCC-5055-4752-A4D8-DF51E9A215AE}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
14
QRCodeService/Application/Commands/CreateLinkCommand.cs
Normal file
14
QRCodeService/Application/Commands/CreateLinkCommand.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using MediatR;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Application.Commands
|
||||||
|
{
|
||||||
|
public class CreateLinkCommand : IRequest<bool>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using MediatR;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Application.Commands
|
||||||
|
{
|
||||||
|
public class CreateLinkCommandHandler : IRequestHandler<CreateLinkCommand, bool>
|
||||||
|
{
|
||||||
|
public Task<bool> Handle(CreateLinkCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
QRCodeService/Application/Queries/GetLinkQueryHandler.cs
Normal file
13
QRCodeService/Application/Queries/GetLinkQueryHandler.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Application.Queries
|
||||||
|
{
|
||||||
|
public class GetLinkQueryHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
13
QRCodeService/Application/Queries/ILinkQueries.cs
Normal file
13
QRCodeService/Application/Queries/ILinkQueries.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Application.Queries
|
||||||
|
{
|
||||||
|
public interface ILinkQueries
|
||||||
|
{
|
||||||
|
Task<Link> GetLinkAsync(string shortCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
QRCodeService/Application/Queries/LinkQueries.cs
Normal file
32
QRCodeService/Application/Queries/LinkQueries.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using MediatR;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Dapper;
|
||||||
|
using MySqlConnector;
|
||||||
|
|
||||||
|
namespace QRCodeService.Application.Queries
|
||||||
|
{
|
||||||
|
public class LinkQueries : ILinkQueries
|
||||||
|
{
|
||||||
|
readonly string _connectionString;
|
||||||
|
|
||||||
|
public LinkQueries(string connectionString)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Link> GetLinkAsync(string shortCode)
|
||||||
|
{
|
||||||
|
using(var connection = new MySqlConnection(_connectionString))
|
||||||
|
{
|
||||||
|
connection.Open();
|
||||||
|
var link = await connection.QueryAsync<Link>(
|
||||||
|
@"SELECT A FROM B WHERE ShortCode = @shortCode",new {shortCode });
|
||||||
|
return link.Single();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
QRCodeService/Application/Queries/LinkViewModel.cs
Normal file
15
QRCodeService/Application/Queries/LinkViewModel.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Application.Queries
|
||||||
|
{
|
||||||
|
public class Link
|
||||||
|
{
|
||||||
|
public string ShortCode { get; set; }
|
||||||
|
|
||||||
|
public string FullUrl { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
58
QRCodeService/Controllers/GenController.cs
Normal file
58
QRCodeService/Controllers/GenController.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using Domain.AggregateModel.LinkAggregate;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using QRCodeService.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using Domain.AggregateModel.AppAggregate;
|
||||||
|
using Base62;
|
||||||
|
using QRCodeService.Extensions;
|
||||||
|
|
||||||
|
namespace QRCodeService.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 生成二维码图片
|
||||||
|
/// </summary>
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class GenController:ControllerBase
|
||||||
|
{
|
||||||
|
readonly ILinkRepository linkRepository;
|
||||||
|
readonly IAppRepository appRepository;
|
||||||
|
|
||||||
|
public GenController(ILinkRepository linkRepository, IAppRepository appRepository)
|
||||||
|
{
|
||||||
|
this.linkRepository = linkRepository;
|
||||||
|
this.appRepository = appRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> CreateLink(GenInputModel model)
|
||||||
|
{
|
||||||
|
|
||||||
|
var app = await appRepository.GetAsync(model.AppId);
|
||||||
|
if (app == null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
using(var md5 = MD5.Create())
|
||||||
|
{
|
||||||
|
var result = md5.ComputeHash(Encoding.UTF8.GetBytes($"{model.AppId}{model.SuffixUrl}{model.Time}{app.AppKey}"));
|
||||||
|
var sign = BitConverter.ToString(result);
|
||||||
|
if (sign != model.Sign)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var shortCode = $"{model.SuffixUrl}".ToMD5().ToBase62();
|
||||||
|
var link = new Link(app.BaseUrl, model.SuffixUrl,1);
|
||||||
|
linkRepository.Add(link);
|
||||||
|
await linkRepository.UnitOfWork.SaveEntitiesAsync();
|
||||||
|
return Ok(link.ShortCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
QRCodeService/Controllers/GoController.cs
Normal file
34
QRCodeService/Controllers/GoController.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using Domain.AggregateModel.LinkAggregate;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 跳转url请求的地址
|
||||||
|
/// </summary>
|
||||||
|
[Route("{shortCode}")]
|
||||||
|
[ApiController]
|
||||||
|
public class GoController: ControllerBase
|
||||||
|
{
|
||||||
|
readonly ILinkRepository LinkRepository;
|
||||||
|
public GoController(ILinkRepository linkRepository)
|
||||||
|
{
|
||||||
|
LinkRepository = linkRepository;
|
||||||
|
}
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Get(string shortcode)
|
||||||
|
{
|
||||||
|
var link = await LinkRepository.GetAsync(shortcode);
|
||||||
|
if (link == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
return Redirect(link.FullUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
QRCodeService/Controllers/ImageController.cs
Normal file
29
QRCodeService/Controllers/ImageController.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using QRCoder;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace QRCodeService.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class ImageController : ControllerBase
|
||||||
|
{
|
||||||
|
[Route("{shortCode}")]
|
||||||
|
public IActionResult Get(string shortCode)
|
||||||
|
{
|
||||||
|
var qrCodeGenerator = new QRCodeGenerator();
|
||||||
|
var data = qrCodeGenerator.CreateQrCode("www.baidu.com", QRCodeGenerator.ECCLevel.Q);
|
||||||
|
var qrCode = new QRCode(data);
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
qrCode.GetGraphic(20).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
|
||||||
|
return File(stream.ToArray(), "image/png", "qrcode.png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
QRCodeService/Controllers/PlaygroundController.cs
Normal file
35
QRCodeService/Controllers/PlaygroundController.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using Base62;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Controllers
|
||||||
|
{
|
||||||
|
[Route("[controller]/[action]")]
|
||||||
|
public class PlaygroundController:ControllerBase
|
||||||
|
{
|
||||||
|
public IActionResult GenShortCode(string url)
|
||||||
|
{
|
||||||
|
var codes = new string[2];
|
||||||
|
using (var md5 = MD5.Create())
|
||||||
|
{
|
||||||
|
var chunkSize = 8;
|
||||||
|
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(url));
|
||||||
|
for (int i = 0; i < 2; i++)
|
||||||
|
{
|
||||||
|
var first = result.Take(chunkSize).ToArray();
|
||||||
|
//即超过30位的忽略处理
|
||||||
|
var res = BitConverter.ToInt64(first);
|
||||||
|
var code = res.ToBase62();
|
||||||
|
codes[i] = code;
|
||||||
|
result = result.Skip(chunkSize).ToArray();
|
||||||
|
}
|
||||||
|
return Ok(codes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
QRCodeService/Extensions/StringExtension.cs
Normal file
21
QRCodeService/Extensions/StringExtension.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Extensions
|
||||||
|
{
|
||||||
|
public static class StringExtension
|
||||||
|
{
|
||||||
|
public static byte[] ToMD5(this string value)
|
||||||
|
{
|
||||||
|
using (var md5 = MD5.Create())
|
||||||
|
{
|
||||||
|
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
QRCodeService/Models/GenInputModel.cs
Normal file
16
QRCodeService/Models/GenInputModel.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService.Models
|
||||||
|
{
|
||||||
|
public class GenInputModel
|
||||||
|
{
|
||||||
|
public int AppId { get; set; }
|
||||||
|
public string Time { get; set; }
|
||||||
|
public string SuffixUrl { get; set; }
|
||||||
|
public string Sign { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
26
QRCodeService/Program.cs
Normal file
26
QRCodeService/Program.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
CreateHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
|
Host.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
|
{
|
||||||
|
webBuilder.UseStartup<Startup>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
31
QRCodeService/Properties/launchSettings.json
Normal file
31
QRCodeService/Properties/launchSettings.json
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:58886",
|
||||||
|
"sslPort": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"QRCodeService": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": "true",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5000",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
QRCodeService/QRCodeService.csproj
Normal file
26
QRCodeService/QRCodeService.csproj
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Base62-Net" Version="1.2.157201" />
|
||||||
|
<PackageReference Include="Dapper" Version="2.0.78" />
|
||||||
|
<PackageReference Include="FluentValidation.AspNetCore" Version="9.5.1" />
|
||||||
|
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" NoWarn="NU1605" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.1" NoWarn="NU1605" />
|
||||||
|
<PackageReference Include="MySqlConnector" Version="1.2.0" />
|
||||||
|
<PackageReference Include="QRCoder" Version="1.4.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Domain\Domain.csproj" />
|
||||||
|
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ProjectExtensions><VisualStudio><UserProperties properties_4launchsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
|
||||||
|
|
||||||
|
</Project>
|
||||||
11
QRCodeService/QRCodeService.csproj.user
Normal file
11
QRCodeService/QRCodeService.csproj.user
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup>
|
||||||
|
<ActiveDebugProfile>QRCodeService</ActiveDebugProfile>
|
||||||
|
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||||
|
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
||||||
56
QRCodeService/Startup.cs
Normal file
56
QRCodeService/Startup.cs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace QRCodeService
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
|
||||||
|
services.AddControllers();
|
||||||
|
services.AddSwaggerGen(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QRCodeService v1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
QRCodeService/appsettings.Development.json
Normal file
9
QRCodeService/appsettings.Development.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft": "Warning",
|
||||||
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
QRCodeService/appsettings.json
Normal file
10
QRCodeService/appsettings.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft": "Warning",
|
||||||
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user