Merge pull request #2 from HosseinJavadi/fit/master/Hassanzadeh
Fit/master/hassanzadeh
This commit is contained in:
@@ -11,4 +11,5 @@ public interface IRepositoryBase<T, TId>
|
||||
Task DeleteById(TId id, CancellationToken cancellationToken = default);
|
||||
Task<T?> GetById(TId id, CancellationToken cancellationToken = default);
|
||||
Task<bool> Exists(Expression<Func<T,bool>> predicate, CancellationToken cancellationToken = default);
|
||||
Task<T?> Get(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,9 @@ namespace VizhTalk.BuildingBlocks.Infrastructure
|
||||
public async Task<bool> Exists(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
|
||||
=> await _dbContext.Set<T>().Where(predicate).AnyAsync(cancellationToken);
|
||||
|
||||
public async Task<T?> Get(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
|
||||
=> await _dbContext.Set<T>().Where(predicate).FirstOrDefaultAsync(predicate, cancellationToken);
|
||||
|
||||
public async Task<T?> GetById(TId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -7,12 +7,11 @@ using System.Threading.Tasks;
|
||||
using VizhTalk.Learning.Application;
|
||||
using VizhTalk.Learning.Domain.Entities;
|
||||
|
||||
namespace VizhTalk.Learning.Infrastructure.Configuration
|
||||
namespace VizhTalk.Learning.Infrastructure.Configuration;
|
||||
|
||||
public static class Assemblies
|
||||
{
|
||||
public static class Assemblies
|
||||
{
|
||||
public static Assembly Infrastructure = typeof(LearningStartup).Assembly;
|
||||
public static Assembly Application = typeof(ILearningModule).Assembly;
|
||||
public static Assembly Domain = typeof(Language).Assembly;
|
||||
}
|
||||
public static Assembly Infrastructure = typeof(LearningStartup).Assembly;
|
||||
public static Assembly Application = typeof(ILearningModule).Assembly;
|
||||
public static Assembly Domain = typeof(Language).Assembly;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Security.Cryptography;
|
||||
using Grpc.Core;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using Vizhtalk.CDN.Presentation.Application.Exceptions;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using Vizhtalk.CDN.Presentation.Domain.ValueObjects;
|
||||
using VizhTalk.BuildingBlocks.Application.Configuration.Commnad;
|
||||
using VizhTalk.Grpc.CDN;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application.CommandHandlers;
|
||||
|
||||
public class CreateMediaGroupCommandHandler(IUnitOfWork unitOfWork, CDNGrpc.CDNGrpcClient cdnClient) : ICommandHandler<CreateMediaGroupCommand, MediaGroupDTO>
|
||||
{
|
||||
public async Task<MediaGroupDTO> Handle(CreateMediaGroupCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var title = request.Title.Trim();
|
||||
|
||||
if (await unitOfWork.MediaGroup.Exists(a => a.Key == request.Key))
|
||||
throw new MediaGroupKeyAlreadyExistsException();
|
||||
|
||||
|
||||
var mediaGroup = MediaGroup.CreateNew(title,request.Key,request.IsSecure);
|
||||
await unitOfWork.MediaGroup.AddAsync(mediaGroup, cancellationToken);
|
||||
await unitOfWork.SaveChangeAsync(cancellationToken);
|
||||
return mediaGroup.Adapt<MediaGroupDTO>();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class UploadFileCommandHandler(IUnitOfWork unitOfWork, CDNGrpc.CDNGrpcClient cdnClient) : ICommandHandler<UploadFileCommand, UploadFileDTO>
|
||||
{
|
||||
public async Task<UploadFileDTO> Handle(UploadFileCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var fileBytes = request.File.ToByteArray();
|
||||
|
||||
var hash = ToSHA512Hash(fileBytes);
|
||||
|
||||
var media = await unitOfWork.Media.Get(a => a.Hash == hash);
|
||||
|
||||
if (media is null)
|
||||
{
|
||||
|
||||
using MemoryStream stream = new MemoryStream(fileBytes);
|
||||
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToSHA512Hash(byte[] data)
|
||||
{
|
||||
if (data == null)
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
|
||||
using (SHA512 sha512 = SHA512.Create())
|
||||
{
|
||||
byte[] hashBytes = sha512.ComputeHash(data);
|
||||
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using VizhTalk.BuildingBlocks.Application.Exceptions;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application.Exceptions;
|
||||
|
||||
public class MediaGroupKeyAlreadyExistsException : BaseException
|
||||
{
|
||||
|
||||
public MediaGroupKeyAlreadyExistsException() : base("Media groupKey already exists")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using VizhTalk.BuildingBlocks.Application.Exceptions;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application.Exceptions;
|
||||
|
||||
public class MediaNotFoundException : BaseException
|
||||
{
|
||||
|
||||
public MediaNotFoundException() : base("Media not found")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using VizhTalk.BuildingBlocks.Application.Exceptions;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application.Exceptions;
|
||||
|
||||
public class UnauthorizedForMediaException : BaseException
|
||||
{
|
||||
|
||||
public UnauthorizedForMediaException() : base("Unauthorized for media")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using VizhTalk.BuildingBlocks.Application;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application;
|
||||
|
||||
public interface ICDNModule : IExecutor;
|
||||
@@ -0,0 +1,17 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
using VizhTalk.BuildingBlocks.Application;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application
|
||||
{
|
||||
public interface IUnitOfWork : IUnitOfWorkBase
|
||||
{
|
||||
IMediaRepository Media { get; }
|
||||
IClientRepository Client { get; }
|
||||
IMediaClientRepository MediaClient { get; }
|
||||
IMediaGroupRepository MediaGroup{ get; }
|
||||
IMediaGroupMediaRepository MediaGroupMedia { get; }
|
||||
|
||||
Task SaveChangeAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Mapster;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using VizhTalk.Grpc.CDN;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Application.Mappers;
|
||||
|
||||
public class MapperConfig
|
||||
{
|
||||
|
||||
public static void Process()
|
||||
{
|
||||
|
||||
var config = TypeAdapterConfig<MediaGroup, MediaGroupDTO>.NewConfig()
|
||||
.MapWith(lan => new MediaGroupDTO()
|
||||
{
|
||||
Id = lan.Id.Value
|
||||
});
|
||||
|
||||
|
||||
config.Compile();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Vizhtalk.CDN.Presentation.Application.QueryHandlers;
|
||||
|
||||
public class MediaQueryHandler
|
||||
{
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Vizhtalk.CDN.Presentation.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Controllers
|
||||
{
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
public class Client : Entity<ClientId>
|
||||
{
|
||||
private List<MediaClient> _mediaClients = new();
|
||||
public IReadOnlyList<MediaClient> MediaClients => _mediaClients;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Text;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.Domain.ValueObjects;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
public class Media : Entity<MediaId>
|
||||
{
|
||||
|
||||
private Media(ContentType contentType, int size , string hash , string path)
|
||||
{
|
||||
|
||||
ContentType = contentType;
|
||||
Size = size;
|
||||
Hash = hash;
|
||||
Path = path;
|
||||
|
||||
}
|
||||
|
||||
private List<MediaGroupMedia> _mediaGroupMedia = new();
|
||||
public ContentType ContentType { get; private set; }
|
||||
public int Size { get; set; }
|
||||
public string Path { get; private set; }
|
||||
public string Hash { get; set; }
|
||||
public IReadOnlyList<MediaGroupMedia> MediaGroupMedias => _mediaGroupMedia;
|
||||
|
||||
|
||||
public static Media Create(ContentType contentType, int size, string hash, string path)
|
||||
=> new (contentType, size, hash, path);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.Domain.ValueObjects;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
public class MediaClient : Entity<MediaClientId>
|
||||
{
|
||||
public MediaClientName Name { get; private set; }
|
||||
public MediaId MediaId { get; private set; }
|
||||
public ClientId ClientId { get; private set; }
|
||||
public MediaGroupMediaId MediaGroupMediaId { get; set; }
|
||||
|
||||
public Client Client { get; private set; }
|
||||
public Media Media { get; private set; }
|
||||
public MediaGroupMedia MediaGroupMedia { get; private set; }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.Domain.ValueObjects;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
public class MediaGroup : Entity<MediaGroupId>
|
||||
{
|
||||
|
||||
private MediaGroup(MediaGroupTitle title, string key, bool isSecure)
|
||||
{
|
||||
Title = title;
|
||||
Key = key;
|
||||
IsSecure = isSecure;
|
||||
}
|
||||
|
||||
private List<MediaGroupMedia> _mediaGroupMedia = new();
|
||||
public bool IsSecure { get; set; }
|
||||
public MediaGroupTitle Title { get; set; }
|
||||
public string Key { get; set; }
|
||||
public IReadOnlyList<MediaGroupMedia> MediaGroupMedias => _mediaGroupMedia;
|
||||
|
||||
public static MediaGroup CreateNew(string title, string Key, bool isSecure)
|
||||
=> new (new MediaGroupTitle(title), Key, isSecure);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
public class MediaGroupMedia : Entity<MediaGroupMediaId>
|
||||
{
|
||||
private List<MediaClient> _mediaClient = new();
|
||||
public MediaId MediaId { get; set; }
|
||||
public MediaGroupId MediaGroupId { get; set; }
|
||||
public Media Media { get; set; }
|
||||
public MediaGroup MediaGroup { get; set; }
|
||||
public IReadOnlyList<MediaClient> MediaClients => _mediaClient;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
|
||||
public interface IClientRepository : IRepositoryBase<Client, ClientId>;
|
||||
@@ -0,0 +1,7 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
|
||||
public interface IMediaClientRepository : IRepositoryBase<MediaClient, MediaClientId>;
|
||||
@@ -0,0 +1,7 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
|
||||
public interface IMediaGroupMediaRepository : IRepositoryBase<MediaGroupMedia, MediaGroupMediaId>;
|
||||
@@ -0,0 +1,7 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
|
||||
public interface IMediaGroupRepository : IRepositoryBase<MediaGroup, MediaGroupId>;
|
||||
@@ -0,0 +1,7 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
using Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
|
||||
public interface IMediaRepository : IRepositoryBase<Media, MediaId>;
|
||||
@@ -0,0 +1,10 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
|
||||
public class ClientId : TypeIdValueBase<string>
|
||||
{
|
||||
public ClientId(string value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
|
||||
public class MediaClientId : TypeIdValueBase<long>
|
||||
{
|
||||
public MediaClientId(long value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
|
||||
public class MediaGroupId : TypeIdValueBase<long>
|
||||
{
|
||||
public MediaGroupId(long value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
|
||||
public class MediaGroupMediaId : TypeIdValueBase<Guid>
|
||||
{
|
||||
public MediaGroupMediaId(Guid value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.TypeIds;
|
||||
|
||||
public class MediaId : TypeIdValueBase<long>
|
||||
{
|
||||
public MediaId(long value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Vizhtalk.CDN.Presentation.Domain.ValueObjects.Rules;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.ValueObjects;
|
||||
|
||||
public class ContentType : ValueObject
|
||||
{
|
||||
private ContentType()
|
||||
{
|
||||
|
||||
}
|
||||
public ContentType(string contentType, byte[] fileBytes)
|
||||
{
|
||||
CheckRule(new ValidContentTypeRule(contentType, fileBytes));
|
||||
|
||||
Value = contentType;
|
||||
}
|
||||
public string Value { get; private set; }
|
||||
}
|
||||
+3
-3
@@ -1,14 +1,14 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
using VizhTalk.BuildingBlocks.Domain.Guards;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.ValueObjects
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.ValueObjects
|
||||
{
|
||||
public class MediaClientName : ValueObject
|
||||
{
|
||||
public MediaClientName(string value)
|
||||
{
|
||||
Guard.NullOrEmpty(value, nameof(MediaClientName));
|
||||
Guard.MaxLength(value, nameof(MediaClientName), 150);
|
||||
value.NullOrEmpty(nameof(MediaClientName));
|
||||
value.MaxLength(nameof(MediaClientName), 150);
|
||||
|
||||
Value = value;
|
||||
}
|
||||
+3
-3
@@ -1,14 +1,14 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
using VizhTalk.BuildingBlocks.Domain.Guards;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.ValueObjects;
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.ValueObjects;
|
||||
|
||||
public class MediaGroupTitle : ValueObject
|
||||
{
|
||||
public MediaGroupTitle(string value)
|
||||
{
|
||||
Guard.NullOrEmpty(value, nameof(MediaGroupTitle));
|
||||
Guard.MaxLength(value, nameof(MediaGroupTitle),150);
|
||||
value.NullOrEmpty(nameof(MediaGroupTitle));
|
||||
value.MaxLength(nameof(MediaGroupTitle),150);
|
||||
|
||||
Value = value;
|
||||
}
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
using System.Text;
|
||||
using VizhTalk.BuildingBlocks.Domain.Exceptions;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.ValueObjects.Rules;
|
||||
namespace Vizhtalk.CDN.Presentation.Domain.ValueObjects.Rules;
|
||||
|
||||
public class ValidContentTypeRule(string contentType, byte[] fileBytes) : IBusinessRule
|
||||
{
|
||||
@@ -81,7 +81,7 @@ public class ValidContentTypeRule(string contentType, byte[] fileBytes) : IBusin
|
||||
return true;
|
||||
|
||||
if (contentType == "audio/mpeg" && (fileBytes.Take(3).SequenceEqual(new byte[] { 0x49, 0x44, 0x33 }) // ID3
|
||||
|| (fileBytes[0] == 0xFF && (fileBytes[1] & 0xE0) == 0xE0))) // MPEG frame
|
||||
|| fileBytes[0] == 0xFF && (fileBytes[1] & 0xE0) == 0xE0)) // MPEG frame
|
||||
return true;
|
||||
|
||||
if (contentType == "image/svg+xml")
|
||||
@@ -1,10 +0,0 @@
|
||||
using Vizhtalk.CDN.Presentation.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Entities
|
||||
{
|
||||
public class Client : Entity<ClientId>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
using Vizhtalk.CDN.Presentation.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.ValueObjects;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Entities
|
||||
{
|
||||
public class Media : Entity<MediaId>
|
||||
{
|
||||
public ContentType ContentType { get; private set; }
|
||||
public int Size { get; set; }
|
||||
public string Path { get; private set; }
|
||||
public string Hash { get; set; }
|
||||
public List<MediaGroupMedia> MediaGroupMedias { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using Vizhtalk.CDN.Presentation.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.ValueObjects;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Entities
|
||||
{
|
||||
public class MediaClient : Entity<MediaClientId>
|
||||
{
|
||||
public MediaClientName Name { get; set; }
|
||||
public MediaId MediaId { get; set; }
|
||||
public ClientId ClientId { get; set; }
|
||||
public MediaGroupMediaId MediaGroup { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using Vizhtalk.CDN.Presentation.TypeIds;
|
||||
using Vizhtalk.CDN.Presentation.ValueObjects;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Entities
|
||||
{
|
||||
public class MediaGroup : Entity<MediaGroupId>
|
||||
{
|
||||
public bool IsSecure { get; set; }
|
||||
public MediaGroupTitle Title { get; set; }
|
||||
public string Key { get; set; }
|
||||
public List<MediaGroupMedia> MediaGroupMedias { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using Vizhtalk.CDN.Presentation.TypeIds;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Entities
|
||||
{
|
||||
public class MediaGroupMedia : Entity<MediaGroupMediaId>
|
||||
{
|
||||
public MediaId MediaId { get; set; }
|
||||
public MediaGroupId MediaGroupId { get; set; }
|
||||
public List<MediaClient> MediaClients { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Driver;
|
||||
using Vizhtalk.CDN.Presentation.Application;
|
||||
using Vizhtalk.CDN.Presentation.Infrastructure.Configuration;
|
||||
using Vizhtalk.CDN.Presentation.Infrastructure.EF;
|
||||
using VizhTalk.BuildingBlocks.Application;
|
||||
using VizhTalk.BuildingBlocks.Application.Configuration.Commnad;
|
||||
using VizhTalk.BuildingBlocks.Application.Configuration.Query;
|
||||
using VizhTalk.BuildingBlocks.Application.Data;
|
||||
using VizhTalk.BuildingBlocks.Application.Events;
|
||||
using VizhTalk.BuildingBlocks.Application.Outbox;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure.DomainEventsDispatching;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure;
|
||||
|
||||
public static class CDNStartup
|
||||
{
|
||||
public static void Initialize(this IServiceCollection services, string connectionString, string databaseName)
|
||||
{
|
||||
services.AddDbContext<CDNDbContext>(options =>
|
||||
{
|
||||
options.UseMongoDB(connectionString, databaseName);
|
||||
});
|
||||
|
||||
services.AddScoped<DbContext>(sp => sp.GetService<CDNDbContext>());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Reflection;
|
||||
using Vizhtalk.CDN.Presentation.Application;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.Configuration;
|
||||
|
||||
public static class Assemblies
|
||||
{
|
||||
public static Assembly Infrastructure = typeof(UnitOfWork).Assembly;
|
||||
public static Assembly Application = typeof(IUnitOfWork).Assembly;
|
||||
public static Assembly Domain = typeof(Media).Assembly;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Vizhtalk.CDN.Presentation.Application;
|
||||
using VizhTalk.BuildingBlocks.Application.Configuration.Commnad;
|
||||
using VizhTalk.BuildingBlocks.Application.Configuration.Query;
|
||||
using VizhTalk.BuildingBlocks.Application.Contract;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
using VizhTalk.BuildingBlocks.Domain.Contract;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.Configuration;
|
||||
|
||||
public class CDNModule(IServiceProvider serviceProvider) : ICDNModule
|
||||
{
|
||||
public async Task Execute(ICommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
|
||||
var handler = serviceProvider.GetRequiredService(handlerType);
|
||||
dynamic dynamicHandler = handler;
|
||||
dynamic dynamicCommand = command;
|
||||
await dynamicHandler.Handle(dynamicCommand, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<TResult> Execute<TResult>(ICommand<TResult> command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var commandType = command.GetType(); // مثلا CreateUserCommand
|
||||
var resultType = commandType.GetInterface(typeof(ICommand<>).Name)!.GetGenericArguments()[0];
|
||||
|
||||
// حالا handlerType میشه:
|
||||
var handlerType = typeof(ICommandHandler<,>).MakeGenericType(commandType, resultType);
|
||||
|
||||
var handler = serviceProvider.GetRequiredService(handlerType);
|
||||
|
||||
dynamic dynamicHandler = handler;
|
||||
dynamic dynamicCommand = command;
|
||||
|
||||
return await dynamicHandler.Handle(dynamicCommand, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<TResult> Execute<TResult>(IQuery<TResult> query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var queryType = query.GetType();
|
||||
var resultType = queryType.GetInterface(typeof(IQuery<>).Name)!.GetGenericArguments()[0];
|
||||
|
||||
// حالا handlerType میشه:
|
||||
var handlerType = typeof(IQueryHandler<,>).MakeGenericType(queryType, resultType);
|
||||
|
||||
var handler = serviceProvider.GetRequiredService(handlerType);
|
||||
|
||||
dynamic dynamicHandler = handler;
|
||||
dynamic dynamicQuery = query;
|
||||
|
||||
return await dynamicHandler.Handle(dynamicQuery, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task Execute(IDomainEvent notification, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var handlerType = typeof(INotificationHandler<>).MakeGenericType(notification.GetType());
|
||||
var handler = serviceProvider.GetRequiredService(handlerType);
|
||||
dynamic dynamicHandler = handler;
|
||||
dynamic dynamicNotification = notification;
|
||||
await dynamicHandler.Handle(dynamicNotification, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vizhtalk.CDN.Presentation.Infrastructure.Configuration;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure.EF.Helpers;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.EF
|
||||
{
|
||||
public class CDNDbContext : DbContext
|
||||
{
|
||||
|
||||
public CDNDbContext(DbContextOptions<CDNDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.FindEntities(Assemblies.Domain)
|
||||
.ApplyConfigurationsFromAssembly(Assemblies.Infrastructure, x => x.FullName.Contains("EF.Configurations"))
|
||||
.PluralizeEntities();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.EF.Configurations;
|
||||
|
||||
public class ClientConfiguration : IEntityTypeConfiguration<Client>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Client> builder)
|
||||
{
|
||||
builder.ToTable(nameof(Client));
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.ClientId(x));
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(Client.MediaClients))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.EF.Configurations;
|
||||
|
||||
public class MediaClientConfiguration : IEntityTypeConfiguration<MediaClient>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<MediaClient> builder)
|
||||
{
|
||||
builder.ToTable(nameof(MediaClient));
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaClientId(x));
|
||||
|
||||
builder.Property(x => x.Name)
|
||||
.HasConversion(x => x.Value, x => new Domain.ValueObjects.MediaClientName(x));
|
||||
|
||||
builder.Property(x => x.ClientId)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.ClientId(x));
|
||||
|
||||
builder.Property(x => x.MediaGroupMediaId)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaGroupMediaId(x));
|
||||
|
||||
builder.Property(x => x.MediaId)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaId(x));
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(MediaClient.Media))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
builder.Metadata.FindNavigation(nameof(MediaClient.Client))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
builder.Metadata.FindNavigation(nameof(MediaClient.MediaGroupMedia))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.EF.Configurations;
|
||||
|
||||
public class MediaConfiguration : IEntityTypeConfiguration<Media>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Media> builder)
|
||||
{
|
||||
builder.ToTable(nameof(Media));
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id)
|
||||
.HasColumnName("MediaId")
|
||||
.HasElementName("_id")
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaId(x));
|
||||
|
||||
builder.OwnsOne(x => x.ContentType, op =>
|
||||
{
|
||||
op.Property(x => x.Value)
|
||||
.HasColumnName("ContentType")
|
||||
.HasMaxLength(50);
|
||||
});
|
||||
|
||||
builder.HasIndex(x => x.Hash);
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(Media.MediaGroupMedias))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.EF.Configurations;
|
||||
|
||||
public class MediaGroupConfiguration : IEntityTypeConfiguration<MediaGroup>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<MediaGroup> builder)
|
||||
{
|
||||
builder.ToTable(nameof(MediaGroup));
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaGroupId(x));
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(MediaGroup.MediaGroupMedias))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Entities;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure.EF.Configurations;
|
||||
|
||||
public class MediaGroupMediaConfiguration : IEntityTypeConfiguration<MediaGroupMedia>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<MediaGroupMedia> builder)
|
||||
{
|
||||
builder.ToTable(nameof(MediaGroupMedia));
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaGroupMediaId(x));
|
||||
|
||||
builder.Property(x => x.MediaId)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaId(x));
|
||||
|
||||
builder.Property(x => x.MediaGroupId)
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.MediaGroupId(x));
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(MediaGroupMedia.Media))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(MediaGroupMedia.MediaGroup))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
|
||||
|
||||
builder.Metadata.FindNavigation(nameof(MediaGroupMedia.MediaClients))?
|
||||
.SetPropertyAccessMode(PropertyAccessMode.Property);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Vizhtalk.CDN.Presentation.Application;
|
||||
using Vizhtalk.CDN.Presentation.Infrastructure.EF;
|
||||
using Vizhtalk.CDN.Presentation.Domain.Repositories;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure.DomainEventsDispatching;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Infrastructure
|
||||
{
|
||||
public class UnitOfWork(CDNDbContext cdnDbContext, IDomainEventsDispatcher domainEventsDispatcher)
|
||||
: UnitOfWorkBase(cdnDbContext, domainEventsDispatcher)
|
||||
{
|
||||
public IMediaRepository Media => throw new NotImplementedException();
|
||||
|
||||
public IClientRepository Client => throw new NotImplementedException();
|
||||
|
||||
public IMediaClientRepository MediaClient => throw new NotImplementedException();
|
||||
|
||||
public IMediaGroupRepository MediaGroup => throw new NotImplementedException();
|
||||
|
||||
public IMediaGroupMediaRepository MediaGroupMedia => throw new NotImplementedException();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,23 @@
|
||||
using MongoDB.Driver.Core.Configuration;
|
||||
using Vizhtalk.CDN.Presentation.Infrastructure;
|
||||
using Vizhtalk.CDN.Presentation.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var connectionString = builder.Configuration.GetConnectionString("VizhTalk.CDN")!;
|
||||
var databaseName = builder.Configuration.GetConnectionString("VizhTalk.CDN.DatabaseName")!;
|
||||
|
||||
builder.Services.Initialize(connectionString, databaseName);
|
||||
builder.Services.AddGrpc();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
|
||||
app.MapGet("/", () => "Hello World!");
|
||||
|
||||
app.MapGrpcService<CDNService>();
|
||||
|
||||
//app.UseCDNModule();
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
using Grpc.Core;
|
||||
using Vizhtalk.CDN.Presentation.Application;
|
||||
using VizhTalk.Grpc.CDN;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.Services;
|
||||
|
||||
public class CDNService : CDNGrpc.CDNGrpcBase
|
||||
public class CDNService(ICDNModule cDNModule) : CDNGrpc.CDNGrpcBase
|
||||
{
|
||||
public override Task<MediaGroupDTO> CreateMediaGroup(CreateMediaGroupCommand request, ServerCallContext context)
|
||||
public override async Task<MediaGroupDTO> CreateMediaGroup(CreateMediaGroupCommand request, ServerCallContext context)
|
||||
{
|
||||
return base.CreateMediaGroup(request, context);
|
||||
return await cDNModule.Execute(request, context.CancellationToken);
|
||||
}
|
||||
public override Task<UploadFileDTO> UploadFile(UploadFileCommand request, ServerCallContext context)
|
||||
public override async Task<UploadFileDTO> UploadFile(UploadFileCommand request, ServerCallContext context)
|
||||
{
|
||||
return base.UploadFile(request, context);
|
||||
return await cDNModule.Execute(request, context.CancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.TypeIds
|
||||
{
|
||||
public class ClientId : TypeIdValueBase<string>
|
||||
{
|
||||
public ClientId(string value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.TypeIds
|
||||
{
|
||||
public class MediaClientId : TypeIdValueBase<long>
|
||||
{
|
||||
public MediaClientId(long value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.TypeIds
|
||||
{
|
||||
public class MediaGroupId : TypeIdValueBase<long>
|
||||
{
|
||||
public MediaGroupId(long value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.TypeIds
|
||||
{
|
||||
public class MediaGroupMediaId : TypeIdValueBase<Guid>
|
||||
{
|
||||
public MediaGroupMediaId(Guid value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.TypeIds
|
||||
{
|
||||
public class MediaId : TypeIdValueBase<long>
|
||||
{
|
||||
public MediaId(long value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using Vizhtalk.CDN.Presentation.ValueObjects.Rules;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
namespace Vizhtalk.CDN.Presentation.ValueObjects
|
||||
{
|
||||
public class ContentType : ValueObject
|
||||
{
|
||||
public ContentType(string contentType, byte[] fileBytes)
|
||||
{
|
||||
CheckRule(new ValidContentTypeRule(contentType, fileBytes));
|
||||
|
||||
Value = contentType;
|
||||
}
|
||||
public string Value { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -8,13 +8,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.19">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.3.1" />
|
||||
<ProjectReference Include="..\VizhTalk.BuildingBlocks.Infrastructure\VizhTalk.BuildingBlocks.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\VizhTalk.Grpc.CDN\VizhTalk.Grpc.CDN.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Infrastructure\" />
|
||||
<Folder Include="Application\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"VizhTalk.CDN": "mongodb://localhost:27017/VizhTalk.CDN"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"VizhTalk.CDN": "mongodb://localhost:27017",
|
||||
"VizhTalk.CDN.DatabaseName": "VizhTalk.CDN"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user