Ejemplo n.º 1
0
        public static Dictionary <string, object> ToDictionary <TEntity>
            (this DbDeleteCommand <TEntity> from) where TEntity : class
        {
            if (from is null)
            {
                return(null);
            }

            return(new Dictionary <string, object>
            {
                { nameof(from.Id), from.Id }
            });
        }
        public async Task <Application.Core.Responses.DeleteResult> Handle
            (DbDeleteCommand <Actor> request,
            CancellationToken cancellationToken)
        {
            try
            {
                Logger.LogInformation(LogEvent.DatabaseRequest,
                                      "Request={Request}.", request.ToDictionary());

                var validation = new DbDeleteCommandValidator <Actor>()
                                 .Validate(request);
                if (!validation.IsValid)
                {
                    Logger.LogWarning
                        (LogEvent.DatabaseRequestArgumentError,
                        "Database command validation error. Error={Error}.",
                        validation.Errors);
                    return(ErrorResult("Database command validation error."));
                }

                var db      = DbContext.Db;
                var deleted = await db.GetCollection <Actor>
                                  (Defaults.ActorCollectionName)
                              .FindOneAndDeleteAsync(x => x.Id == request.Id)
                              .ConfigureAwait(false);

                if (deleted is null)
                {
                    Logger.LogWarning(LogEvent.DatabaseEmptyResponse,
                                      "Database null response.");
                    return(ErrorResult("Database null response"));
                }

                return(new Application.Core.Responses.DeleteResult(true));
            }
            catch (Exception e)
            {
                Logger.LogWarning(LogEvent.DatabaseExceptionError, e,
                                  "Database exception error. Error={Error}.", e.Message);
                return(ErrorResult("Database exception error"));
            }
        }