public async Task UpdateFriendAsync(FriendUpdateDTO model) { var idClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; if (!long.TryParse(idClaim, out var ownerId)) { throw new UnauthorizedAccessException(); } var oldFriend = await _friendRepository.GetByIdAsync(model.Id, friend => friend.Owner); if (oldFriend == null) { throw new SqlNullValueException("Friend does not exist"); } if (oldFriend.Owner.Id == ownerId) { oldFriend.Name = model.Name; } else { throw new NoPermissionException("You are not member of this friend connection"); } oldFriend.ModifiedBy = ownerId; await _friendRepository.UpdateAsync(oldFriend); }
public async Task <IActionResult> UpdateFriend(FriendUpdateDTO model) { try { await _friendService.UpdateFriendAsync(model); return(Ok()); } catch (DbUpdateConcurrencyException) { return(BadRequest(new { Message = "The record you attempted to edit was modified by another user after you got the original value" })); } catch (UnauthorizedAccessException) { return(Unauthorized()); } catch (NoPermissionException e) { return(BadRequest(new { e.Message })); } catch (SqlNullValueException e) { return(NotFound(new { e.Message })); } catch (Exception e) { return(BadRequest(new { e.Message })); } }