Beispiel #1
0
        public ResultHandler <Expression <Func <EventModel, bool> > > BuildExpression(FilterStatement <EventModel> filterStatement)
        {
            try
            {
                ExpressionStarter <EventModel> result = PredicateBuilder.New <EventModel>();

                switch (filterStatement.PropertyName)
                {
                case "Audience":
                    var values = JsonConvert.DeserializeObject <IEnumerable <int> >(filterStatement.ValueJson);
                    foreach (var value in values)
                    {
                        result.Or(x => x.Audience == value);
                    }
                    break;

                default:
                    throw new NotImplementedException("No filter support for this property");
                }

                return(ResultHandler.Ok <Expression <Func <EventModel, bool> > >(result));
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex, "MultiCheckboxFilter.BuildExpression Failure");
                return(ResultHandler.Fail <Expression <Func <EventModel, bool> > >(ex));
            }
        }
        //public bool CheckIsApproval(int id)
        //{
        //    try
        //    {
        //        var order = this._dataAccess.GetOrder(id);
        //        // Compute if the order is approved
        //        bool response = CheckIfApproved(order);

        //        return response;
        //    }
        //    catch (Exception ex)
        //    {
        //        _log.LogError(ex, "CheckIsApproval Failed");
        //        return false;
        //    }
        //}
        public async Task <ResultHandler> InsertRange(IEnumerable <EventModel> items)
        {
            try
            {
                await this._repository.InsertRange(items);

                return(ResultHandler.Ok());
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Failed inserting object to DB");
                return(ResultHandler.Fail(ex));
            }
        }
        public async Task <ResultHandler> Update(EventModel obj)
        {
            try
            {
                await this._repository.Update(obj);

                return(ResultHandler.Ok(obj));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Failed to update object");
                return(ResultHandler.Fail(ex));
            }
        }
        public ResultHandler <Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > > GetOrderBy <TEntity>(OrderByStatement <TEntity> orderByStatement)
        {
            try
            {
                if (orderByStatement == null ||
                    !orderByStatement.IsValid)
                {
                    return(ResultHandler.Fail <Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > >(ErrorCode.ValidationError));
                }

                //1. Detemine method name.
                string methodName = orderByStatement.Direction == OrderByDirection.Asc ? "OrderBy" : "OrderByDescending";

                //2. Build a outer expression
                Type typeQueryable = typeof(IQueryable <TEntity>);
                ParameterExpression argQueryable          = Expression.Parameter(typeQueryable, "p");      //p
                LambdaExpression    outerLambdaExpression = Expression.Lambda(argQueryable, argQueryable); // p => p
                Expression          outerExpression       = outerLambdaExpression.Body;

                //3. Build a TEntity parameter expression
                Type type = typeof(TEntity);
                ParameterExpression arg = Expression.Parameter(type, "x");

                //4. Build a Memeber expression
                PropertyInfo     pi         = type.GetProperty(orderByStatement.PropertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                MemberExpression expr       = Expression.Property(arg, pi);
                Type             memberType = pi.PropertyType;

                //5. Build inner expression:
                LambdaExpression lambda          = Expression.Lambda(expr, arg);
                Expression       innerExpression = Expression.Quote(lambda);

                MethodCallExpression resultExp = Expression.Call(typeof(Queryable),
                                                                 methodName,
                                                                 new Type[] { typeof(TEntity), memberType },
                                                                 outerExpression, innerExpression);

                var finalLambda = Expression.Lambda(resultExp, argQueryable);

                Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > result = (Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> >)finalLambda.Compile();
                return(ResultHandler.Ok(result));
            }
            catch (Exception ex)
            {
                return(ResultHandler.Fail <Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > >(ex));
            }
        }
        public async Task <ResultHandler <EventModel> > GetById(Guid id)
        {
            try
            {
                var entity = await this._repository.GetById(id);

                if (entity == null)
                {
                    return(ResultHandler.Fail <EventModel>(ErrorCode.EntityNotFound));
                }

                return(ResultHandler.Ok <EventModel>(entity));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Failed to get object with id: {0} from DB", id);
                return(ResultHandler.Fail <EventModel>(ex));
            }
        }
        public async Task <ResultHandler> Delete(Guid id)
        {
            try
            {
                var entity = await this._repository.GetById(id);

                if (entity == null)
                {
                    return(ResultHandler.Fail(ErrorCode.EntityNotFound, "Entity not found"));
                }
                await this._repository.Remove(entity);

                return(ResultHandler.Ok());
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Failed to  delete object with id: {0} from DB", id);
                return(ResultHandler.Fail(ex));
            }
        }
        public async Task <ResultHandler <bool> > IsOwner(Guid id, string ownerId)
        {
            try
            {
                var isOwner = await this._repository.IsOwner(id, ownerId);

                if (isOwner)
                {
                    return(ResultHandler.Ok <bool>(isOwner));
                }
                else
                {
                    return(ResultHandler.Fail <bool>(ErrorCode.Unauthorized));
                }
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Failed to get object with id: {0} from DB", id);
                return(ResultHandler.Fail <bool>(ex));
            }
        }
        public ResultHandler <IAsyncEnumerable <EventModel> > Get(IEnumerable <FilterStatement <EventModel> > filterStatements,
                                                                  OrderByStatement <EventModel> orderByStatement = null,
                                                                  string includeProperties = "")
        {
            try
            {
                IEnumerable <Expression <Func <EventModel, bool> > > filters = null;
                if (filterStatements != null)
                {
                    var filtersResult = this._filtersService.BuildExpressions(filterStatements);
                    if (!filtersResult.Success)
                    {
                        return(ResultHandler.Fail <IAsyncEnumerable <EventModel> >(filtersResult.ErrorCode));
                    }
                    filters = filtersResult.Value;
                }

                Func <IQueryable <EventModel>, IOrderedQueryable <EventModel> > orderBy = null;
                if (orderByStatement != null)
                {
                    var orderByService = new OrderByService();
                    var orderByResult  = orderByService.GetOrderBy <EventModel>(orderByStatement);
                    if (!orderByResult.Success)
                    {
                        return(ResultHandler.Fail <IAsyncEnumerable <EventModel> >(orderByResult.ErrorCode));
                    }
                    orderBy = orderByResult.Value as Func <IQueryable <EventModel>, IOrderedQueryable <EventModel> >;
                }

                IAsyncEnumerable <EventModel> result = this._repository.Get(filters, orderBy, includeProperties);

                return(ResultHandler.Ok(result));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Failed to get objects from DB");
                return(ResultHandler.Fail <IAsyncEnumerable <EventModel> >(ex));
            }
        }
Beispiel #9
0
        public ResultHandler <IEnumerable <Expression <Func <EventModel, bool> > > > BuildExpressions(IEnumerable <FilterStatement <EventModel> > filterStatements)
        {
            try
            {
                List <Expression <Func <EventModel, bool> > > result = new List <Expression <Func <EventModel, bool> > >();
                foreach (var filterStatement in filterStatements)
                {
                    IFilter <EventModel> filter = this._eventFilterResolver(filterStatement.FilterType);
                    ResultHandler <Expression <Func <EventModel, bool> > > expressionResult = filter.BuildExpression(filterStatement);
                    if (!expressionResult.Success)
                    {
                        this._logger.LogError(expressionResult.ErrorMessage);
                        return(ResultHandler.Fail <IEnumerable <Expression <Func <EventModel, bool> > > >(expressionResult.ErrorCode));
                    }

                    result.Add(expressionResult.Value);
                }
                return(ResultHandler.Ok <IEnumerable <Expression <Func <EventModel, bool> > > >(result));
            }
            catch (Exception ex)
            {
                return(ResultHandler.Fail <IEnumerable <Expression <Func <EventModel, bool> > > >(ex));
            }
        }