public async Task <IActionResult> AddObservedUser([FromBody] ObservedUsersRelationshipDto pair) { if (!await IsSelfService(pair.ObservingUserId)) { return(Unauthorized()); } var result = await _userService.AddObservedUser(pair); if (result.IsSuccessful) { return(Ok()); } if (result.Payload != null) { return(Conflict()); } if (result.IsClientError) { return(NotFound(result.Message)); } return(StatusCode(500, result.Message)); }
public async Task <OperationResult> AddObservedUser(ObservedUsersRelationshipDto pair) { var observingUser = await GetUserById(pair.ObservingUserId); if (observingUser == null) { return(new OperationResult() { IsSuccessful = false, IsClientError = true, Message = "Observing user not found!" }); } var observedUser = await GetUserById(pair.ObservedUserId); if (observedUser == null) { return(new OperationResult() { IsSuccessful = false, IsClientError = true, Message = "Observed user not found!" }); } var currentList = await GetObservedUsersForUser(pair.ObservingUserId); var ids = currentList.Select(u => u.Id); if (ids.Contains(pair.ObservedUserId)) { return(new OperationResult() { IsSuccessful = false, IsClientError = true, Payload = pair }); } await userRepository.AddObsersvedUser(pair.ObservingUserId, pair.ObservedUserId); var result = await userRepository.SaveChangesAsync(); if (result > 0) { return(new OperationResult() { IsSuccessful = true, Payload = pair }); } else { return(OtherServices.GetIncorrectDatabaseConnectionResult()); } }