public async Task <LookUpRegRespObj> Handle(AddUpdateIdentificationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.IdentificationId < 1)
                {
                    if (await _dataContext.cor_identification.AnyAsync(x => x.IdentificationName.Trim().ToLower() == request.IdentificationName.Trim().ToLower() && x.Deleted == false))
                    {
                        return(new LookUpRegRespObj
                        {
                            Status = new APIResponseStatus
                            {
                                IsSuccessful = false,
                                Message = new APIResponseMessage
                                {
                                    FriendlyMessage = "This Name Already Exist"
                                }
                            }
                        });
                    }
                }
                var item = new cor_identification
                {
                    Active             = true,
                    CreatedOn          = request.CreatedOn,
                    Deleted            = false,
                    CreatedBy          = request.CreatedBy,
                    IdentificationName = request.IdentificationName,
                };
                if (request.IdentificationId > 0)
                {
                    item.IdentificationId = request.IdentificationId;
                    item.UpdatedOn        = DateTime.Today;
                }
                var addedOrUpdated = await _repo.AddUpdateIdentificationAsync(item);

                if (!addedOrUpdated)
                {
                    return new LookUpRegRespObj
                           {
                               Status = new APIResponseStatus
                               {
                                   IsSuccessful = false,
                                   Message      = new APIResponseMessage
                                   {
                                       FriendlyMessage = "Unable to add item"
                                   }
                               }
                           }
                }
                ;
                return(new LookUpRegRespObj
                {
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = true,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = "Successful"
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                #region Log error to file
                return(new LookUpRegRespObj
                {
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = false,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = "Error occured!! Unable to process request",
                            TechnicalMessage = $"{ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"
                        }
                    }
                });

                #endregion
            }
        }
Exemplo n.º 2
0
            public async Task <FileUploadRespObj> Handle(UploadIdentificationCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    var apiResponse = new FileUploadRespObj {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage()
                        }
                    };

                    var files = _accessor.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 _Identification = await _repo.GetAllIdentificationAsync();

                    var uploadedRecord = new List <CommonLookupsObj>();
                    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) Columns Expected";
                                        return(apiResponse);
                                    }
                                    //First row is considered as the header
                                    for (int i = 2; i <= totalRows; i++)
                                    {
                                        var lkp = new CommonLookupsObj
                                        {
                                            ExcelLineNumber = i,
                                            LookupName      = workSheet.Cells[i, 1].Value.ToString()
                                        };
                                        uploadedRecord.Add(lkp);
                                    }
                                }
                        }
                    }


                    var _IdentificationList = await _repo.GetAllIdentificationAsync();

                    if (uploadedRecord.Count > 0)
                    {
                        foreach (var item in uploadedRecord)
                        {
                            if (string.IsNullOrEmpty(item.LookupName))
                            {
                                apiResponse.Status.Message.FriendlyMessage = $"Document type name can not be empty detected on line {item.ExcelLineNumber}";
                                return(apiResponse);
                            }
                            var currentIdentification = _IdentificationList.FirstOrDefault(c => c.IdentificationName.ToLower().Trim() == item.LookupName.ToLower().Trim() && c.Deleted == false);

                            if (currentIdentification == null)
                            {
                                var Identification = new cor_identification();
                                Identification.IdentificationName = item.LookupName;
                                await _repo.AddUpdateIdentificationAsync(Identification);
                            }
                            else
                            {
                                currentIdentification.IdentificationName = item.LookupName;
                                await _repo.AddUpdateIdentificationAsync(currentIdentification);
                            }
                        }
                    }
                    apiResponse.Status.IsSuccessful            = true;
                    apiResponse.Status.Message.FriendlyMessage = "Successful";
                    return(apiResponse);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }