public async Task <GetHeroDTO> UpdateHero(UpdateHeroDTO hero) { var updated = _heroRepository.Update(_mapper.Map <Hero>(hero)); await _heroRepository.SaveChangesAsync(); return(_mapper.Map <GetHeroDTO>(updated)); }
public async Task UpdateHero(int id, UpdateHeroDTO hero) { var dbHero = this._heroRepository .All() .FirstOrDefault(x => x.Id == id); if (dbHero?.Name == hero.Name) { throw new ArgumentException("Please enter a name that is different from the previous one."); } if (string.IsNullOrEmpty(hero.Name) || string.IsNullOrWhiteSpace(hero.Name)) { throw new InvalidOperationException("Name cannot be null or empty string."); } var editHistory = new EditHistory() { OldValue = dbHero.Name, NewValue = hero.Name, HeroId = dbHero.Id }; dbHero.EditHistory.Add(editHistory); dbHero.Name = hero.Name; await this._heroRepository.SaveChangesAsync(); }
public async Task <ActionResult <GetHeroDTO> > Update(Guid id, [FromBody] UpdateHeroDTO dto) { var updatedHero = await _heroAppService.UpdateHero(id, dto); if (updatedHero == null) { return(NotFound()); } return(NoContent()); }
public async Task UpdateHero_WithIncorrectData_ShouldThrowInvalidOperationException(string name) { var repo = new Mock <IRepository <Hero> >(); repo.Setup(r => r.All()).Returns(this.GetTestData().AsQueryable); this._heroService = new HeroService(repo.Object, null, null, null); var updateHeroDto = new UpdateHeroDTO() { Name = name }; await Assert.ThrowsAsync <InvalidOperationException>(() => this._heroService.UpdateHero(1, updateHeroDto)); }
public async Task UpdateHero_WithSameNameLikePrevious_ShouldThrowArgumentException() { var repo = new Mock <IRepository <Hero> >(); repo.Setup(r => r.All()).Returns(this.GetTestData().AsQueryable); this._heroService = new HeroService(repo.Object, null, null, null); var updateHeroDto = new UpdateHeroDTO() { Name = "Cpt America" }; await Assert.ThrowsAsync <ArgumentException>(() => this._heroService.UpdateHero(1, updateHeroDto)); }
public async Task <GetHeroDTO> UpdateHero(Guid id, UpdateHeroDTO updatedHero) { var originalHero = await _heroRepository.GetById(id); if (originalHero == null) { return(null); } originalHero.Name = updatedHero?.Name; originalHero.Nickname = updatedHero?.Nickname; originalHero.Team = updatedHero?.Team; originalHero.Individuality = updatedHero?.Individuality; originalHero.Age = updatedHero?.Age; originalHero.HeroType = updatedHero.HeroType; _heroRepository.Update(originalHero); await _heroRepository.SaveChangesAsync(); return(_mapper.Map <GetHeroDTO>(originalHero)); }
public void UpdateHero_WithCorrectData_ShouldSuccessfullyUpdateHero() { string errorMessagePrefix = "HeroService UpdateHero() method does not work properly."; var repo = new Mock <IRepository <Hero> >(); repo.Setup(r => r.All()).Returns(this.GetTestData().AsQueryable); this._heroService = new HeroService(repo.Object, null, null, null); var updateHeroDto = new UpdateHeroDTO() { Name = "asd" }; var expectedName = "asd"; var isPassedSuccessfully = this._heroService.UpdateHero(1, updateHeroDto).IsCompletedSuccessfully; var actualName = repo.Object.All().FirstOrDefault(x => x.Id == 1)?.Name; Assert.True(isPassedSuccessfully, errorMessagePrefix); Assert.True(expectedName == actualName, errorMessagePrefix); }
public async Task <IActionResult> UpdateHero(int id, UpdateHeroDTO hero) { var userId = HttpContext.User.Claims.First(x => x.Type == "UserID").Value; var dbHero = this._heroService.GetById(userId, id); _logger.LogInfo($"Update hero with {id}..."); if (dbHero == null) { return(NotFound(new { message = "Hero that you are trying to update is not found." })); } if (dbHero.Name == hero.Name) { return(BadRequest(new { message = "Name that you are trying to enter is equal to previous !" })); } await this._heroService.UpdateHero(id, hero); _logger.LogInfo($"Hero with {id} successfully updated."); return(this.NoContent()); }
public async Task <ActionResult <GetHeroDTO> > Update([FromBody] UpdateHeroDTO dto) { return(Ok(await _heroAppService.UpdateHero(dto))); }