Example #1
0
        public async Task <ActionResult> Index(string lastEditedMoleculeId)
        {
            var filter = new MoleculeFilter();

            // Get values from database.
            var molecules = await _moleculeService.GetAllAsync(filter);

            var substances = await _substanceService.GetAllAsync(new SubstanceFilter());

            IList <MoleculeViewModel> moleculeViewModels = new List <MoleculeViewModel>();

            // Create list of molecules for view.
            foreach (var molecule in molecules)
            {
                var model = new MoleculeViewModel(molecule, null);

                if (molecule.SubstanceIdString != null)
                {
                    var substance = await _moleculeService.GetSubstanceForMoleculeAsync(molecule.SubstanceIdString);

                    // Set name of substance for molecule.
                    model.SubstanceValue = substance.Name;
                }

                moleculeViewModels.Add(model);
            }

            // Create view model for Index view.
            var moleculeViewModelIndex = new MoleculeViewModelIndex(moleculeViewModels, substances);

            // Set value to where to scroll to.
            moleculeViewModelIndex.LastEditedMoleculeId = lastEditedMoleculeId;

            return(View(moleculeViewModelIndex));
        }
Example #2
0
        /// <summary>
        ///     Gets the AssignMoleculeViewModel.
        /// </summary>
        /// <author>Anna Krebs</author>
        /// <param name="id"></param>
        /// <returns></returns>
        private async Task <AssignMoleculeViewModel> GetAssignMoleculeViewModel(string id)
        {
            var essentialOil = await _essentialOilService.GetByIdAsync(id);

            if (essentialOil == null)
            {
                Log.Error($"An unexpected error occurred while getting id. No entity with id {id} could be found.");
                throw new ArgumentNullException(string.Format(Resources.Resources.Error_NoEntityWithIdFound, id));
            }

            // Get values from database.
            var molecules = await _moleculeService.GetAllAsync(new MoleculeFilter());

            var substances = await _substanceService.GetAllAsync(new SubstanceFilter());

            var essentialOilMolecules =
                await _essentialOilMoleculeService.GetAllAsync(new EssentialOilMoleculeFilter { EssentialOilId = id });

            IList <AssignValueViewModel> assignValueViewModels = new List <AssignValueViewModel>();

            // Create list of molecules for view.
            foreach (var molecule in molecules)
            {
                var assignValueViewModel = new AssignValueViewModel(molecule, null, null);

                foreach (var essentialOilMolecule in essentialOilMolecules)
                {
                    // Map values from database to model.
                    if (assignValueViewModel.AssignedValueId == essentialOilMolecule.MoleculeId)
                    {
                        assignValueViewModel.MoleculePercentage = essentialOilMolecule.MoleculePercentage;
                    }
                }

                assignValueViewModels.Add(assignValueViewModel);
            }

            var model = new AssignMoleculeViewModel(essentialOil, null, assignValueViewModels, substances);

            Log.Info($"Get AssignMoleculeViewModel for essential oil with id {id}");
            return(model);
        }