public async Task <WorkflowGroupRegRespObj> Handle(AddUpdateWorkflowGroupCommand request, CancellationToken cancellationToken) { try { if (request.WorkflowGroupId < 1) { if (await _repo.WorkflowGroupExistAsync(request.WorkflowGroupName)) { return new WorkflowGroupRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Workflow gorup already exist", } } } } } ; var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _userManger.FindByIdAsync(currentUserId); var approvalGroup = new cor_workflowgroup { WorkflowGroupName = request.WorkflowGroupName, Active = true, Deleted = false, CreatedBy = user.UserName, CreatedOn = DateTime.Now, UpdatedBy = user.UserName, UpdatedOn = DateTime.Now, }; if (request.WorkflowGroupId > 0) { approvalGroup.WorkflowGroupId = request.WorkflowGroupId; } await _repo.AddUpdateWorkflowGroupAsync(approvalGroup); var actionTaken = request.WorkflowGroupId > 0 ? "updated" : "added"; return(new WorkflowGroupRegRespObj { WorkflowGroupId = approvalGroup.WorkflowGroupId, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = $"Successfully {actionTaken}", } } }); } catch (Exception ex) { #region Log error to file var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : AddUpdateWorkflowGroupCommandHandler{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new WorkflowGroupRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Unable to process item", MessageId = errorCode, TechnicalMessage = $"ErrorID : AddUpdateWorkflowGroupCommandHandler{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <FileUploadRespObj> Handle(UploadWorkflowGroupCommand request, CancellationToken cancellationToken) { try { var apiResponse = new FileUploadRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage() } }; var files = _httpContextAccessor.HttpContext.Request.Form.Files; var byteList = new List <byte[]>(); foreach (var fileBit in files) { if (fileBit.Length > 0) { using (var ms = new MemoryStream()) { await fileBit.CopyToAsync(ms); byteList.Add(ms.ToArray()); } } } var uploadedRecord = new List <WorkflowGroupObj>(); ExcelPackage.LicenseContext = LicenseContext.NonCommercial; if (byteList.Count() > 0) { foreach (var byteItem in byteList) { using (MemoryStream stream = new MemoryStream(byteItem)) using (ExcelPackage excelPackage = new ExcelPackage(stream)) { //Use first sheet by default ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[0]; int totalRows = workSheet.Dimension.Rows; int totalColumns = workSheet.Dimension.Columns; if (totalColumns != 1) { apiResponse.Status.Message.FriendlyMessage = $"One (1) Column Expected"; return(apiResponse); } //First row is considered as the header for (int i = 2; i <= totalRows; i++) { var lkp = new WorkflowGroupObj { ExcelLineNumber = i, WorkflowGroupName = workSheet.Cells[i, 1]?.Value != null ? workSheet.Cells[i, 1]?.Value.ToString() : string.Empty, }; uploadedRecord.Add(lkp); } } } } var _DomainList = await _repo.GetAllWorkflowGroupAsync(); if (uploadedRecord.Count > 0) { foreach (var item in uploadedRecord) { if (string.IsNullOrEmpty(item.WorkflowGroupName)) { apiResponse.Status.Message.FriendlyMessage = $"Workflow Group Name can not be empty detected on line {item.ExcelLineNumber}"; return(apiResponse); } var currentItem = _DomainList.FirstOrDefault(s => s.WorkflowGroupName.Trim().ToLower() == item.WorkflowGroupName.Trim().ToLower() && s.Deleted == false); if (currentItem != null) { currentItem.WorkflowGroupName = currentItem.WorkflowGroupName; await _repo.AddUpdateWorkflowGroupAsync(currentItem); } else { var newItem = new cor_workflowgroup(); newItem.WorkflowGroupName = item.WorkflowGroupName; await _repo.AddUpdateWorkflowGroupAsync(newItem); } } } apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message.FriendlyMessage = "Successful"; return(apiResponse); } catch (Exception ex) { throw new Exception(ex.Message); } }