public static Dictionary <string, object> ToDictionary (this DbGetActorByNameRequest from) { if (from is null) { return(null); } return(new Dictionary <string, object> { { nameof(from.UserName), from.UserName } }); }
public async Task <EntityResponse <Actor> > Handle (DbGetActorByNameRequest request, CancellationToken cancellationToken) { try { Logger.LogInformation(LogEvent.DatabaseRequest, "Request={Request}.", request.ToDictionary()); var validation = new DbGetActorByNameRequestValidator() .Validate(request); if (!validation.IsValid) { Logger.LogWarning(LogEvent.DatabaseRequestArgumentError, "Request validation error. Error={Error}", validation.Errors); return(ErrorResult("Request validation error.")); } var db = DbContext.Db; var result = await db.GetCollection <Actor> (Defaults.ActorCollectionName).AsQueryable() .Where(t => t.Login == request.UserName) .FirstOrDefaultAsync() .ConfigureAwait(false); if (result is null) { Logger.LogWarning(LogEvent.DatabaseEmptyResponse, "Empty response."); return(ErrorResult("Empty response")); } return(new EntityResponse <Actor>(result)); } catch (Exception e) { Logger.LogError(LogEvent.DatabaseExceptionError, e, "Exception error. Error={Error}.", e.Message); return(ErrorResult("Exception error")); } }
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")); } }
public async Task <ListResponse <Actor> > Handle(ActorListQuery request, CancellationToken cancellationToken) { Logger.LogInformation(AppLogEvent.HandleRequest, "Handle Get Actor List {request}", request.ToDictionary()); if (request is null) { Logger.LogWarning(AppLogEvent.HandleArgumentError, "Handle get task collection request with empty request"); return(ErrorResponse("Request empty argument")); } try { // Validate request var validator = new ActorListQueryValidator(); var validationResult = await validator.ValidateAsync(request) .ConfigureAwait(false); if (!validationResult.IsValid) { Logger.LogError("Query validation error. " + "Request = {request}. Error = {Error}", request.ToDictionary(), validationResult.Errors.Select(x => x.ErrorMessage)); return(ErrorResponse (validationResult.Errors.Select(x => x.ErrorMessage))); } // Get Actor for current user by user name var currentActorRequest = new DbGetActorByNameRequest (request.CurrentPrincipal?.Identity?.Name); var currentActorResponse = await Mediator .Send(currentActorRequest) .ConfigureAwait(false); Actor currentActor = null; if (currentActorResponse.Success) { currentActor = currentActorResponse.Entity; } else { Logger.LogWarning(AppLogEvent.HandleArgumentError, "Not found current actor"); return(ErrorResponse("Not found current actor")); } var defaultLimit = Configuration.GetValue (Defaults.ConfigurationApiDefaultLimitParameterName, Defaults.ConfigurationApiDefaultLimitDefaultValue); var maxLimit = Configuration.GetValue (Defaults.ConfigurationApiMaxLimitParameterName, Defaults.ConfigurationApiMaxLimitDefaultValue); var dbRequest = request.ToDbGetActorFilterRequest(defaultLimit, maxLimit); var dbResponse = await Mediator.Send(dbRequest) .ConfigureAwait(false); return(dbResponse); } catch (Exception e) { Logger.LogError(AppLogEvent.HandleErrorResponse, e, "Call repository exception"); return(ErrorResponse("Not found")); } }
public async Task <CreateResult> Handle(ActorCreateCommand command, CancellationToken cancellationToken) { Logger.LogInformation(AppLogEvent.HandleRequest, "Handle Actor Create Command. Command={Command}", command.ToDictionary()); if (command is null) { Logger.LogWarning(AppLogEvent.HandleArgumentError, "Handle Create Actor Command got empty command"); return(ErrorResult("Empty Actor Create command")); } try { var validator = new ActorCreateCommandValidator(); var validationResult = await validator.ValidateAsync(command) .ConfigureAwait(false); if (!validationResult.IsValid) { Logger.LogWarning(AppLogEvent.RequestValidationError, "Validation command error. Command = {command}. " + "Error = {Error}", command.ToDictionary(), validationResult.Errors); return(ErrorResult(validationResult.Errors .Select(x => x.ErrorMessage))); } // Check duplicates var dbActorRequest = new DbGetActorByNameRequest(command.Login); var duplicateResponse = await Mediator.Send(dbActorRequest) .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")); } // Get Actor for current user by user name Actor createdBy = null; if (!command.DataSeedMode) { var currentUserName = command.CurrentPrincipal? .Identity? .Name; var creatorRequest = new DbGetActorByNameRequest (currentUserName); var creatorResponse = await Mediator.Send(creatorRequest) .ConfigureAwait(false); if (creatorResponse.Success) { createdBy = creatorResponse.Entity; } } var actor = command.ToActor(createdBy?.Id); var validatorBeforeSave = new ActorBeforeSaveValidator(); var validationBeforeSaveResult = await validatorBeforeSave .ValidateAsync(actor) .ConfigureAwait(false); if (!validationBeforeSaveResult.IsValid) { Logger.LogWarning(AppLogEvent.RequestNotValid, "Actor validation error. Entity={Entity}. " + "Error={Error}", actor.ToDictionary(), validationBeforeSaveResult.Errors); return(ErrorResult(validationBeforeSaveResult.Errors .Select(x => x.ErrorMessage))); } if (!command.DataSeedMode) { var checkPermissionResult = await CheckPermission(actor, createdBy) .ConfigureAwait(false); if (!checkPermissionResult.Success) { Logger.LogWarning(AppLogEvent.SecurityNotPassed, "Actor check create permission error. " + "Entity={Entity}. CurrentActor={CurrentActor}." + " Error={Error}", actor.ToDictionary(), createdBy?.ToDictionary(), checkPermissionResult.Errors); return(checkPermissionResult); } } var createCommand = new DbCreateCommand <Actor>(actor); var createResult = await Mediator.Send(createCommand) .ConfigureAwait(false); return(createResult); } catch (Exception e) { Logger.LogError(AppLogEvent.HandleErrorResponse, e, "Call repository exception"); return(ErrorResult("Not found")); } }