public async Task UpdateAsync(int id, EditTrophyInputModel input) { var trophy = this.trophiesRepository.All() .FirstOrDefault(r => r.Id == id); trophy.LakeId = input.LakeId; trophy.RigId = input.RigId; trophy.BaitDescription = input.BaitDescription; trophy.Weight = input.Weight; await this.trophiesRepository.SaveChangesAsync(); }
public async Task UpdateAsyncShouldWorkCorrect() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; using var db = new ApplicationDbContext(options); var trophyRepository = new EfDeletableEntityRepository <Trophy>(db); var service = new TrophiesService(null, trophyRepository); await trophyRepository.AddAsync(new Trophy { Id = 1, Weight = 50, BaitDescription = "corn", LakeId = 1, OwnerId = "asdasd123", }); await trophyRepository.SaveChangesAsync(); var input = new EditTrophyInputModel { Weight = 49, BaitDescription = "grass", }; await service.UpdateAsync(1, input); await trophyRepository.SaveChangesAsync(); AutoMapperConfig.RegisterMappings(typeof(TrophyModel).Assembly); var trophy = service.GetById <TrophyModel>(1); Assert.Equal(49, trophy.Weight); Assert.Equal("grass", trophy.BaitDescription); }
public async Task <IActionResult> Edit(int id, EditTrophyInputModel input) { var currentUserId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; var trophyOwnerId = this.trophiesService.GetTrophyOwnerId(id); var isAdministrator = this.User.IsInRole(GlobalConstants.AdministratorRoleName); if (currentUserId != trophyOwnerId && !isAdministrator) { return(this.NotFound()); } if (!this.ModelState.IsValid) { input.LakesItems = this.lakesService.GetAllAsKeyValuePairs(); input.RigsItems = this.rigsService.GetAllAsKeyValuePairs(); return(this.View(input)); } await this.trophiesService.UpdateAsync(id, input); return(this.RedirectToAction(nameof(this.ById), new { id })); }