public Dictionary <string, string> TryUpdate(Guid characterId, CharacterUpdateForm characterForm) { var updatedCharacter = _characters.SingleOrDefault(x => x.Id == characterId); var errors = new Dictionary <string, string>(); if (updatedCharacter is null) { errors.Add(nameof(Character), $"Character with {characterId} does not exist."); } if (_characters.Any(x => x.Id != characterId && x.Name == characterForm.Name)) { errors.Add(nameof(characterForm.Name), $"Character with {characterForm.Name} does exist, can't have doppelgangers."); } if (characterForm.Friends.Any(x => _characters.All(y => y.Id != x))) { errors.Add(nameof(characterForm.Friends), $"Some of provided friends do not exist."); } if (errors.Any()) { return(errors); } updatedCharacter.Name = characterForm.Name; updatedCharacter.Episodes = new Episodes(Episode.List.Where(x => characterForm.Episodes.Contains(x.Value)).ToArray()); updatedCharacter.Friends = new Friends(_characters.Where(x => characterForm.Friends.Contains(x.Id)).ToArray()); return(new Dictionary <string, string>()); }
public async Task <IActionResult> Patch([FromRoute] Guid characterId, [FromBody] CharacterUpdateForm characterForm) { var updateResult = await _mediator.Send(new CharacterUpdateCommand(characterId, characterForm)); if (updateResult.IsSuccessful) { return(Ok(updateResult)); } if (updateResult.Errors.ContainsKey("Not Found")) { return(NotFound(updateResult)); } return(BadRequest(updateResult)); }
public CharacterUpdateCommand(Guid characterId, CharacterUpdateForm characterUpdateForm) { CharacterId = characterId; CharacterUpdateForm = characterUpdateForm; }
public Dictionary <string, string> TryUpdate(Guid characterId, CharacterUpdateForm characterForm) { var chr = this .Characters .Include(x => x.Episodes) .Include(x => x.Friends) .FirstOrDefault(x => x.Id == characterId); if (chr is null) { return(new Dictionary <string, string> { { nameof(Character), "Character does not exist" } }); } try { chr.Name = characterForm.Name; var currentFriends = this.CharacterFriends.Where(x => x.CharacterId == chr.Id).ToList(); var toDelete = currentFriends .Where(x => !characterForm.Friends.Contains(x.FriendId)) .ToList(); var toAdd = characterForm .Friends .Where(x => !currentFriends.Select(y => y.FriendId).Contains(x)) .Select(x => new CharacterFriend { CharacterId = chr.Id, FriendId = x, }) .ToList(); this.CharacterFriends.RemoveRange(toDelete); this.CharacterFriends.AddRange(toAdd); var currentEpisodes = this.CharacterEpisodes.Where(x => x.CharacterId == chr.Id).ToList(); var episodesToDelete = currentEpisodes .Where(x => !characterForm.Episodes.Contains(x.Episode.Value)) .ToList(); var episodesToAdd = characterForm .Episodes .Where(x => !currentEpisodes.Select(y => y.Episode.Value).Contains(x)) .Select(x => new CharacterEpisode { CharacterId = chr.Id, Episode = Episode.FromValue(x), }) .ToList(); this.CharacterEpisodes.RemoveRange(episodesToDelete); this.CharacterEpisodes.AddRange(episodesToAdd); SaveChanges(); return(new Dictionary <string, string>()); } catch (Exception ex) { return(new Dictionary <string, string> { { "database error", ex.Message } }); } }