Example #1
0
        public async Task UpdateAsyncShouldWorkCorrect()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);
            var lakeRepository = new EfDeletableEntityRepository <Lake>(db);

            var service = new LakesService(null, lakeRepository);

            await lakeRepository.AddAsync(new Lake
            {
                Id         = 1,
                Name       = "Test",
                WebsiteUrl = "www.test.com",
                Area       = 11.11,
                IsFree     = false,
            });

            await lakeRepository.SaveChangesAsync();

            var input = new EditLakeInputModel
            {
                CountryId  = 2,
                Name       = "Test2",
                WebsiteUrl = "www.test2.com",
                Area       = 22.22,
                IsFree     = true,
            };

            await service.UpdateAsync(1, input);

            await lakeRepository.SaveChangesAsync();

            AutoMapperConfig.RegisterMappings(typeof(LakeModel).Assembly);
            var currLake = service.GetById <LakeModel>(1);

            Assert.Equal(2, currLake.CountryId);
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, EditLakeInputModel input)
        {
            var currentUserId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var lakeOwnerId   = this.lakesService.GetLakeOwnerId(id);

            var isAdministrator = this.User.IsInRole(GlobalConstants.AdministratorRoleName);

            if (currentUserId != lakeOwnerId && !isAdministrator)
            {
                return(this.NotFound());
            }

            if (!this.ModelState.IsValid)
            {
                input.CountriesItems = this.countriesService.GetAllAsKeyValuePairs();

                return(this.View(input));
            }

            await this.lakesService.UpdateAsync(id, input);

            return(this.RedirectToAction(nameof(this.ById), new { id }));
        }