public ConfigurationViewModel Add(ConfigurationWriteModel model)
        {
            var configuration = BuildFromWriteModel(model);
            var entity        = repository.Add(configuration);

            return(ConfigurationViewModel.From(configuration));
        }
        private Configuration BuildFromWriteModel(ConfigurationWriteModel model)
        {
            List <ConfigurationComponent> configComponents = ExtractValidComponents(model);

            var configuration = new Configuration(model.Id, model.Name, configComponents);

            return(configuration);
        }
        public ConfigurationViewModel Update(ConfigurationWriteModel model)
        {
            var configuration = repository.GetById(model.Id);

            configuration.ConfigurationComponents = ExtractValidComponents(model);
            configuration.Name = model.Name;
            var updated = repository.Update(configuration);

            return(ConfigurationViewModel.From(updated));
        }
        private List <ConfigurationComponent> ExtractValidComponents(ConfigurationWriteModel model)
        {
            var components       = compRepo.GetAllById(model.Components.Select(cmp => cmp.Id).ToArray());
            var configComponents = new List <ConfigurationComponent>();

            foreach (var item in components)
            {
                var quantity = model.Components.First(cmp => cmp.Id == item.Id).Quantity;
                var cfgCmp   = new ConfigurationComponent(item, quantity);
                configComponents.Add(cfgCmp);
            }

            return(configComponents);
        }