public async Task <ActionResult> UpdateConnection(string otherId, [FromBody] JsonPatchDocument <ConnectionUpdateModel> patchDoc) { var userId = _identityService.GetUserIdentity(); var connectionResult = await _connectionsService.GetAsync(userId, otherId); if (!connectionResult.Succeeded) { return(BadRequest(connectionResult.Errors)); } var existingConnection = connectionResult.Data; var updateModel = ConnectionUpdateModel.From(existingConnection); patchDoc.ApplyTo(updateModel); TryValidateModel(updateModel); if (!ModelState.IsValid) { var validationErrors = ModelState .Keys .SelectMany(k => ModelState[k].Errors) .Select(e => e.ErrorMessage) .ToArray(); return(BadRequest(validationErrors)); } var updateResult = await _connectionsService.UpdateAsync(userId, otherId, updateModel); if (!updateResult.Succeeded) { return(BadRequest(updateResult.Errors)); } return(Ok()); }
public async Task <Result> UpdateAsync(string userId, string otherId, ConnectionUpdateModel model) { var connectionResult = await GetAsync(userId, otherId); if (!connectionResult.Succeeded) { return(Result.Failure(connectionResult.Errors)); } var connectionModel = connectionResult.Data; var connection = new Connection { UserId = connectionModel.UserId, OtherId = connectionModel.OtherId, Notes = connectionModel.Notes, Relationship = connectionModel.Relationship }; connection.Notes = model.Notes; if (connection.Relationship == model.Relationship) { _context.Update(connection); var connectionUpdatedEvent = new ConnectionUpdatedIntegrationEvent(userId, otherId, connection.Notes, connection.Relationship); await _integrationEventService.SaveEventsAndUsersContextChangesAsync(connectionUpdatedEvent); await _integrationEventService.PublishThroughEventBusAsync(connectionUpdatedEvent); return(Result.Success); } var otherResult = await GetAsync(otherId, userId); if (!otherResult.Succeeded) { return(Result.Failure(otherResult.Errors)); } var otherModel = otherResult.Data; var other = new Connection { UserId = otherModel.UserId, OtherId = otherModel.OtherId, Notes = otherModel.Notes, Relationship = otherModel.Relationship }; RelationshipEnum?relationshipForOther = null; switch (connection.Relationship) // TODO refactor or document { case None: { if (model.Relationship == OutgoingRequest) { relationshipForOther = IncomingRequest; } if (model.Relationship == Blocked) { relationshipForOther = BeingBlocked; } break; } case IncomingRequest: { if (model.Relationship == Friend || model.Relationship == OutgoingRequest) { relationshipForOther = Friend; model.Relationship = Friend; } if (model.Relationship == Blocked) { relationshipForOther = BeingBlocked; } break; } case OutgoingRequest: case Friend: { if (model.Relationship == None) { relationshipForOther = None; } if (model.Relationship == Blocked) { relationshipForOther = BeingBlocked; } break; } case Blocked: { if (model.Relationship == None) { if (other.Relationship == Blocked) { model.Relationship = BeingBlocked; } else { relationshipForOther = None; } } break; } case BeingBlocked: { if (model.Relationship == Blocked) { relationshipForOther = Blocked; } break; } } if (!relationshipForOther.HasValue) { var error = Errors.InvalidRelationshipUpdate(); return(Result.Failure(error)); } connection.Relationship = model.Relationship; _context.Update(connection); other.Relationship = relationshipForOther.Value; _context.Update(other); var userConnectionUpdatedEvent = new ConnectionUpdatedIntegrationEvent(userId, otherId, connection.Notes, connection.Relationship); var otherConnectionUpdatedEvent = new ConnectionUpdatedIntegrationEvent(otherId, userId, other.Notes, other.Relationship); await _integrationEventService.SaveEventsAndUsersContextChangesAsync(userConnectionUpdatedEvent, otherConnectionUpdatedEvent); await _integrationEventService.PublishThroughEventBusAsync(userConnectionUpdatedEvent); await _integrationEventService.PublishThroughEventBusAsync(otherConnectionUpdatedEvent); return(Result.Success); }