Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(AddTutorialViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = this.ModelState.Values.SelectMany(p => p.Errors).Select(e => e.ErrorMessage).ToList();

                var errorModel = this.errorService.GetErrorModel(errors);

                return(View("Error", errorModel));
            }

            try

            {
                await this.tutorialService.UpdateTutorial(model);
            }
            catch (Exception e)
            {
                ViewData["Errors"] = e.Message;

                return(this.View("Error"));
            }

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public async Task AddTutorial(AddTutorialViewModel model)
        {
            if (this.repository.All().Any(t => t.Title == model.Title))
            {
                throw new InvalidOperationException("Tutorial with such title already exists.");
            }

            if (this.repository.All().Any(t => t.Title != model.Title && t.Url == model.Url))
            {
                throw new InvalidOperationException("Tutorial with such url already exists but with other title already exists.");
            }

            var tutorial = this.mapper.Map <AddTutorialViewModel, Tutorial>(model);

            this.repository.Add(tutorial);

            try
            {
                await this.repository.SaveChangesAsync();
            }
            catch (Exception e)
            {
                this.logger.LogDebug(e.Message);

                throw new InvalidOperationException("Sorry, an error occurred and your request couldn't be processed.");
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> AddTutorial(AddTutorialViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = this.ModelState.Values.SelectMany(p => p.Errors).Select(e => e.ErrorMessage).ToList();

                var errorModel = this.errorService.GetErrorModel(errors);

                return(View("Error", errorModel));
            }

            await this.tutorialService.AddTutorial(model);

            return(RedirectToAction("Index"));
        }
        public async Task AddTutorial_WithValidModel_ShouldBeSuccessful()
        {
            //Arrange
            var db = this.SetDb();

            var repo = new Repository <Tutorial>(db);

            var service = new TutorialService(repo, this.Mapper, null);

            var model = new AddTutorialViewModel {
                Title = "Lotus Flower", Description = "Lotus Test Descripion", Url = "https://someurl.bg"
            };

            //Act
            await service.AddTutorial(model);

            var expectedTutorialsCount = 1;
            var actualTutorialsCount   = repo.All().Count();

            //Assert
            Assert.Equal(expectedTutorialsCount, actualTutorialsCount);
        }
        public async Task AddTutorial_WithDuplicateTitle_ShouldTrow()
        {
            //Arrange
            var db = this.SetDb();

            var repo = new Repository <Tutorial>(db);

            var service = new TutorialService(repo, this.Mapper, null);

            var model1 = new AddTutorialViewModel {
                Title = "Lotus Flower", Description = "Lotus Test Descripion", Url = "https://someurl.bg"
            };
            var model2 = new AddTutorialViewModel {
                Title = "Lotus Flower", Description = "Lotus Test Descripion", Url = "https://otherurl.bg"
            };

            await service.AddTutorial(model1);

            //Act

            //Assert
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.AddTutorial(model2));
        }