Ejemplo n.º 1
0
        private void WriteToDb(SalesPerDay salesPerDay)
        {
            using (IUnitOfWork unitOfWork = new EFUnitOfWork(Properties.Settings.Default.connectionString))
            {
                AutoMapper.Mapper.Reset();
                AutoMapper.Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap <SalesPerDay, User>()
                    .ForMember(x => x.FirstName, opt => opt.MapFrom(src => src.ManagerFirstName))
                    .ForMember(x => x.SecondName, opt => opt.MapFrom(src => src.ManagerSecondName));
                    cfg.CreateMap <SalesPerDay, Product>()
                    .ForMember(x => x.Name, opt => opt.MapFrom(src => src.Product));
                });
                User        manager = AutoMapper.Mapper.Map <SalesPerDay, User>(salesPerDay);
                List <Task> tasks   = new List <Task>();
                tasks.Add(Task.Run(() => CheckUser(manager, unitOfWork)));

                Product product = AutoMapper.Mapper.Map <SalesPerDay, Product>(salesPerDay);
                tasks.Add(Task.Run(() => CheckProduct(product, unitOfWork)));
                AutoMapper.Mapper.Reset();
                AutoMapper.Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap <SalesPerDay, User>()
                    .ForMember(x => x.FirstName, opt => opt.MapFrom(src => src.ClientFirstName))
                    .ForMember(x => x.SecondName, opt => opt.MapFrom(src => src.ClientSecondName));

                    cfg.CreateMap <SalesPerDay, Sale>()
                    .ForMember(x => x.DateSale, opt => opt.MapFrom(src => Convert.ToDateTime(src.DateSale)))
                    .ForMember(x => x.Description, opt => opt.MapFrom(src => src.Description))
                    .ForMember(x => x.Price, opt => opt.MapFrom(src => src.Price))
                    .ForMember(x => x.Client, opt => opt.Ignore())
                    .ForMember(x => x.Manager, opt => opt.Ignore())
                    .ForMember(x => x.Product, opt => opt.Ignore());
                });
                User client = AutoMapper.Mapper.Map <SalesPerDay, User>(salesPerDay);
                Sale sale   = AutoMapper.Mapper.Map <SalesPerDay, Sale>(salesPerDay);

                Task.WaitAll(tasks.ToArray());

                tasks.Add(Task.Run(() => CheckUser(client, unitOfWork)));
                Task.WaitAll(tasks.ToArray());
                unitOfWork.Save();

                sale.Manager = unitOfWork.Users.GetAll().FirstOrDefault(x => x.FirstName.ToLower() == manager.FirstName.ToLower() && x.SecondName.ToLower() == manager.SecondName.ToLower());
                sale.Client  = unitOfWork.Users.GetAll().FirstOrDefault(x => x.FirstName.ToLower() == client.FirstName.ToLower() && x.SecondName.ToLower() == client.SecondName.ToLower());;
                sale.Product = unitOfWork.Products.GetAll().FirstOrDefault(x => x.Name.ToLower() == product.Name.ToLower());
                tasks.Add(Task.Run(() => CheckSale(sale, unitOfWork)));
                Task.WaitAll(tasks.ToArray());
                unitOfWork.Save();
            }
        }
Ejemplo n.º 2
0
        private void ParseFile(string filePath)
        {
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

            if (fileNameWithoutExtension == null)
            {
                return;
            }
            string[]    splittedFileName = fileNameWithoutExtension.Split('_');
            SalesPerDay salesPerDay;

            if (splittedFileName.Count() > 1)
            {
                salesPerDay = new SalesPerDay(splittedFileName[0].Trim(), splittedFileName[1].Trim());
            }
            else
            {
                return;
            }
            try
            {
                using (var reader = new StreamReader(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        salesPerDay.ParseLine(line);
                        WriteToDb(salesPerDay);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }