diff --git a/Application/Cqrs/Campus/Commands/CreateCampus/CreateCampusCommand.cs b/Application/Cqrs/Campus/Commands/CreateCampus/CreateCampusCommand.cs new file mode 100644 index 0000000..5c93d26 --- /dev/null +++ b/Application/Cqrs/Campus/Commands/CreateCampus/CreateCampusCommand.cs @@ -0,0 +1,10 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Commands.CreateCampus; + +public class CreateCampusCommand : IRequest +{ + public required string CodeName { get; set; } + public string? FullName { get; set; } + public string? Address { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Commands/CreateCampus/CreateCampusCommandHandler.cs b/Application/Cqrs/Campus/Commands/CreateCampus/CreateCampusCommandHandler.cs new file mode 100644 index 0000000..0b796bc --- /dev/null +++ b/Application/Cqrs/Campus/Commands/CreateCampus/CreateCampusCommandHandler.cs @@ -0,0 +1,31 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Application.Common.Exceptions; +using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using System.Threading; +using System.Threading.Tasks; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Commands.CreateCampus; + +public class CreateCampusCommandHandler(ICampusDbContext dbContext) : IRequestHandler +{ + public async Task Handle(CreateCampusCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Campuses.FirstOrDefaultAsync(c => c.CodeName == request.CodeName, cancellationToken: cancellationToken); + + if (entity != null) + throw new RecordExistException(typeof(Domain.Schedule.Campus), nameof(Domain.Schedule.Campus.CodeName), entity.Id); + + var campus = new Domain.Schedule.Campus() + { + CodeName = request.CodeName, + FullName = request.FullName, + Address = request.Address + }; + + var result = await dbContext.Campuses.AddAsync(campus, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + + return result.Entity.Id; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommand.cs b/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommand.cs new file mode 100644 index 0000000..c6d54f0 --- /dev/null +++ b/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommand.cs @@ -0,0 +1,10 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Commands.UpdateCampus; + +public class UpdateCampusCommand : IRequest +{ + public required int Id { get; set; } + public string? FullName { get; set; } + public string? Address { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommandHandler.cs b/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommandHandler.cs new file mode 100644 index 0000000..6c93195 --- /dev/null +++ b/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommandHandler.cs @@ -0,0 +1,23 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Application.Common.Exceptions; +using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using System.Threading; +using System.Threading.Tasks; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Commands.UpdateCampus; + +public class UpdateCampusCommandHandler(ICampusDbContext dbContext) : IRequestHandler +{ + public async Task Handle(UpdateCampusCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Campuses.FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Campus), nameof(Domain.Schedule.Campus.Id), request.Id); + + if (!string.IsNullOrEmpty(request.FullName)) + entity.FullName = request.FullName; + if (!string.IsNullOrEmpty(request.Address)) + entity.Address = request.Address; + + await dbContext.SaveChangesAsync(cancellationToken); + } +} \ No newline at end of file