Exemplo n.º 1
0
        public ObjectResult Put(RouteInfo routeInfo, object entity)
        {
            var objectId = Convert.ChangeType(routeInfo.Entity.Id.GetValue(entity), routeInfo.Entity.Id.PropertyType);
            var routeId  = Convert.ChangeType(routeInfo.Id, routeInfo.Entity.Id.PropertyType);

            if (!objectId.Equals(routeId))
            {
                return(new BadRequestObjectResult(null));
            }

            if (!IsValid(entity))
            {
                return(new BadRequestObjectResult(this.actionContext.ModelState));
            }

            dbContext.Entry(entity).State = EntityState.Modified;
            dbContext.SaveChanges();

            return(new OkObjectResult(entity));
        }
Exemplo n.º 2
0
        public ObjectResult Get(RouteInfo routeInfo)
        {
            if (routeInfo.Id != null)
            {
                var result = ((dynamic)routeInfo.Entity.DbSet.GetValue(dbContext)).Find(Convert.ChangeType(routeInfo.Id, routeInfo.Entity.Id.PropertyType));
                if (result != null)
                {
                    return(GetOkObjectResult(result));
                }
                else
                {
                    return(new NotFoundObjectResult(null));
                }
            }
            else if (routeInfo.HasModifiers)
            {
                IQueryable dbSet = ((IQueryable)routeInfo.Entity.DbSet.GetValue(dbContext));

                foreach (var include in routeInfo.IncludeExpression)
                {
                    dbSet = (dynamic)EntityFrameworkQueryableExtensions.Include((dynamic)dbSet, include);
                }

                if (!string.IsNullOrWhiteSpace(routeInfo.FilterExpression))
                {
                    dbSet = dbSet.Where(routeInfo.FilterExpression, routeInfo.FilterValues);
                }

                if (routeInfo.Take != 0)
                {
                    dbSet = dbSet.Skip(routeInfo.Skip).Take(routeInfo.Take);
                }

                if (!string.IsNullOrWhiteSpace(routeInfo.SortExpression))
                {
                    dbSet = dbSet.OrderBy(routeInfo.SortExpression);
                }

                if (routeInfo.IsCount)
                {
                    return(GetOkObjectResult(dbSet.Count()));
                }

                if (routeInfo.IsPageResult)
                {
                    return(GetOkObjectResult(GetPagedResult(dbSet, (IQueryable)routeInfo.Entity.DbSet.GetValue(dbContext), routeInfo.Page, routeInfo.Take)));
                }

                return(GetOkObjectResult(dbSet.ToDynamicList()));
            }
            else
            {
                if (routeInfo.IsCount)
                {
                    return(GetOkObjectResult(((IQueryable)routeInfo.Entity.DbSet.GetValue(dbContext)).Count()));
                }
                else if (routeInfo.IsPageResult)
                {
                    return(GetOkObjectResult(GetPagedResult((IQueryable)routeInfo.Entity.DbSet.GetValue(dbContext), (IQueryable)routeInfo.Entity.DbSet.GetValue(dbContext), 1, 0)));
                }
                else
                {
                    return(GetOkObjectResult(routeInfo.Entity.DbSet.GetValue(dbContext)));
                }
            }
        }