private bool IsBulkUserFileValid(BulkUserFile bulkUserFile)
        {
            var retVal = true;

            try
            {
                if (bulkUserFile == null)
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(bulkUserFile.FileName))
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(bulkUserFile.FileSize))
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(bulkUserFile.Action))
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                retVal = false;
                _logger.LogError("BulkUploadController-IsBulkUserFileValid: Exception occurred...");
                _logger.LogError(ex);
            }

            return(retVal);
        }
Example #2
0
 public void UpdateTable(BulkUserFile bulkUserFile, string status)
 {
     try
     {
         if (_cc != null)
         {
             bulkUserFile.Status = status;
             _cc.Update(bulkUserFile);
             _cc.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         _logger.LogError($"DbHelper-UpdateTable:Exception occured while updating the bulk user file for id {bulkUserFile.Id}");
         _logger.LogError(ex);
     }
 }
        public async Task <IActionResult> Index(List <IFormFile> files, string id)
        {
            try
            {
                BulkUserFile    bulkUserFile = null;
                List <BulkUser> bulkUsers    = null;

                ClaimsPrincipal currentUser = this.User;
                var             actionFor   = string.IsNullOrEmpty(id) ? CareStreamConst.Bulk_User_Create : id;

                foreach (var formFile in files)
                {
                    try
                    {
                        if (formFile.Length > 0)
                        {
                            bulkUserFile = new BulkUserFile();

                            var filePath = Path.GetTempFileName();

                            using (var stream = new FileStream(filePath, FileMode.Create))
                            {
                                await formFile.CopyToAsync(stream);
                            }

                            TextReader reader = new StreamReader(filePath);
                            var        csv    = new CsvReader(reader, System.Globalization.CultureInfo.InvariantCulture);
                            csv.Read();
                            csv.ReadHeader();

                            bulkUserFile.Action      = actionFor;
                            bulkUserFile.CreatedDate = DateTime.Now;
                            bulkUserFile.FileName    = formFile.FileName;
                            bulkUserFile.FileSize    = formFile.Length.ToString();
                            bulkUserFile.Status      = CareStreamConst.Bulk_User_Loaded_Status;
                            bulkUserFile.UploadBy    = string.IsNullOrEmpty(currentUser.Identity.Name) ? CareStreamConst.Bulk_User_UploadedBy : currentUser.Identity.Name;

                            bulkUsers = ProcessCSVAndCreateDbObject(csv, actionFor);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"BulkUploadController-Index: Error reading the file name {formFile.FileName} ");
                        _logger.LogError(ex);
                    }
                }

                if (bulkUserFile != null)
                {
                    if (IsBulkUserFileValid(bulkUserFile))
                    {
                        try
                        {
                            _cc.Add(bulkUserFile);
                            var result = _cc.SaveChanges();

                            if (result > 0)
                            {
                                var fileId = bulkUserFile.Id;
                                if (bulkUsers != null)
                                {
                                    if (bulkUsers.Any())
                                    {
                                        foreach (var bulkUser in bulkUsers)
                                        {
                                            try
                                            {
                                                bulkUser.FileId = fileId;
                                                if (IsBulkUserValid(bulkUser, actionFor))
                                                {
                                                    _cc.Add(bulkUser);
                                                    var bulkUserResult = _cc.SaveChanges();

                                                    if (bulkUserResult == 0)
                                                    {
                                                        _logger.LogWarn($"BulkUploadController-Index: Unable to save bulk user for action {bulkUser.Action} and file id {fileId}");
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                _logger.LogError($"BulkUploadController-Index: Error saving the user detail for action {bulkUser.Action}");
                                                _logger.LogError(ex);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"BulkUploadController-Index: Error saving the file name {bulkUserFile.FileName} ");
                            _logger.LogError(ex);
                        }
                    }
                }

                ShowSuccessMessage("Succssfully uploaded the file.");
            }
            catch (Exception ex)
            {
                ShowErrorMessage("File upload failed: " + ex.Message);
                _logger.LogError("BulkUploadController-Index: Exception occurred...");
                _logger.LogError(ex);
            }

            return(RedirectToAction(nameof(Upload), new { id = id }));
        }