public async Task <ActionResult> CreateOrEdit(string id)
        {
            EssentialOilViewModel model;

            // Get EssentialOilViewModel according to whether is edit or create.
            if (!string.IsNullOrEmpty(id))
            {
                // Edit
                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));
                }

                model = new EssentialOilViewModel(essentialOil);
            }
            else
            {
                // Create
                model = new EssentialOilViewModel();
            }

            return(View(model));
        }
Beispiel #2
0
        /// <summary>
        ///     Creates the essential oil view model that is shown as the result of the effect search.
        /// </summary>
        /// <author>Anna Krebs</author>
        /// <param name="searchEssentialOilItemsResults"></param>
        /// <param name="maxEffectDegreeDiscomfortValue"></param>
        /// <returns></returns>
        private IList <EssentialOilViewModel> CreateEssentialOilViewModels(
            IList <SearchEssentialOilItem> searchEssentialOilItemsResults, int maxEffectDegreeDiscomfortValue)
        {
            // Order the results by descending MatchAmount, then by descending EffectDegreeDiscomfortValue, then essential oil name.
            searchEssentialOilItemsResults = searchEssentialOilItemsResults
                                             .OrderByDescending(m => m.MatchAmount)
                                             .ThenByDescending(m => m.EffectDegreeDiscomfortValue)
                                             .ThenBy(m => m.EssentialOil.Name)
                                             .ToList();

            // Create the view model with the result list.
            IList <EssentialOilViewModel> essentialOilViewModels = new List <EssentialOilViewModel>();

            foreach (var resultItem in searchEssentialOilItemsResults)
            {
                // Get the specific result item and add to view model.
                var essentialOilViewModel = new EssentialOilViewModel(resultItem.EssentialOil);
                essentialOilViewModel.EffectDegreeDiscomfortValue = resultItem.EffectDegreeDiscomfortValue;
                essentialOilViewModel.MatchAmount        = resultItem.MatchAmount;
                essentialOilViewModel.WeightedMatchValue =
                    _essentialOilService.CalculateWeightedMatchValue(resultItem, maxEffectDegreeDiscomfortValue);
                essentialOilViewModel.SearchEffectTextsInEssentialOil = resultItem.SearchEffectTextsInEssentialOil;

                essentialOilViewModels.Add(essentialOilViewModel);
            }

            return(essentialOilViewModels);
        }
Beispiel #3
0
        public async Task <ActionResult> EssentialOilDetails(List <SearchEffectItem> searchEffects, string essentialOilId)
        {
            if (string.IsNullOrEmpty(essentialOilId))
            {
                Log.Error("An unexpected error occurred while getting id. No id was set.");
                throw new ArgumentNullException(
                          $"{Resources.Resources.Error_UnexpectedError} {Resources.Resources.Error_TryAgainLater}");
            }

            // Get EssentialOilViewModel.
            var essentialOil = await _essentialOilService.GetByIdAsync(essentialOilId);

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

            // Get the assigned values for the essential oil.
            essentialOil.Effects = await _essentialOilService.GetAssignedEffectsForEssentialOilAsync(essentialOil.Id);

            essentialOil.Molecules =
                await _essentialOilService.GetAssignedMoleculesForEssentialOilAsync(essentialOil.Id);

            var model = new EssentialOilViewModel(essentialOil);

            // Add searched effects to model, in order to restore them later, if "Zurück" is clicked.
            model.SearchEffects = searchEffects;

            Log.Info("Show EssentialOilDetails");
            // Return PartialView as html.
            return(PartialView("~/Views/SearchEssentialOil/_EssentialOilDetails.cshtml", model));
        }
        public async Task <ActionResult> EssentialOilDetails(string essentialOilId, string searchEssentialOilText)
        {
            if (string.IsNullOrEmpty(essentialOilId))
            {
                _log.LogError("An unexpected error occurred while getting id. No id was set.");
                throw new ArgumentNullException(
                          $"{Resources.Resources.Error_UnexpectedError} {Resources.Resources.Error_TryAgainLater}");
            }

            // Get EssentialOilViewModel.
            var essentialOil = await _essentialOilService.GetByIdAsync(essentialOilId);

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

            // Get the assigned values for the essential oil.
            essentialOil.Effects = await _essentialOilService.GetAssignedEffectsForEssentialOilAsync(essentialOil.Id);

            essentialOil.Molecules =
                await _essentialOilService.GetAssignedMoleculesForEssentialOilAsync(essentialOil.Id);

            var model = new EssentialOilViewModel(essentialOil);

            model.SearchEssentialOilText = searchEssentialOilText;


            _log.LogInformation("Show EssentialOilDetails");
            // Return PartialView as html.
            return(PartialView("_EssentialOilDetails", model));
        }
        public async Task <ActionResult> CreateOrEdit(EssentialOilViewModel model, IFormFile uploadFile)
        {
            ValidationResultList validationResult = new ValidationResultList();
            EssentialOil         essentialOil     = new EssentialOil();

            if (ModelState.IsValid)
            {
                try
                {
                    if (uploadFile?.Length > 0)
                    {
                        // Get file name & base 64 string for picture.
                        essentialOil.PictureFileName     = Path.GetFileName(uploadFile.FileName);
                        essentialOil.PictureDataAsString = await _conversionHelper.ResizeAndGenerateBase64StringForPicture(uploadFile);
                    }

                    // Map view model to entity.
                    model.MapViewModelToEntity(essentialOil);

                    // Edit or create
                    if (essentialOil.Id != null)
                    {
                        // Edit
                        // Only update if essential oil name doesn't already exist.
                        validationResult = await _essentialOilService.UpdateAsync(essentialOil);
                    }
                    else
                    {
                        // Create
                        // Only insert if essential oil name doesn't already exist.
                        validationResult = await _essentialOilService.InsertAsync(essentialOil);
                    }
                }
                catch (Exception e)
                {
                    Log.Error($"CreateOrEdit. An unexpected error occurred while inserting or editing: {e}");
                    throw new ArgumentException(Resources.Resources.Error_UnexpectedError);
                }
            }

            // Show validation result, if validation error occurred while
            // inserting or if ModelState is invalid.
            if (validationResult.HasErrors || !ModelState.IsValid)
            {
                AddValidationResultsToModelStateErrors(validationResult.Errors);

                Log.Info("Show CreateOrEdit");
                return(View(nameof(CreateOrEdit), model));
            }

            // If form is valid, navigate to AssignMolecule.
            Log.Info("Redirect to AssignMolecule");


            return(RedirectToAction(nameof(AssignMolecule), new { id = model.Id }));
        }