Ejemplo n.º 1
0
        public AssignValueViewModel(Molecule molecule, Effect effect, EssentialOil essentialOil)
        {
            // Assign value depending on whether molecule, effect or essential oil was assigned.
            if (molecule != null)
            {
                AssignedValueId   = molecule.Id;
                SubstanceIdString = molecule.SubstanceIdString;
                SubstanceValue    = molecule.Name;

                // Show e.g. "Aromatische Aldehyde allgemein" if is general molecule.
                if (molecule.IsGeneral)
                {
                    AssignedValueName = $"{SubstanceValueDisplayName} {Resources.Resources.Molecule_General}";
                }
                else
                {
                    AssignedValueName = molecule.Name;
                }
            }
            else if (effect != null)
            {
                AssignedValueId   = effect.Id;
                AssignedValueName = effect.Name;
                CategoryIdString  = effect.CategoryIdString;
            }
            else if (essentialOil != null)
            {
                AssignedValueId               = essentialOil.Id;
                AssignedValueName             = essentialOil.Name;
                NameLatin                     = essentialOil.NameLatin;
                FirstLetterOfEssentialOilName = essentialOil.Name.Substring(0, 1).ToUpper();
            }
        }
Ejemplo n.º 2
0
 public AssignMoleculeViewModel(EssentialOil essentialOil, Effect effect,
                                IList <AssignValueViewModel> moleculeViewModels, IList <Substance> substances)
 {
     _essentialOil   = essentialOil;
     _effect         = effect;
     AssignMolecules = moleculeViewModels;
     Substances      = substances;
 }
Ejemplo n.º 3
0
        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 }));
        }
Ejemplo n.º 4
0
        public EssentialOilViewModel(EssentialOil essentialOil)
        {
            _essentialOil = essentialOil;

            if (_essentialOil.Type != null)
            {
                // Parses enum to string in order to display the appropriate name of the essential oil type.
                EssentialOilType e;
                _essentialOilType = Enum.TryParse(_essentialOil.Type, true, out e) ? (EssentialOilType?)e : null;
            }
        }
 public ActionResult EditEssentialOil(EssentialOil model)
 {
     if (ModelState.IsValid)
     {
         unitOfWork.EssentialOils.Edit(model);
         unitOfWork.Complete();
         unitOfWork.Dispose();
         return(RedirectToAction("Index", "EssentialOil"));
     }
     else
     {
         return(View(model));
     }
 }
 public AssignEssentialOilEffectViewModel(EssentialOil essentialOil, Effect effect,
                                          IList <AssignValueViewModel> assignValueViewModel, IList <Category> categories)
 {
     if (essentialOil != null)
     {
         _essentialOil = essentialOil;
         AssignEffects = assignValueViewModel;
         Categories    = categories;
     }
     else if (effect != null)
     {
         _effect             = effect;
         AssignEssentialOils = assignValueViewModel;
     }
 }
 public ActionResult AddEssentialOil(EssentialOil model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             unitOfWork.EssentialOils.Add(model);
             unitOfWork.Complete();
             unitOfWork.Dispose();
             return(RedirectToAction("Index", "EssentialOil"));
         }
     }
     catch (Exception ex)
     {
         //TODO: We want to show an error message
         return(View());
     }
     return(View());
 }
Ejemplo n.º 8
0
        /// <summary>
        ///     Initializes Essential Oil collection in database with default values.
        /// </summary>
        /// <author>Anna Krebs</author>
        /// <param name="name"></param>
        /// <param name="nameLatin"></param>
        /// <param name="essentialOilType"></param>
        /// <returns></returns>
        private async Task InitEssentialOilCollection(string name, string nameLatin, EssentialOilType essentialOilType)
        {
            var essentialOilTypeString = essentialOilType.ToString();

            var essentialOilCollection = _mongoContext.Database.GetCollection <EssentialOil>(nameof(EssentialOil));

            var isDuplicate =
                await MongoHelper <EssentialOil> .IsDuplicate(Constants.Name, name, essentialOilCollection, null);

            if (!isDuplicate)
            {
                var essentialOil = new EssentialOil {
                    Name = name, NameLatin = nameLatin, Type = essentialOilTypeString
                };
                await essentialOilCollection.InsertOneAsync(essentialOil);

                Log.Info($"Inserted essentialOil {name} with Id {essentialOil.ObjectId} into database.");
            }
        }
Ejemplo n.º 9
0
 public EssentialOilViewModel()
 {
     _essentialOil = new EssentialOil();
 }
 public AssignEssentialOilEffectViewModel()
 {
     _essentialOil = new EssentialOil();
     _effect       = new Effect();
 }
Ejemplo n.º 11
0
 public AssignMoleculeViewModel()
 {
     _essentialOil = new EssentialOil();
     _effect       = new Effect();
 }