public async Task <IActionResult> Update(DrugFormModel model)
        {
            if (!ModelState.IsValid)
            {
                model.DrugGroups = await this.populator.GetDrugGroups();

                model.Brands = await this.populator.GetBrands();

                model.SideEffects = await this.populator.GetSideEffects();

                return(View(model));
            }

            model.Description = this.htmlService.Sanitize(model.Description);

            var dbModel = Mapper.Map <Drug>(model);

            var successfulEditing = await this.reprDrugsService.UpdateAsync(dbModel);

            if (!successfulEditing)
            {
                ModelState.AddModelError(WebConstants.StatusMessage, WebConstants.DrugNameExists);
                model.DrugGroups = await this.populator.GetDrugGroups();

                model.Brands = await this.populator.GetBrands();

                model.SideEffects = await this.populator.GetSideEffects();

                return(View(model));
            }

            TempData.AddSuccessMessage($"Drug {model.Name} successfully updated.");
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create()
        {
            var model = new DrugFormModel
            {
                DrugGroups  = await this.populator.GetDrugGroups(),
                Brands      = await this.populator.GetBrands(),
                SideEffects = await this.populator.GetSideEffects()
            };

            return(View(model));
        }
        public async Task <IActionResult> Create(DrugFormModel model)
        {
            if (!ModelState.IsValid)
            {
                model.DrugGroups = await this.populator.GetDrugGroups();

                model.Brands = await this.populator.GetBrands();

                model.SideEffects = await this.populator.GetSideEffects();

                return(View(model));
            }

            model.Description = this.htmlService.Sanitize(model.Description);

            var dbModel = Mapper.Map <Drug>(model);

            var userId = this.userManager.GetUserId(User);

            dbModel.RepresentativeId = userId;
            dbModel.DateOfAddition   = DateTime.UtcNow;

            var successfulCreation = await this.reprDrugsService.CreateAsync(dbModel);

            if (!successfulCreation)
            {
                ModelState.AddModelError(WebConstants.StatusMessage, WebConstants.DrugNameExists);
                model.DrugGroups = await this.populator.GetDrugGroups();

                model.Brands = await this.populator.GetBrands();

                model.SideEffects = await this.populator.GetSideEffects();

                return(View(model));
            }

            if (model.SideEffectIds != null)
            {
                await this.reprDrugsService.SideEffectsInDrug(model.SideEffectIds, dbModel.Id);
            }

            TempData.AddSuccessMessage($"Drug {model.Name} successfully created.");
            return(RedirectToAction(nameof(Index)));
        }