Beispiel #1
0
        public async Task UpdateAsync(EditLicenseViewModel model)
        {
            var entity = this.licensesRepository.All().Where(x => x.Id == model.Id).FirstOrDefault();

            if (entity == null)
            {
                throw new Exception("License does not exist!");
            }

            entity.Name        = model.Name;
            entity.Description = model.Description;
            await this.licensesRepository.SaveChangesAsync();
        }
        public void Update_ShouldThrowExceptionWhenIdNotFound()
        {
            var model = new EditLicenseViewModel()
            {
                Id = "id", Name = "name", Description = "desc"
            };

            var repo      = DeletableEntityRepositoryMock.Get <License>(new List <License>());
            var service   = new LicenseService(repo.Object);
            var exception = Assert.Throws <AggregateException>(() => service.UpdateAsync(model).Wait());

            Assert.Contains("License does not exist!", exception.Message);
        }
        public IActionResult Edit(EditLicenseViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            try
            {
                this.licenseService.UpdateAsync(model);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(model));
            }

            return(this.RedirectToAction("Index", "Licenses"));
        }
        public void Update_ShouldRunSuccessfully()
        {
            var license = LicenseCreator.Create();
            var list    = new List <License>()
            {
                license
            };
            var model = new EditLicenseViewModel()
            {
                Id = license.Id, Name = "Updated", Description = "Updated"
            };

            var repo    = DeletableEntityRepositoryMock.Get <License>(list);
            var service = new LicenseService(repo.Object);

            service.UpdateAsync(model).Wait();

            Assert.Single(list);
            Assert.Equal("Updated", list.First().Name);
            Assert.Equal("Updated", list.First().Description);
        }