diff --git a/BMA.EHR.Application/Common/Interfaces/IApplicationDBContext.cs b/BMA.EHR.Application/Common/Interfaces/IApplicationDBContext.cs index 2422892b..ab32d1af 100644 --- a/BMA.EHR.Application/Common/Interfaces/IApplicationDBContext.cs +++ b/BMA.EHR.Application/Common/Interfaces/IApplicationDBContext.cs @@ -7,6 +7,8 @@ namespace BMA.EHR.Application.Common.Interfaces { DbSet Set() where T : class; + void Attatch(T entity) where T : class; + Task SaveChangesAsync(); } } diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index d8914f35..c8c9e2e5 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -42,6 +42,15 @@ namespace BMA.EHR.Application.Repositories.Commands .FirstOrDefaultAsync(x => x.Id == id); } + public override async Task AddAsync(Command command) + { + var status = await _dbContext.Set().FirstOrDefaultAsync(c => c.Sequence == 1); + command.CommandStatus = status!; + _dbContext.Attatch(status!); + + return await base.AddAsync(command); + } + #endregion public async Task> GetReceiverByCommmandIdAsync(Guid Id) diff --git a/BMA.EHR.Application/Repositories/GenericRepository.cs b/BMA.EHR.Application/Repositories/GenericRepository.cs index c196430c..f752c581 100644 --- a/BMA.EHR.Application/Repositories/GenericRepository.cs +++ b/BMA.EHR.Application/Repositories/GenericRepository.cs @@ -1,4 +1,5 @@ using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Domain.Models.Base; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using System.Security.Claims; @@ -49,12 +50,12 @@ namespace BMA.EHR.Application.Repositories public virtual async Task AddAsync(T entity) { - //if (entity is IAuditableEntity) - //{ - // (entity as IAuditableEntity).CreatedUserId = Guid.Parse(UserId); - // (entity as IAuditableEntity).CreatedUserFullName = FullName; - // (entity as IAuditableEntity).CreatedDate = DateTime.Now; - //} + if (entity is EntityBase) + { + (entity as EntityBase).CreatedUserId = UserId!; + (entity as EntityBase).CreatedFullName = FullName!; + (entity as EntityBase).CreatedAt = DateTime.Now; + } await _dbSet.AddAsync(entity); @@ -65,12 +66,12 @@ namespace BMA.EHR.Application.Repositories public virtual async Task UpdateAsync(T entity) { - //if (entity is IAuditableEntity) - //{ - // (entity as IAuditableEntity).ModifiedUserId = Guid.Parse(UserId); - // (entity as IAuditableEntity).ModifiedUserFullName = FullName; - // (entity as IAuditableEntity).ModifiedDate = DateTime.Now; - //} + if (entity is EntityBase) + { + (entity as EntityBase).LastUpdateUserId = UserId!; + (entity as EntityBase).LastUpdateFullName = FullName!; + (entity as EntityBase).LastUpdatedAt = DateTime.Now; + } _dbSet.Update(entity); await _dbContext.SaveChangesAsync(); diff --git a/BMA.EHR.Infrastructure/Persistence/ApplicationDBContext.cs b/BMA.EHR.Infrastructure/Persistence/ApplicationDBContext.cs index b41d8822..6b57e77d 100644 --- a/BMA.EHR.Infrastructure/Persistence/ApplicationDBContext.cs +++ b/BMA.EHR.Infrastructure/Persistence/ApplicationDBContext.cs @@ -313,5 +313,10 @@ namespace BMA.EHR.Infrastructure.Persistence { return base.SaveChangesAsync(); } + + public void Attatch(T entity) where T : class + { + Attach(entity); + } } }