CreateLanguageCommand
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
namespace VizhTalk.BuildingBlocks.Application.Exceptions
|
||||
{
|
||||
public abstract class BaseException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VizhTalk.BuildingBlocks.Application.Exceptions
|
||||
{
|
||||
public class LanguageTitleAlreadyExistsException : BaseException
|
||||
{
|
||||
public LanguageTitleAlreadyExistsException() : base("title already exists")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace VizhTalk.BuildingBlocks.Domain;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace VizhTalk.BuildingBlocks.Domain;
|
||||
|
||||
public interface IRepositoryBase<T, TId>
|
||||
where T : Entity<TId> where TId : ITypeIdValueBase
|
||||
@@ -8,5 +10,5 @@ public interface IRepositoryBase<T,TId>
|
||||
Task DeleteAsync(T entity, CancellationToken cancellationToken = default);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,7 @@ public abstract class TypedIdValueBase<T> : IEquatable<TypedIdValueBase<T>> , IT
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj is TypedIdValueBase<T> other && Equals(other);
|
||||
}
|
||||
@@ -34,13 +32,12 @@ public abstract class TypedIdValueBase<T> : IEquatable<TypedIdValueBase<T>> , IT
|
||||
|
||||
public bool Equals(TypedIdValueBase<T>? other)
|
||||
{
|
||||
if(other == null)
|
||||
return false;
|
||||
|
||||
return EqualityComparer<T>.Default.Equals(this.Value, other.Value);
|
||||
}
|
||||
|
||||
public bool Equals(ITypeIdValueBase? other)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static bool operator ==(TypedIdValueBase<T>? obj1, TypedIdValueBase<T>? obj2)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VizhTalk.BuildingBlocks.Domain;
|
||||
@@ -19,6 +20,8 @@ namespace VizhTalk.BuildingBlocks.Infrastructure
|
||||
{
|
||||
this._dbContext = dbContext;
|
||||
}
|
||||
|
||||
|
||||
public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
@@ -46,6 +49,9 @@ namespace VizhTalk.BuildingBlocks.Infrastructure
|
||||
entity.SetDeleted(true);
|
||||
}
|
||||
|
||||
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?> GetById(TId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -3,5 +3,5 @@ using VizhTalk.Grpc.Learning.Language;
|
||||
|
||||
namespace VizhTalk.Learning.Application.CommandHandlers.Commands;
|
||||
|
||||
public record CreateLanguageCommand(CreateLanguageRequest model) : ICommand<LanguageDTO>;
|
||||
public record UpdateLanguageCommand(UpdateLanguageRequest model) : ICommand;
|
||||
public record CreateLanguageCommand(CreateLanguageRequest Model) : ICommand<LanguageDTO>;
|
||||
public record UpdateLanguageCommand(UpdateLanguageRequest Model) : ICommand;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Mapster;
|
||||
using VizhTalk.BuildingBlocks.Application.Configuration.Commnad;
|
||||
using VizhTalk.BuildingBlocks.Application.Exceptions;
|
||||
using VizhTalk.Grpc.Learning.Language;
|
||||
using VizhTalk.Learning.Application.CommandHandlers.Commands;
|
||||
using VizhTalk.Learning.Domain.Entities;
|
||||
@@ -11,17 +12,14 @@ namespace VizhTalk.Learning.Application.CommandHandlers
|
||||
{
|
||||
public async Task<LanguageDTO> Handle(CreateLanguageCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var language = Language.CreateNew(new LanguageTitle(request.model.Title));
|
||||
await unitOfWork.Language.AddAsync(language, cancellationToken);
|
||||
return language.Adapt<LanguageDTO>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var title = request.Model.Title.Trim();
|
||||
if (await unitOfWork.Language.Exists(x => x.Title.Value == title))
|
||||
throw new LanguageTitleAlreadyExistsException();
|
||||
|
||||
throw;
|
||||
}
|
||||
var language = Language.CreateNew(new LanguageTitle(title));
|
||||
await unitOfWork.Language.AddAsync(language, cancellationToken);
|
||||
await unitOfWork.SaveChangeAsync(cancellationToken);
|
||||
return language.Adapt<LanguageDTO>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,7 @@ namespace VizhTalk.Learning.Application;
|
||||
|
||||
public interface IUnitOfWork : IUnitOfWorkBase
|
||||
{
|
||||
public ILanguageRepository Language { get; }
|
||||
ILanguageRepository Language { get; }
|
||||
|
||||
Task SaveChangeAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -12,4 +12,8 @@
|
||||
<ProjectReference Include="..\VizhTalk.Services.Grpc.Learning\VizhTalk.Grpc.Learning.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Exceptions\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -18,4 +18,6 @@ public class Language : Entity<LanguageId>, IAggregateRoot
|
||||
|
||||
public static Language CreateNew(LanguageTitle title)
|
||||
=> new(title);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ namespace VizhTalk.Learning.Domain.ValueObjects
|
||||
{
|
||||
public class LanguageTitle : ValueObject
|
||||
{
|
||||
public LanguageTitle()
|
||||
{
|
||||
|
||||
}
|
||||
public LanguageTitle(string title)
|
||||
{
|
||||
Guard.NullOrEmpty(title, nameof(LanguageTitle));
|
||||
@@ -12,6 +16,6 @@ namespace VizhTalk.Learning.Domain.ValueObjects
|
||||
|
||||
Value = title;
|
||||
}
|
||||
public string Value { get; set; }
|
||||
public string Value { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,12 @@ namespace VizhTalk.Learning.Infrastructure.EF.Configurations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasConversion(x => x.Value, x => new Domain.TypeIds.LanguageId(x));
|
||||
|
||||
builder.Property(x => x.Title)
|
||||
.HasConversion(s => s.Value, s => new Domain.ValueObjects.LanguageTitle(s))
|
||||
builder.OwnsOne(x => x.Title, op =>
|
||||
{
|
||||
op.Property(x => x.Value)
|
||||
.HasColumnName("Title")
|
||||
.HasMaxLength(60);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public static class LearningStartup
|
||||
{
|
||||
op.UseSqlServer(connectionString);
|
||||
});
|
||||
services.AddScoped<DbContext, LearningDbContext>();
|
||||
services.AddScoped<DbContext>(sp => sp.GetService<LearningDbContext>());
|
||||
services.AddScoped<ILearningModule, LearningModule>();
|
||||
services.AddScoped<IExecutor, LearningModule>();
|
||||
services.AddScoped<IUnitOfWorkBase, UnitOfWorkBase>();
|
||||
|
||||
+26
-6
@@ -11,7 +11,7 @@ using VizhTalk.Learning.Infrastructure.EF;
|
||||
namespace VizhTalk.Learning.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(LearningDbContext))]
|
||||
[Migration("20250901155327_Initial")]
|
||||
[Migration("20250901180950_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@@ -39,15 +39,35 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(60)
|
||||
.HasColumnType("nvarchar(60)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Languages", "Learning");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("VizhTalk.Learning.Domain.Entities.Language", b =>
|
||||
{
|
||||
b.OwnsOne("VizhTalk.Learning.Domain.ValueObjects.LanguageTitle", "Title", b1 =>
|
||||
{
|
||||
b1.Property<long>("LanguageId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b1.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasMaxLength(60)
|
||||
.HasColumnType("nvarchar(60)")
|
||||
.HasColumnName("Title");
|
||||
|
||||
b1.HasKey("LanguageId");
|
||||
|
||||
b1.ToTable("LanguageTitles", "Learning");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("LanguageId");
|
||||
});
|
||||
|
||||
b.Navigation("Title")
|
||||
.IsRequired();
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
+24
-1
@@ -20,7 +20,6 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
|
||||
{
|
||||
Id = table.Column<long>(type: "bigint", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
|
||||
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||
IsActive = table.Column<bool>(type: "bit", nullable: false)
|
||||
},
|
||||
@@ -28,11 +27,35 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
|
||||
{
|
||||
table.PrimaryKey("PK_Languages", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "LanguageTitles",
|
||||
schema: "Learning",
|
||||
columns: table => new
|
||||
{
|
||||
LanguageId = table.Column<long>(type: "bigint", nullable: false),
|
||||
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_LanguageTitles", x => x.LanguageId);
|
||||
table.ForeignKey(
|
||||
name: "FK_LanguageTitles_Languages_LanguageId",
|
||||
column: x => x.LanguageId,
|
||||
principalSchema: "Learning",
|
||||
principalTable: "Languages",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "LanguageTitles",
|
||||
schema: "Learning");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Languages",
|
||||
schema: "Learning");
|
||||
@@ -36,15 +36,35 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(60)
|
||||
.HasColumnType("nvarchar(60)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Languages", "Learning");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("VizhTalk.Learning.Domain.Entities.Language", b =>
|
||||
{
|
||||
b.OwnsOne("VizhTalk.Learning.Domain.ValueObjects.LanguageTitle", "Title", b1 =>
|
||||
{
|
||||
b1.Property<long>("LanguageId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b1.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasMaxLength(60)
|
||||
.HasColumnType("nvarchar(60)")
|
||||
.HasColumnName("Title");
|
||||
|
||||
b1.HasKey("LanguageId");
|
||||
|
||||
b1.ToTable("LanguageTitles", "Learning");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("LanguageId");
|
||||
});
|
||||
|
||||
b.Navigation("Title")
|
||||
.IsRequired();
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure;
|
||||
using VizhTalk.BuildingBlocks.Infrastructure.DomainEventsDispatching;
|
||||
using VizhTalk.Learning.Application;
|
||||
using VizhTalk.Learning.Domain.Repositories;
|
||||
@@ -26,5 +21,8 @@ namespace VizhTalk.Learning.Infrastructure
|
||||
return _language;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveChangeAsync(CancellationToken cancellationToken = default)
|
||||
=> await learningDbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user