CreateLanguageCommand

This commit is contained in:
2025-09-01 22:42:10 +04:30
parent 746b6159e4
commit fc6d32bad7
17 changed files with 151 additions and 49 deletions
@@ -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,12 +1,14 @@
namespace VizhTalk.BuildingBlocks.Domain; using System.Linq.Expressions;
public interface IRepositoryBase<T,TId> namespace VizhTalk.BuildingBlocks.Domain;
public interface IRepositoryBase<T, TId>
where T : Entity<TId> where TId : ITypeIdValueBase where T : Entity<TId> where TId : ITypeIdValueBase
{ {
Task<T> AddAsync(T entity,CancellationToken cancellationToken = default); Task<T> AddAsync(T entity, CancellationToken cancellationToken = default);
Task UpdateAsync(T entity,CancellationToken cancellationToken = default); Task UpdateAsync(T entity, CancellationToken cancellationToken = default);
Task DeleteAsync(T entity,CancellationToken cancellationToken = default); Task DeleteAsync(T entity, CancellationToken cancellationToken = default);
Task DeleteById(TId id,CancellationToken cancellationToken = default); Task DeleteById(TId id, CancellationToken cancellationToken = default);
Task<T?> GetById(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) public override bool Equals(object? obj)
{ {
if (ReferenceEquals(null, obj)) if (ReferenceEquals(null, obj))
{
return false; return false;
}
return obj is TypedIdValueBase<T> other && Equals(other); 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) public bool Equals(TypedIdValueBase<T>? other)
{ {
if(other == null)
return false;
return EqualityComparer<T>.Default.Equals(this.Value, other.Value); 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) public static bool operator ==(TypedIdValueBase<T>? obj1, TypedIdValueBase<T>? obj2)
{ {
@@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using VizhTalk.BuildingBlocks.Domain; using VizhTalk.BuildingBlocks.Domain;
@@ -19,6 +20,8 @@ namespace VizhTalk.BuildingBlocks.Infrastructure
{ {
this._dbContext = dbContext; this._dbContext = dbContext;
} }
public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default) public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@@ -46,6 +49,9 @@ namespace VizhTalk.BuildingBlocks.Infrastructure
entity.SetDeleted(true); 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) public async Task<T?> GetById(TId id, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@@ -3,5 +3,5 @@ using VizhTalk.Grpc.Learning.Language;
namespace VizhTalk.Learning.Application.CommandHandlers.Commands; namespace VizhTalk.Learning.Application.CommandHandlers.Commands;
public record CreateLanguageCommand(CreateLanguageRequest model) : ICommand<LanguageDTO>; public record CreateLanguageCommand(CreateLanguageRequest Model) : ICommand<LanguageDTO>;
public record UpdateLanguageCommand(UpdateLanguageRequest model) : ICommand; public record UpdateLanguageCommand(UpdateLanguageRequest Model) : ICommand;
@@ -1,5 +1,6 @@
using Mapster; using Mapster;
using VizhTalk.BuildingBlocks.Application.Configuration.Commnad; using VizhTalk.BuildingBlocks.Application.Configuration.Commnad;
using VizhTalk.BuildingBlocks.Application.Exceptions;
using VizhTalk.Grpc.Learning.Language; using VizhTalk.Grpc.Learning.Language;
using VizhTalk.Learning.Application.CommandHandlers.Commands; using VizhTalk.Learning.Application.CommandHandlers.Commands;
using VizhTalk.Learning.Domain.Entities; using VizhTalk.Learning.Domain.Entities;
@@ -11,17 +12,14 @@ namespace VizhTalk.Learning.Application.CommandHandlers
{ {
public async Task<LanguageDTO> Handle(CreateLanguageCommand request, CancellationToken cancellationToken) public async Task<LanguageDTO> Handle(CreateLanguageCommand request, CancellationToken cancellationToken)
{ {
try var title = request.Model.Title.Trim();
{ if (await unitOfWork.Language.Exists(x => x.Title.Value == title))
var language = Language.CreateNew(new LanguageTitle(request.model.Title)); throw new LanguageTitleAlreadyExistsException();
await unitOfWork.Language.AddAsync(language, cancellationToken);
return language.Adapt<LanguageDTO>();
}
catch (Exception ex)
{
throw; var language = Language.CreateNew(new LanguageTitle(title));
} await unitOfWork.Language.AddAsync(language, cancellationToken);
await unitOfWork.SaveChangeAsync(cancellationToken);
return language.Adapt<LanguageDTO>();
} }
} }
+3 -1
View File
@@ -5,5 +5,7 @@ namespace VizhTalk.Learning.Application;
public interface IUnitOfWork : IUnitOfWorkBase 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" /> <ProjectReference Include="..\VizhTalk.Services.Grpc.Learning\VizhTalk.Grpc.Learning.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Exceptions\" />
</ItemGroup>
</Project> </Project>
@@ -18,4 +18,6 @@ public class Language : Entity<LanguageId>, IAggregateRoot
public static Language CreateNew(LanguageTitle title) public static Language CreateNew(LanguageTitle title)
=> new(title); => new(title);
} }
@@ -5,6 +5,10 @@ namespace VizhTalk.Learning.Domain.ValueObjects
{ {
public class LanguageTitle : ValueObject public class LanguageTitle : ValueObject
{ {
public LanguageTitle()
{
}
public LanguageTitle(string title) public LanguageTitle(string title)
{ {
Guard.NullOrEmpty(title, nameof(LanguageTitle)); Guard.NullOrEmpty(title, nameof(LanguageTitle));
@@ -12,6 +16,6 @@ namespace VizhTalk.Learning.Domain.ValueObjects
Value = title; Value = title;
} }
public string Value { get; set; } public string Value { get; private set; }
} }
} }
@@ -20,9 +20,12 @@ namespace VizhTalk.Learning.Infrastructure.EF.Configurations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasConversion(x => x.Value, x => new Domain.TypeIds.LanguageId(x)); .HasConversion(x => x.Value, x => new Domain.TypeIds.LanguageId(x));
builder.Property(x => x.Title) builder.OwnsOne(x => x.Title, op =>
.HasConversion(s => s.Value, s => new Domain.ValueObjects.LanguageTitle(s)) {
op.Property(x => x.Value)
.HasColumnName("Title")
.HasMaxLength(60); .HasMaxLength(60);
});
} }
} }
@@ -27,7 +27,7 @@ public static class LearningStartup
{ {
op.UseSqlServer(connectionString); op.UseSqlServer(connectionString);
}); });
services.AddScoped<DbContext, LearningDbContext>(); services.AddScoped<DbContext>(sp => sp.GetService<LearningDbContext>());
services.AddScoped<ILearningModule, LearningModule>(); services.AddScoped<ILearningModule, LearningModule>();
services.AddScoped<IExecutor, LearningModule>(); services.AddScoped<IExecutor, LearningModule>();
services.AddScoped<IUnitOfWorkBase, UnitOfWorkBase>(); services.AddScoped<IUnitOfWorkBase, UnitOfWorkBase>();
@@ -11,7 +11,7 @@ using VizhTalk.Learning.Infrastructure.EF;
namespace VizhTalk.Learning.Infrastructure.Migrations namespace VizhTalk.Learning.Infrastructure.Migrations
{ {
[DbContext(typeof(LearningDbContext))] [DbContext(typeof(LearningDbContext))]
[Migration("20250901155327_Initial")] [Migration("20250901180950_Initial")]
partial class Initial partial class Initial
{ {
/// <inheritdoc /> /// <inheritdoc />
@@ -39,15 +39,35 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
b.Property<bool>("IsDeleted") b.Property<bool>("IsDeleted")
.HasColumnType("bit"); .HasColumnType("bit");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(60)
.HasColumnType("nvarchar(60)");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Languages", "Learning"); 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 #pragma warning restore 612, 618
} }
} }
@@ -20,7 +20,6 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
{ {
Id = table.Column<long>(type: "bigint", nullable: false) Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"), .Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(60)", maxLength: 60, nullable: false),
IsDeleted = table.Column<bool>(type: "bit", nullable: false), IsDeleted = table.Column<bool>(type: "bit", nullable: false),
IsActive = 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); 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 /> /// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable(
name: "LanguageTitles",
schema: "Learning");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Languages", name: "Languages",
schema: "Learning"); schema: "Learning");
@@ -36,15 +36,35 @@ namespace VizhTalk.Learning.Infrastructure.Migrations
b.Property<bool>("IsDeleted") b.Property<bool>("IsDeleted")
.HasColumnType("bit"); .HasColumnType("bit");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(60)
.HasColumnType("nvarchar(60)");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Languages", "Learning"); 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 #pragma warning restore 612, 618
} }
} }
@@ -1,9 +1,4 @@
using System; using VizhTalk.BuildingBlocks.Infrastructure;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VizhTalk.BuildingBlocks.Infrastructure;
using VizhTalk.BuildingBlocks.Infrastructure.DomainEventsDispatching; using VizhTalk.BuildingBlocks.Infrastructure.DomainEventsDispatching;
using VizhTalk.Learning.Application; using VizhTalk.Learning.Application;
using VizhTalk.Learning.Domain.Repositories; using VizhTalk.Learning.Domain.Repositories;
@@ -26,5 +21,8 @@ namespace VizhTalk.Learning.Infrastructure
return _language; return _language;
} }
} }
public async Task SaveChangeAsync(CancellationToken cancellationToken = default)
=> await learningDbContext.SaveChangesAsync(cancellationToken);
} }
} }