From 680bfba81edfc720161b3681cc99d289a5ef75c6 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Fri, 4 Aug 2023 11:59:02 +0700 Subject: [PATCH] =?UTF-8?q?fix=20bug=20GetList=20=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=20=E0=B9=80=E0=B8=9E?= =?UTF-8?q?=E0=B8=B4=E0=B9=88=E0=B8=A1=20api=20=E0=B9=81=E0=B8=81=E0=B9=89?= =?UTF-8?q?=E0=B9=84=E0=B8=82=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88?= =?UTF-8?q?=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CommandRepository.cs | 89 +++++++++ .../Controllers/OrderController.cs | 171 +++++++++++++++++- BMA.EHR.Domain/Shared/GlobalConstants.cs | 2 +- BMA.EHR.Domain/Shared/GlobalMessages.cs | 2 + 4 files changed, 255 insertions(+), 9 deletions(-) diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index b69f52db..4a8718c8 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1,8 +1,10 @@ using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Models.Commands.Core; +using BMA.EHR.Domain.Models.MetaData; using BMA.EHR.Domain.Models.Organizations; using BMA.EHR.Domain.Models.Placement; using BMA.EHR.Domain.Shared; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Command = BMA.EHR.Domain.Models.Commands.Core.Command; @@ -44,6 +46,15 @@ namespace BMA.EHR.Application.Repositories.Commands .FirstOrDefaultAsync(x => x.Id == id); } + public override async Task> GetAllAsync() + { + return await _dbContext.Set() + .Include(x => x.Placement) + .Include(x => x.CommandType) + .Include(x => x.CommandStatus) + .ToListAsync(); + } + public override async Task AddAsync(Command command) { var status = await _dbContext.Set().FirstOrDefaultAsync(c => c.Sequence == 1); @@ -335,6 +346,54 @@ namespace BMA.EHR.Application.Repositories.Commands #region " Documents " + public async Task> GetExistDocument(Guid id, string category) + { + try + { + var command = await _dbContext.Set() + .Include(x => x.Documents) + .ThenInclude(x => x.Document) + .FirstOrDefaultAsync(c => c.Id == id); + if (command == null) + throw new Exception(GlobalMessages.CommandNotFound); + + // insert new record to comand Document + var exist = command.Documents.Where(x => x.Category == category).ToList(); + + return exist; + } + catch + { + throw; + } + } + + public async Task UploadDocument(Guid id, string category, CommandDocument document) + { + try + { + var command = await _dbContext.Set().Include(x => x.Documents).FirstOrDefaultAsync(c => c.Id == id); + if (command == null) + throw new Exception(GlobalMessages.CommandNotFound); + + // insert new record to comand Document + var exist = command.Documents.Where(x => x.Category == category).ToList(); + if (exist.Any()) + { + _dbContext.Set().RemoveRange(exist); + await _dbContext.SaveChangesAsync(); + } + + // insert new Record + command.Documents.Add(document); + await _dbContext.SaveChangesAsync(); + } + catch + { + throw; + } + } + public async Task> GetCommandDocumentAsync(Guid id) { try @@ -407,6 +466,36 @@ namespace BMA.EHR.Application.Repositories.Commands #endregion + #region " Change Detail Sequence " + + + #endregion + + #region " Placement " + + public async Task> GetPlacementPositionPath(Guid id) + { + try + { + var data = await _dbContext.Set() + .Include(x => x.PositionPath) + .Include(x => x.Placement) + .Where(x => x.Placement!.Id == id) + .Where(x => x.PositionPath != null) + .Select(x => x.PositionPath) + .Distinct() + .ToListAsync(); + + return data!; + } + catch + { + throw; + } + } + + #endregion + public async Task UpdateCommandInfo(Guid id, string orderNo, string orderYear, DateTime signDate) { try diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index 1d19f681..66aa17f9 100644 --- a/BMA.EHR.Command.Service/Controllers/OrderController.cs +++ b/BMA.EHR.Command.Service/Controllers/OrderController.cs @@ -33,8 +33,7 @@ namespace BMA.EHR.Command.Service.Controllers private readonly IHttpContextAccessor _httpContextAccessor; private readonly PrefixRepository _prefixRepository; private readonly CommandTypeRepository _commandTypeRepository; - - + private readonly CommandStatusRepository _commandStatusRepository; #endregion @@ -46,7 +45,8 @@ namespace BMA.EHR.Command.Service.Controllers MinIOService documentService, IHttpContextAccessor httpContextAccessor, PrefixRepository prefixRepository, - CommandTypeRepository commandTypeRepository) + CommandTypeRepository commandTypeRepository, + CommandStatusRepository commandStatusRepository) { _repository = repository; _context = context; @@ -55,6 +55,7 @@ namespace BMA.EHR.Command.Service.Controllers _placementRepository = placementRepository; _prefixRepository = prefixRepository; _commandTypeRepository = commandTypeRepository; + _commandStatusRepository = commandStatusRepository; } #endregion @@ -159,7 +160,7 @@ namespace BMA.EHR.Command.Service.Controllers throw new Exception(GlobalMessages.CommandNotFound); var cover = command.Documents.FirstOrDefault(x => x.Category == GlobalConstants.TYPE_COVER); - var attatchment = command.Documents.FirstOrDefault(x => x.Category == GlobalConstants.TYPE_ATTATCHMENT); + var attatchment = command.Documents.FirstOrDefault(x => x.Category == GlobalConstants.TYPE_ATTACHMENT); if (command.CommandNo != "" && command.CommandYear != null && @@ -250,6 +251,7 @@ namespace BMA.EHR.Command.Service.Controllers var data = (await _repository.GetAllAsync()) .Select(d => new { + OrderId = d.Id, OrderName = d.CommandSubject, OrderNo = d.CommandNo, FiscalYear = d.CommandYear, @@ -310,7 +312,7 @@ namespace BMA.EHR.Command.Service.Controllers /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpGet("{orderId}")] + [HttpGet("detail/{orderId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] @@ -333,7 +335,7 @@ namespace BMA.EHR.Command.Service.Controllers orderBy = data.IssuerOrganizationId, signatoryBy = data.AuthorizedUserFullName, signatoryPosition = data.AuthorizedPosition, - examRound = data.Placement.Round, + examRound = data.Placement.Id, registerPosition = "", conclusionRegisterNo = data.ConclusionRegisterNo, conclusionRegisterDate = data.ConclusionRegisterDate, @@ -357,7 +359,7 @@ namespace BMA.EHR.Command.Service.Controllers /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpPost] + [HttpPost("detail")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] @@ -392,6 +394,56 @@ namespace BMA.EHR.Command.Service.Controllers } } + /// + /// PM7-23 : แก้ไขข้อมูลรายละเอียดการออกคำสั่ง + /// + /// Record Id ของคำสั่ง + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPut("detail/{orderId}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> PutAsync(Guid orderId, [FromBody] CreateCommandRequest req) + { + try + { + var order = await _repository.GetByIdAsync(orderId); + if (order == null) + throw new Exception(GlobalMessages.CommandNotFound); + + var placement = await _placementRepository.GetByIdAsync(req.examRound); + var commandType = await _commandTypeRepository.GetByIdAsync(req.orderTypeValue); + var status = await _commandStatusRepository.GetByIdAsync(order.CommandStatusId); + + order.CommandNo = req.orderNo.ToString(); + order.CommandYear = req.orderYear.ToString(); + order.CommandSubject = req.orderTitle; + order.PositionName = req.registerPosition; + order.CommandType = commandType!; + order.IssuerOrganizationId = req.orderBy; + order.AuthorizedUserFullName = req.signatoryBy; + order.AuthorizedPosition = req.signatoryPosition; + order.Placement = placement!; + order.ConclusionRegisterNo = req.conclusionRegisterNo; + order.ConclusionRegisterDate = req.conclusionRegisterDate; + order.ConclusionResultNo = req.conclusionResultNo; + order.ConclusionResultDate = req.conclusionResultDate; + order.CommandStatus = status!; + + var result = await _repository.UpdateAsync(order); + + return Success(result); + } + catch + { + throw; + } + } + + /// /// PM7-24 : dropdown รอบการสอบ หน้ารายละเอียดการออกคำสั่ง /// @@ -688,7 +740,7 @@ namespace BMA.EHR.Command.Service.Controllers var documents = await _repository.GetCommandDocumentAsync(orderId); var cover = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_COVER).FirstOrDefault(); - var attach = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_ATTATCHMENT).FirstOrDefault(); + var attach = documents.Where(x => x.Category.Trim().ToLower() == GlobalConstants.TYPE_ATTACHMENT).FirstOrDefault(); var result = new { @@ -707,6 +759,109 @@ namespace BMA.EHR.Command.Service.Controllers } } + + /// + /// PM7-35 : อัปโหลดไฟล์คำสั่ง + /// + /// Record Id ของคำสั่ง + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("attachment/order-file/{orderId}"), DisableRequestSizeLimit] + public async Task> UploadCommandCoverAsync(Guid orderId) + { + try + { + // check upload file + if (Request.Form.Files == null || Request.Form.Files.Count == 0) + { + return Error(GlobalMessages.NoFileToUpload); + } + + var file = Request.Form.Files[0]; + + // get exit file + var docs = await _repository.GetExistDocument(orderId, GlobalConstants.TYPE_COVER); + + // delete exist document from s3 + foreach (var doc in docs) + { + await _documentService.DeleteFileAsync(doc.Document.ObjectRefId); + } + + // upload new document to s3 + var cover = await _documentService.UploadFileAsync(file); + + // create new CommandDocumentEntity + var commandDoc = new CommandDocument + { + Category = GlobalConstants.TYPE_COVER, + Document = cover, + }; + + // send to repo to save in database + await _repository.UploadDocument(orderId, GlobalConstants.TYPE_COVER, commandDoc); + + return Success(); + } + catch + { + throw; + } + } + + /// + /// PM7-36 : อัปโหลดไฟล์เอกสารแนบท้าย + /// + /// Record Id ของคำสั่ง + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("attachment/file/{orderId}"), DisableRequestSizeLimit] + public async Task> UploadCommandAttachmentAsync(Guid orderId) + { + try + { + // check upload file + if (Request.Form.Files == null || Request.Form.Files.Count == 0) + { + return Error(GlobalMessages.NoFileToUpload); + } + + var file = Request.Form.Files[0]; + + // get exit file + var docs = await _repository.GetExistDocument(orderId, GlobalConstants.TYPE_ATTACHMENT); + + // delete exist document from s3 + foreach (var doc in docs) + { + await _documentService.DeleteFileAsync(doc.Document.ObjectRefId); + } + + // upload new document to s3 + var cover = await _documentService.UploadFileAsync(file); + + // create new CommandDocumentEntity + var commandDoc = new CommandDocument + { + Category = GlobalConstants.TYPE_ATTACHMENT, + Document = cover, + }; + + // send to repo to save in database + await _repository.UploadDocument(orderId, GlobalConstants.TYPE_ATTACHMENT, commandDoc); + + return Success(); + } + catch + { + throw; + } + } + #endregion /// diff --git a/BMA.EHR.Domain/Shared/GlobalConstants.cs b/BMA.EHR.Domain/Shared/GlobalConstants.cs index d89f50ec..22ed9b28 100644 --- a/BMA.EHR.Domain/Shared/GlobalConstants.cs +++ b/BMA.EHR.Domain/Shared/GlobalConstants.cs @@ -2,7 +2,7 @@ { public class GlobalConstants { - public static readonly string TYPE_ATTATCHMENT = "attachment"; + public static readonly string TYPE_ATTACHMENT = "attachment"; public static readonly string TYPE_COVER = "cover"; } } diff --git a/BMA.EHR.Domain/Shared/GlobalMessages.cs b/BMA.EHR.Domain/Shared/GlobalMessages.cs index 0c5c2e77..81bdf617 100644 --- a/BMA.EHR.Domain/Shared/GlobalMessages.cs +++ b/BMA.EHR.Domain/Shared/GlobalMessages.cs @@ -16,6 +16,8 @@ public static readonly string FileNotFoundOnServer = "ไม่พบไฟล์ในระบบ!!"; + public static readonly string NoFileToUpload = "ไม่พบไฟล์ที่ทำการอัพโหลด!"; + #region " Meta Data " public static readonly string DataExist5 = "เนื่องจากมีการกำหนดวันหยุดในการทำงาน 5 วันอยู่";