public void ProcessInputFile(InputFile inputFile)
        {
            try
            {
                var fullPath = string.Concat(inputFile.FilePath, "/", inputFile.FileName);

                if (VerifyFileExistence(inputFile, fullPath))
                {
                    return;
                }

                var fileContent = File.ReadAllLines(fullPath);

                if (fileContent == null || fileContent.Length == 0)
                {
                    ProcessFileFailed(inputFile);
                    return;
                }

                var contentDto = new FileContentDto {
                    InputFile = inputFile
                };

                contentDto.InputFile.Processed   = true;
                contentDto.InputFile.Canceled    = false;
                contentDto.InputFile.ProcessDate = DateTime.Now;

                foreach (var line in fileContent)
                {
                    if (line.StartsWith(_configuration["SalesmanIdentifier"]))
                    {
                        AddSalesman(line, contentDto);
                    }
                    if (line.StartsWith(_configuration["CustomerIdentifier"]))
                    {
                        AddCustomer(line, contentDto);
                    }
                    if (line.StartsWith(_configuration["SaleIdentifier"]))
                    {
                        AddSale(line, contentDto);
                    }
                }

                if (!contentDto.Customers.Any() &&
                    !contentDto.Sales.Any() &&
                    !contentDto.Salesmen.Any())
                {
                    ProcessFileFailed(inputFile);
                    return;
                }

                _salesDataProcessor.SaveContentToDatabase(contentDto);
            }
            catch (Exception exception)
            {
                _logger.LogCritical("An unexpected exception occurred when processing input file {FileName}", inputFile.FileName);
                _logger.LogCritical("Exception {message}", exception.Message);
                _logger.LogTrace(exception.StackTrace);
            }
        }
Exemplo n.º 2
0
        public void SaveContentToDatabase(FileContentDto content)
        {
            try
            {
                AddOrUpdateFile(content.InputFile);

                AddOrUpdateSalesman(content.Salesmen);

                AddOrUpdateCustomer(content.Customers);

                AddOrUpdateSales(content.Sales, content.InputFile.FileName);

                var saved = _context.SaveChanges();

                if (saved > 0)
                {
                    _processor.BuildOutputData(content);
                }
            }
            catch (Exception exception)
            {
                _logger.LogError("An unexpected exception occurred while processing view model data.");
                _logger.LogError("Exceptiom {message}", exception.Message);
                _logger.LogTrace(exception.StackTrace);
            }
        }
        public void BuildOutputData(FileContentDto fileContent)
        {
            try
            {
                _outputData = new OutputDataDto
                {
                    FileName         = fileContent.InputFile.FileName
                    , FileExtension  = fileContent.InputFile.FileExtension
                    , GenerationDate = DateTime.Now
                }
                .GetCustomersQuantity(fileContent)
                .GetSalesmenQuantity(fileContent)
                .GetIdFromMostExpensiveSale(fileContent
                                            , _context)
                .GetWorstSalesman(fileContent);

                _clientPublisher.Publish(_outputData
                                         , _configuration["RabbitMqHostName"]
                                         , _configuration["RabbitMqUsername"]
                                         , _configuration["RabbitMqPassword"]
                                         , int.Parse(_configuration["RabbitMqRetryCount"])
                                         , _configuration["RabbitMqPublishQueueName"]);
            }
            catch (Exception exception)
            {
                _logger.LogCritical("An unexpected exception occurred when building output data object");
                _logger.LogCritical("Exception: {message}", exception.Message);
                _logger.LogTrace(exception.Message);
            }
        }
Exemplo n.º 4
0
        public static OutputDataDto GetWorstSalesman(this OutputDataDto outputDto, FileContentDto contentDto)
        {
            var worstSalesman = contentDto.Sales.DistinctBy(x => x.SalesmanName).Min(x => x.SalesmanName);

            outputDto.WorstSalesman = worstSalesman;

            return(outputDto);
        }
Exemplo n.º 5
0
        protected override Task HandleCore(UploadFileCommand message)
        {
            var dto = new FileContentDto
            {
                Created = message.Created,
                Data    = message.Data,
                Id      = message.Id,
                Name    = message.Name,
                Size    = message.Data.Length,
                Type    = message.Type,
                UserId  = message.UserId
            };

            return(_fileRepository.Save(dto));
        }
        private void AddCustomer(string line, FileContentDto viewModel)
        {
            var customer = GetStandardLine(line);

            if (customer == null)
            {
                return;
            }
            viewModel.Customers.Add(new Customer
            {
                Cnpj         = customer.Value.first,
                Name         = customer.Value.second,
                BusinessArea = customer.Value.third.ToString()
            });
        }
        private void AddSalesman(string line, FileContentDto viewModel)
        {
            var salesman = GetStandardLine(line);

            if (salesman == null)
            {
                return;
            }

            viewModel.Salesmen.Add(new Salesman
            {
                Cpf    = salesman.Value.first,
                Name   = salesman.Value.second,
                Salary = float.Parse(salesman.Value.third.ToString())
            });
        }
        private void AddSale(string line, FileContentDto viewModel)
        {
            var lineContent = GetStandardLine(line);

            if (lineContent == null)
            {
                return;
            }

            var salesData = lineContent.Value.second.Split(_configuration["SalesSeparator"]);

            if (salesData.Length < 0)
            {
                return;
            }

            var saleInfo = (from saleData in salesData
                            select saleData.Split(_configuration["SaleDataSeparator"])
                            into data where data.Length >= 0
                            select(int.Parse(data[0].Replace(_configuration["SaleDataStartDelimiter"], string.Empty))
                                   , int.Parse(data[1])
                                   , float.Parse(data[2].Replace(_configuration["SaleDataEndDelimiter"], string.Empty))))
                           .ToList();

            var sale = new Sale
            {
                SaleId         = int.Parse(lineContent.Value.first)
                , SalesmanName = lineContent.Value.third.ToString()
                , SalesInfo    = new List <SaleInfo>()
            };

            saleInfo.ForEach(s => sale.SalesInfo.Add(new SaleInfo
            {
                ItemId       = s.Item1,
                ItemQuantity = s.Item2,
                ItemPrice    = s.Item3
            }));


            viewModel.Sales.Add(sale);
        }
Exemplo n.º 9
0
        private async Task <FileContentDto> DownloadFile(SyncableFileRemote node, CancellationToken cancelToken)
        {
            FileContentDto ret = null;

            var list = await _client.Get <List <FileContentDto> >
                           (_urlPattern.f(node.Fid), cancelToken);

            if (list == null)
            {
                return(Error_(ret, "_client.Get<List<FileContentDto>>() == NULL", ""));
            }

            if (list.Count == 0)
            {
                return(Warn_(ret, "Remote file not found in server.", $"[nid: {node.Nid}]  {node.Name}"));
            }

            if (list.Count > 1)
            {
                return(Warn_(ret, "Duplicate files found in server.", string.Join(", ", list.Select(x => x.fid))));
            }

            return(Trace_(list[0], "Successfully downloaded file content from server", $"{node.Name}"));
        }
Exemplo n.º 10
0
        public static OutputDataDto GetIdFromMostExpensiveSale(this OutputDataDto outputDto, FileContentDto contentDto
                                                               , SalesProcessorDbContext context)
        {
            var sales = context.Sales.Where(s => s.InputFileName == contentDto.InputFile.FileName).ToList();

            var results = new List <Tuple <int, float> >();

            sales.ForEach(s =>
            {
                s.SalesInfo = context.SalesInfo.Where(info => info.FkSale == s.Id).ToList();
                var price   = s.SalesInfo.Sum(saleInfo => saleInfo.ItemPrice);
                results.Add(Tuple.Create(s.SaleId, price));
            });

            results = results.OrderByDescending(x => x.Item2).ToList();

            outputDto.MostExpensiveSale = results.First().Item1;

            return(outputDto);
        }
Exemplo n.º 11
0
        public static OutputDataDto GetSalesmenQuantity(this OutputDataDto outputDto, FileContentDto contentDto)
        {
            outputDto.SalesmenQuantity = contentDto.Salesmen.DistinctBy(s => s.Cpf).Count();

            return(outputDto);
        }
Exemplo n.º 12
0
        public static OutputDataDto GetCustomersQuantity(this OutputDataDto outputDto, FileContentDto contentDto)
        {
            outputDto.CustomersQuantity = contentDto.Customers.DistinctBy(c => c.Cnpj).Count();

            return(outputDto);
        }
Exemplo n.º 13
0
 public Task Save(FileContentDto file)
 {
     return(_collection.InsertOneAsync(file));
 }