Esempio n. 1
0
        public void TestReadCsv()
        {
            var catalogReposityMock = new Mock<ICatalogRepository>();
            var eventBrokerMock = new Mock<IEventBroker>();

            var catalog = new SupplierCatalog();
            catalog.Id = 1;

            catalogReposityMock.Setup(x => x.FindById(1))
                .Returns(catalog);

            var catalogService = new CatalogService(eventBrokerMock.Object, catalogReposityMock.Object);

            var filePath = @"Resources\Hardware.csv";

            catalogService.ProcessAction(new ImportHardwareCommand(1, filePath));

            catalogReposityMock.Verify(x => x.Save(catalog));

            Assert.AreEqual(2, catalog.Hardwares.Count);
            Assert.AreEqual(2, catalog.Hardwares[0].Components.Count);
            Assert.AreEqual(4, catalog.Supplies.Count);

            eventBrokerMock.Verify(x => x.Publish(It.IsAny<HardwareCreatedEvent>()), Times.Exactly(2));
            eventBrokerMock.Verify(x => x.Publish(It.IsAny<SupplyCreatedEvent>()), Times.Exactly(4));
        }
Esempio n. 2
0
        public void Delete(SupplierCatalog catalog)
        {
            var session = SessionManager.OpenSessionIfRequired(this.sessionFactory);
            using (var transaction = session.BeginTransaction())
            {
                foreach (Hardware h in catalog.Hardwares)
                {
                    foreach (HardwareSupply s in h.Components)
                    {
                        session.Delete(s);
                    }
                }

                session.Delete(catalog);
                session.Transaction.Commit();
            }
        }
Esempio n. 3
0
        public void Save(SupplierCatalog catalog)
        {
            var session = SessionManager.OpenSessionIfRequired(this.sessionFactory);
            using (var transaction = session.BeginTransaction())
            {
                foreach(var hardware in catalog.Hardwares)
                {
                    var tasks = new List<HardwareTask>(hardware.Tasks);
                    hardware.Tasks.Clear();
                    foreach (var item in tasks)
                    {
                        hardware.Tasks.Add(session.Merge(item));
                    }
                }

                session.SaveOrUpdate(catalog);
                session.Transaction.Commit();
            }
        }
Esempio n. 4
0
        private ProjectHardware MapProjectHardware(Project project, Hardware hardware, ProjectHardware projectHardware, SupplierCatalog catalog)
        {
            Mapper.CreateMap<Supply, ProjectSupply>()
                .ForMember(x => x.SupplyId, y => y.MapFrom(z => z.Id))
                .ForMember(x => x.Price, y => y.MapFrom(z => z.CatalogPrice))
                .ForMember(x => x.Id, y => y.Ignore());
            Mapper.CreateMap<HardwareSupply, ProjectHardwareSupply>()
                .ForMember(x => x.HardwareSupplyId, y => y.MapFrom(z => z.Id))
                .ForMember(x => x.Id, y => y.Ignore());
            Mapper.CreateMap<Hardware, ProjectHardware>()
                .ForMember(x => x.HardwareId, y => y.MapFrom(z => z.Id))
                .ForMember(x => x.Tasks, y => y.Ignore())
                .ForMember(x => x.Id, y => y.Ignore());

            var projectTasksByOriginalId = new Dictionary<int, ProjectTask>();
            foreach (var item in project.Tasks)
            {
                projectTasksByOriginalId.Add(item.TaskId, item);
            }
            var hardwareTasksByOriginalId = new Dictionary<int, HardwareTask>();
            foreach (var item in hardware.Tasks)
            {
                hardwareTasksByOriginalId.Add(item.Task.Id, item);
            }

            Mapper.Map<Hardware, ProjectHardware>(hardware, projectHardware);
            projectHardware.CatalogId = catalog.Id;

            if (projectHardware.Tasks == null)
            {
                projectHardware.Tasks = new List<ProjectHardwareTask>();
            }

            foreach(var item in projectHardware.Tasks)
            {
                HardwareTask hardwareTask;
                if (hardwareTasksByOriginalId.TryGetValue(item.Task.TaskId, out hardwareTask))
                {
                    item.CatalogValue = hardwareTask.Value;
                    item.Value = hardwareTask.Value;
                    hardwareTasksByOriginalId.Remove(item.Task.TaskId);
                }
            }

            foreach (var item in hardwareTasksByOriginalId.Values)
            {
                ProjectTask projectTask;
                if (projectTasksByOriginalId.TryGetValue(item.Task.Id, out projectTask))
                {
                    var projectHardwareTask = new ProjectHardwareTask
                                                  {
                                                      Value = item.Value,
                                                      CatalogValue = item.Value,
                                                      HardwareTaskId = item.Id,
                                                      HardwareTaskType = ProjectHardwareTaskType.DAY,
                                                      Task = projectTask
                                                  };
                    projectHardware.Tasks.Add(projectHardwareTask);
                }
            }

            foreach (var item in projectHardware.Components)
            {
                item.Supply.CatalogId = catalog.Id;
            }
            return projectHardware;
        }
Esempio n. 5
0
        private static ProjectSupply MapProjectSupply(Supply supply, ProjectSupply projectSupply, int quantity,
            SupplierCatalog catalog)
        {
            Mapper.CreateMap<Supply, ProjectSupply>()
                .ForMember(x => x.SupplyId, y => y.MapFrom(z => z.Id))
                .ForMember(x => x.Id, y => y.Ignore());

            Mapper.Map<Supply, ProjectSupply>(supply, projectSupply);
            projectSupply.CatalogId = catalog.Id;
            projectSupply.Quantity = quantity;
            projectSupply.Price = supply.CatalogPrice;
            return projectSupply;
        }