public async Task <UpdateResult> Handle (DbUpdateCommand <Geo> command, CancellationToken cancellationToken) { try { Logger.LogInformation(LogEvent.DatabaseRequest, "Request={Request}.", command.ToDictionary()); var validation = new DbUpdateCommandGeoValidator() .Validate(command); if (!validation.IsValid) { Logger.LogWarning (LogEvent.DatabaseRequestArgumentError, "Database command validation error. Error={Error}.", validation.Errors); return(ErrorResult("Database command validation error.")); } var dbGeo = command.Entity.ToEntity <DbGeo>(); var updated = await DbContext.Db.GetCollection <DbGeo> (Defaults.GeoCollectionName) .FindOneAndReplaceAsync (x => x.Id == dbGeo.Id, dbGeo) .ConfigureAwait(false); if (string.IsNullOrEmpty(updated?.Id)) { return(ErrorResult("Unknown Error.")); } return(new UpdateResult(success: true)); } catch (Exception e) { Logger.LogWarning(LogEvent.DatabaseExceptionError, e, "Database exception error. Error={Error}.", e.Message); return(ErrorResult("Database exception error")); } }
public static Dictionary <string, object> ToDictionary (this DbUpdateCommand <GeoTask> from) { if (from is null) { return(null); } if (from.Entity is null) { return(new Dictionary <string, object>()); } return(new Dictionary <string, object> { { nameof(from.Entity.AssistentActors), string.Join(',', from.Entity.AssistentActors .Select(x => x.Id)) }, { nameof(from.Entity.CreatedAt), from.Entity.CreatedAt }, { nameof(from.Entity.CreatedBy), from.Entity.CreatedBy?.Id }, { nameof(from.Entity.Description), from.Entity.Description }, { nameof(from.Entity.GeosIds), string.Join(',', from.Entity.GeosIds) }, { nameof(from.Entity.Id), from.Entity.Id }, { nameof(from.Entity.IsArchived), from.Entity.IsArchived }, { nameof(from.Entity.ObserverActors), string.Join(',', from.Entity.ObserverActors .Select(x => x.Id)) }, { nameof(from.Entity.PlanFinishAt), from.Entity.PlanFinishAt }, { nameof(from.Entity.PlanStartAt), from.Entity.PlanStartAt }, { nameof(from.Entity.ProjectId), from.Entity.ProjectId }, { nameof(from.Entity.ResponsibleActor), from.Entity.ResponsibleActor?.Id }, { nameof(from.Entity.Status), from.Entity.Status }, { nameof(from.Entity.StatusChangedAt), from.Entity.StatusChangedAt }, { nameof(from.Entity.Title), from.Entity.Title }, }); }
public static Dictionary <string, object> ToDictionary (this DbUpdateCommand <Actor> from) { if (from is null) { return(null); } if (from.Entity is null) { return(new Dictionary <string, object>()); } var dict = new Dictionary <string, object>(); var entityDict = from.Entity.ToDictionary(); foreach (var item in entityDict) { dict.TryAdd(item.Key, item.Value); } return(dict); }
public async Task <UpdateResult> Handle(ActorUpdateCommand command, CancellationToken cancellationToken) { try { Logger.LogInformation(AppLogEvent.HandleRequest, "Handle Actor Update {Command}", command.ToDictionary()); if (command is null || string.IsNullOrWhiteSpace(command.Id)) { Logger.LogWarning(AppLogEvent.HandleArgumentError, "Actor Update Command is empty"); return(ErrorResult("Actor Update Command is empty")); } var validator = new ActorUpdateCommandValidator(); var validationResult = await validator.ValidateAsync(command) .ConfigureAwait(false); if (!validationResult.IsValid) { var validationErrors = validationResult.Errors .Select(x => x.ErrorMessage) .ToList(); Logger.LogWarning(AppLogEvent.HandleArgumentError, "Actor Update Command validation error. " + "Command={Command}. Error={Error}.", command.ToDictionary(), validationErrors); return(ErrorResult(validationErrors)); } var oldActorResponse = await Mediator .Send(new DbGetEntityByIdRequest <Actor>(command.Id)) .ConfigureAwait(false); if (!oldActorResponse.Success) { Logger.LogWarning(AppLogEvent.HandleErrorResponse, "Get actor for update error. Id={Id}. Error={Error}", command.Id, oldActorResponse.Errors); return(ErrorResult(oldActorResponse.Errors)); } var oldActor = oldActorResponse.Entity; var currentActorRequest = new DbGetActorByNameRequest (command.CurrentPrincipal?.Identity?.Name); var currentActorResponse = await Mediator .Send(currentActorRequest) .ConfigureAwait(false); var currentActor = currentActorResponse.Success ? currentActorResponse.Entity : null; Actor newActor = command.ToActor(oldActor); // Check duplicates for new login if (newActor.Login != oldActor.Login) { var duplicateRequest = new DbGetActorByNameRequest (newActor.Login); var duplicateResponse = await Mediator .Send(duplicateRequest) .ConfigureAwait(false); if (duplicateResponse.Success && duplicateResponse.Entity != null) { Logger.LogWarning(AppLogEvent.RequestValidationError, "Validation command error. Command={command}. " + "Error={Error}.", command.ToDictionary(), "Duplicated actor login"); return(ErrorResult("Duplicated actor login")); } } var validatorBeforeSave = new ActorBeforeSaveValidator(); var validationBeforeSaveResult = await validatorBeforeSave .ValidateAsync(newActor) .ConfigureAwait(false); if (!validationBeforeSaveResult.IsValid) { Logger.LogWarning(AppLogEvent.RequestValidationError, "Update Actor validation error. Entity={Entity}. " + "Error={Error}.", newActor.ToDictionary(), validationBeforeSaveResult.Errors); return(ErrorResult(validationBeforeSaveResult.Errors .Select(x => x.ErrorMessage))); } var permissionValidateResult = await CheckPermissionAsync (oldActor, newActor, currentActor) .ConfigureAwait(false); if (!permissionValidateResult.IsValid) { Logger.LogWarning(AppLogEvent.SecurityNotPassed, "Current actor has no rights to update Actor. " + "Actor={Actor}. Entity before update={OldEntity}. " + "Entity after update={NewEntity}. Error={Error}.", currentActor.ToDictionary(), oldActor.ToDictionary(), newActor.ToDictionary(), permissionValidateResult.Errors .Select(x => x.ErrorMessage)); return(ErrorResult(permissionValidateResult.Errors .Select(x => x.ErrorMessage))); } var updateCommand = new DbUpdateCommand <Actor>(newActor); var updateResult = await Mediator.Send(updateCommand) .ConfigureAwait(false); return(updateResult); } catch (Exception e) { Logger.LogError(AppLogEvent.HandleErrorResponse, e, "Actor update exception"); return(ErrorResult("Actor update exception")); } }