コード例 #1
0
        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)));
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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)));
        }