Exemplo n.º 1
0
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var objectResult  = (context.Result as ObjectResult);
            var isResultExist = objectResult != null;

            if (isResultExist)
            {
                var operationResult = objectResult.Value as IOperationResult;
                var codeResult      = (int)operationResult.Type;
                objectResult.StatusCode = codeResult;
            }
            else
            {
                var errorList = new List <string>();
                errorList.Add("Controller didn't return objectResult data");
                var data = new OperationResult.OperationResult();
                data.Type   = ResultType.Invalid;
                data.Errors = errorList;

                objectResult            = new ObjectResult(data);
                objectResult.StatusCode = (int)data.Type;
            }

            context.Result = objectResult;
        }
        public OperationResult.OperationResult DeleteOne(Expression <Func <TDocument, bool> > filterExpression)
        {
            var result = new OperationResult.OperationResult();

            try {
                _mongoCollection.DeleteOne(filterExpression);
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public OperationResult.OperationResult AddMany(List <TDocument> documents)
        {
            var result = new OperationResult.OperationResult();

            try {
                _mongoCollection.InsertMany(documents);
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 4
0
        public OperationResult.OperationResult UpdateMany(List <TEntity> entities)
        {
            var result = new OperationResult.OperationResult();

            try {
                _dbSet.UpdateRange(entities);
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 5
0
        public OperationResult.OperationResult Delete(TDocument document)
        {
            var result = new OperationResult.OperationResult();

            try {
                _elasticClient.Delete <TDocument>(document, request => request.Index(_elasticSearchIndex.IndexName));
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 6
0
        public OperationResult.OperationResult AddMany(List <TDocument> documents)
        {
            var result = new OperationResult.OperationResult();

            try {
                _elasticClient.IndexMany(documents, _elasticSearchIndex.IndexName);
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public OperationResult.OperationResult ReplaceOne(TDocument document)
        {
            var result = new OperationResult.OperationResult();

            try {
                _mongoCollection.ReplaceOne(doc => doc.Id.Equals(document.Id), document);
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 8
0
        public OperationResult.OperationResult Delete(TEntity entity)
        {
            var result = new OperationResult.OperationResult();

            try {
                _dbSet.Remove(entity);
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 9
0
        public async Task <OperationResult.OperationResult> DeleteManyAsync(List <TDocument> documents)
        {
            var result = new OperationResult.OperationResult();

            try {
                await _elasticClient.DeleteManyAsync(documents, _elasticSearchIndex.IndexName);

                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 10
0
        public async Task <OperationResult.OperationResult> AddManyAsync(List <TEntity> entities)
        {
            var result = new OperationResult.OperationResult();

            try {
                await _dbSet.AddRangeAsync(entities);

                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 11
0
        public async Task <OperationResult.OperationResult> DeleteManyAsync(List <TEntity> entities)
        {
            var result = new OperationResult.OperationResult();

            try {
                await Task.Run(() => _dbSet.RemoveRange(entities));

                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
Exemplo n.º 12
0
        public async Task <OperationResult.OperationResult> AddAsync(TDocument document)
        {
            var result = new OperationResult.OperationResult();

            try {
                await _elasticClient.IndexAsync(document, request => request.Index(_elasticSearchIndex.IndexName));

                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public OperationResult.OperationResult DeleteById(string id)
        {
            var result   = new OperationResult.OperationResult();
            var objectId = new ObjectId(id);

            try {
                _mongoCollection.DeleteOne(item => item.Id.Equals(objectId));
                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public async Task <OperationResult.OperationResult> AddAsync(TDocument document)
        {
            var result = new OperationResult.OperationResult();

            try {
                await _mongoCollection.InsertOneAsync(document);

                result.Type = ResultType.Success;
            }
            catch (Exception exception) {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public OperationResult.OperationResult Add(IClientSessionHandle clientSession, TDocument document)
        {
            var result = new OperationResult.OperationResult();

            try
            {
                _mongoCollection.InsertOne(clientSession, document);
                result.Type = ResultType.Success;
            }
            catch (Exception exception)
            {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public async Task <OperationResult.OperationResult> DeleteManyAsync(IClientSessionHandle clientSession, Expression <Func <TDocument, bool> > filterExpression)
        {
            var result = new OperationResult.OperationResult();

            try
            {
                await _mongoCollection.DeleteManyAsync(clientSession, filterExpression);

                result.Type = ResultType.Success;
            }
            catch (Exception exception)
            {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public async Task <OperationResult.OperationResult> ReplaceOneAsync(IClientSessionHandle clientSession, TDocument document)
        {
            var result = new OperationResult.OperationResult();

            try
            {
                await _mongoCollection.ReplaceOneAsync(clientSession, doc => doc.Id.Equals(document.Id), document);

                result.Type = ResultType.Success;
            }
            catch (Exception exception)
            {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public async Task <OperationResult.OperationResult> AddManyAsync(IClientSessionHandle clientSession, List <TDocument> documents)
        {
            var result = new OperationResult.OperationResult();

            try
            {
                await _mongoCollection.InsertManyAsync(clientSession, documents);

                result.Type = ResultType.Success;
            }
            catch (Exception exception)
            {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public async Task <OperationResult.OperationResult> DeleteByIdAsync(IClientSessionHandle clientSession, string id)
        {
            var result   = new OperationResult.OperationResult();
            var objectId = new ObjectId(id);

            try
            {
                await _mongoCollection.DeleteOneAsync(clientSession, item => item.Id.Equals(objectId));

                result.Type = ResultType.Success;
            }
            catch (Exception exception)
            {
                result.Type = ResultType.Invalid;
                result.Errors.Add(exception.Message);
            }

            return(result);
        }
        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            var objectResult           = context.Result as ObjectResult;
            var fluentValidationResult = objectResult?.Value as ValidationProblemDetails;
            var operationResult        = new OperationResult.OperationResult();

            if (fluentValidationResult != null)
            {
                var errors = fluentValidationResult.Errors
                             .SelectMany(item => item.Value.Select(value => item.Key + ": " + value))
                             .ToList();
                operationResult.Type   = (ResultType)fluentValidationResult.Status;
                operationResult.Errors = errors;

                objectResult.Value = operationResult;
            }

            await next();
        }
        public async Task OnExceptionAsync(ExceptionContext context)
        {
            var errorList = new List <string>();
            var data      = new OperationResult.OperationResult();
            var isDevEnv  = _environment.IsDevelopment();

            errorList.Add(context.Exception.Message);
            _logger.LogError(context.Exception, context.Exception.Message);

            if (isDevEnv)
            {
                errorList.Add(context.Exception.StackTrace);
            }

            data.Errors = errorList;
            data.Type   = ResultType.Invalid;

            var result = new ObjectResult(data);

            result.StatusCode = (int)data.Type;

            context.Result = result;
        }