From e8607f76faa087fadd66db413ad6acaf36ee56e0 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Fri, 4 Aug 2023 09:47:58 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=9A=E0=B8=B1?= =?UTF-8?q?=E0=B9=89=E0=B8=81=E0=B8=9A=E0=B8=B1=E0=B8=99=E0=B8=97=E0=B8=B6?= =?UTF-8?q?=E0=B8=81=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87?= =?UTF-8?q?=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=84=E0=B8=94=E0=B9=89=20?= =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1=E0=B8=81=E0=B8=B2?= =?UTF-8?q?=E0=B8=A3=E0=B9=83=E0=B8=AA=E0=B9=88=E0=B8=84=E0=B9=88=E0=B8=B2?= =?UTF-8?q?=E0=B8=9C=E0=B8=B9=E0=B9=89=E0=B8=97=E0=B8=B3=E0=B8=A3=E0=B8=B2?= =?UTF-8?q?=E0=B8=A2=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B9=83=E0=B8=99=20Generi?= =?UTF-8?q?cRepoository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IApplicationDBContext.cs | 2 ++ .../Commands/CommandRepository.cs | 9 +++++++ .../Repositories/GenericRepository.cs | 25 ++++++++++--------- .../Persistence/ApplicationDBContext.cs | 5 ++++ 4 files changed, 29 insertions(+), 12 deletions(-) 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); + } } }