Esempio n. 1
0
        public HttpResponseMessage DownloadBatchFiles(BatchSources batchSources)
        {
            try
            {
                downloadManager.Process(batchSources.Sources);


                return(Request.CreateResponse(HttpStatusCode.OK, "chack after few minutes te downloaded files"));
            }
            catch (ArgumentException ex)
            {
                logger.AddErrorLog(ex);


                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
            catch (Exception ex)
            {
                //handle exception

                logger.AddErrorLog(ex);

                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Esempio n. 2
0
 public Restaurant InsertRestaurant(Restaurant restaurant)
 {
     using (var conn = GetConnection())
     {
         _logger.AddInfoLog("Establishing connection to dabatase");
         try
         {
             _logger.AddInfoLog("");
             conn.Open();
             var cmd = new MySqlCommand($"insert into restaurants values({restaurant.ToString()})", conn);
             cmd.ExecuteNonQuery();
         }
         catch (Exception e)
         {
             _logger.AddErrorLog($"Error while inserting new restaurant: {restaurant.Name}" + e.Message);
         }
     }
     return(restaurant);
 }
Esempio n. 3
0
 public User AddUser(User user)
 {
     using (var conn = GetConnection())
     {
         try
         {
             _logger.AddInfoLog("Establishing connection with database");
             conn.Open();
             _logger.AddInfoLog($"Inserting new user: {user.ToString()} to database");
             var cmd = new MySqlCommand($"insert into users values({user.ToString()})", conn);
             cmd.ExecuteNonQuery();
         }
         catch (Exception e)
         {
             _logger.AddErrorLog($"Error while inserting new user: {user.ToString()}" + e.Message);
         }
     }
     return(user);
 }
Esempio n. 4
0
 public Comment InsertComment(Comment comment)
 {
     using (var conn = GetConnection())
     {
         _logger.AddInfoLog("Establishing connection to database");
         try
         {
             conn.Open();
             var cmd = new MySqlCommand($"insert into comments values({comment.ToString()})", conn);
             _logger.AddInfoLog("Inserting new comment into database");
             cmd.ExecuteNonQuery();
         }catch (Exception e)
         {
             _logger.AddErrorLog("Error while inserting comennt into database" + e.Message);
         }
     }
     return(comment);
 }
Esempio n. 5
0
        public void Process(string sources)
        {
            //TODO : Move this logic to separate windows service and make the service listen on Message Queue like Rabbitmq

            logger.AddInformationLog($"sources value: {sources}");

            //Data validations
            if (string.IsNullOrWhiteSpace(sources))
            {
                throw new ArgumentException($"The sources Is null or empty string.");
            }

            string delimiter = ConfigurationManager.AppSettings["Delimiter"];

            logger.AddInformationLog($"Delimiter config value: {delimiter}");

            string sourcesParsingDelimiter = string.IsNullOrWhiteSpace(delimiter) ? "," : delimiter;

            logger.AddInformationLog($"Sources parsing delimiter: {sourcesParsingDelimiter}");

            string localPath = ConfigurationManager.AppSettings["LocalDirectory"];

            logger.AddInformationLog($"LocalDirectory config value: {localPath}");

            List <string> soursesUrls = parser.Parse(sources, sourcesParsingDelimiter).ToList();

            logger.AddInformationLog($"sourses Urls counts after parsing: {soursesUrls?.Count}");

            ConcurrentQueue <Exception> exceptions = new ConcurrentQueue <Exception>();

            using (repoDownloadedFile)
            {
                Parallel.ForEach(soursesUrls, url =>
                {
                    try
                    {
                        IDownloadStrategy downloadStrategy = downloadStrategyFactory.Build(url);

                        string physicalPath = downloadStrategy.Download(url, localPath);
                        string virtualPath  = $"{localPath.Replace('\\', '/')}/{Path.GetFileName(physicalPath)}";
                        logger.AddInformationLog($"Local path is: {localPath} for URL: {url} ");

                        repoDownloadedFile.Add(new DomainModels.DownloadedFile()
                        {
                            FileRemotePath   = url,
                            LocalPath        = virtualPath,
                            ProcessingStatus = (int)DownloadStatutes.ReadyForProcessing,
                        });

                        repoDownloadedFile.SaveChanges();

                        logger.AddInformationLog($"Source :{url} saved to the database with status ready for processing");
                    }
                    catch (Exception ex)
                    {
                        exceptions.Enqueue(ex);
                        logger.AddErrorLog(ex);
                    }
                });

                if (exceptions.Any())
                {
                    throw new AggregateException(exceptions);
                }
            }
        }
Esempio n. 6
0
 public virtual void AddErrorLog(LogData requestResponseObject)
 {
     _logger.AddErrorLog(requestResponseObject);
 }
Esempio n. 7
0
        private string DownloadSFTPFile(string host, int port, string username, string password, string sftpInboundPath, string filename)
        {
            try
            {
                using (SftpClient client = new SftpClient(host, port, username, password))
                {
                    logger.AddInformationLog(nameof(client) + " is created.");

                    client.Connect();

                    logger.AddInformationLog(nameof(client) + " is connected.");

                    if (client.IsConnected)
                    {
                        logger.AddInformationLog("FTP Connection established.");

                        string tempFileDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp");

                        logger.AddInformationLog(nameof(tempFileDirPath) + ": " + tempFileDirPath);

                        DirectoryInfo tempFileDir = new DirectoryInfo(tempFileDirPath);

                        logger.AddInformationLog(nameof(tempFileDir) + " object is created.");

                        if (!tempFileDir.Exists)
                        {
                            logger.AddInformationLog(nameof(tempFileDir) + " not exists.");

                            tempFileDir.Create();

                            logger.AddInformationLog(nameof(tempFileDir) + " created.");
                        }

                        string tempFilePath = $"{tempFileDir.FullName}\\{filename}";

                        logger.AddInformationLog(nameof(tempFilePath) + ": " + tempFilePath);

                        // Delete the file if exists.
                        if (File.Exists(tempFilePath))
                        {
                            logger.AddInformationLog(nameof(tempFilePath) + " exists.");

                            File.Delete(tempFilePath);

                            logger.AddInformationLog(nameof(tempFilePath) + " deleted.");
                        }

                        using (FileStream file = File.OpenWrite(tempFilePath))
                        {
                            logger.AddInformationLog(nameof(file) + " opened.");

                            string bufferSize = ConfigurationManager.AppSettings["BufferSize"];//4096

                            client.BufferSize = string.IsNullOrWhiteSpace(bufferSize) ? 4096 : Convert.ToUInt32(bufferSize);
                            logger.AddInformationLog($"BufferSize config value: {client?.BufferSize}");

                            client.DownloadFile($"{sftpInboundPath}{filename}", file);

                            logger.AddInformationLog($"file download from sftp success at path : {tempFilePath}");
                        }

                        return(tempFilePath);
                    }
                    else
                    {
                        logger.AddErrorLog("Unable to establish FTP Connection.");
                    }
                    return(null);
                }
            }
            catch (Exception ex)
            {
                logger.AddErrorLog("Error downloading the file via FTP.", ex);

                throw;
            }
        }