Exemplo n.º 1
0
        private static bool ProcessEntitySetAction(string actionName, IEdmEntitySet entitySet, IEdmStructuredType castType,
                                                   ODataControllerActionContext context, ActionModel action)
        {
            if (actionName == "Get" || actionName == $"Get{entitySet.Name}")
            {
                IEdmCollectionType castCollectionType = null;
                if (castType != null)
                {
                    castCollectionType = castType.ToCollection(true);
                }

                IEdmCollectionType entityCollectionType = entitySet.EntityType().ToCollection(true);

                // GET ~/Customers or GET ~/Customers/Ns.VipCustomer
                IList <ODataSegmentTemplate> segments = new List <ODataSegmentTemplate>
                {
                    new EntitySetSegmentTemplate(entitySet)
                };
                if (castType != null)
                {
                    segments.Add(new CastSegmentTemplate(castCollectionType, entityCollectionType, entitySet));
                }
                ODataPathTemplate template = new ODataPathTemplate(segments);
                action.AddSelector("Get", context.Prefix, context.Model, template, context.Options?.RouteOptions);

                // GET ~/Customers/$count or GET ~/Customers/Ns.VipCustomer/$count
                segments = new List <ODataSegmentTemplate>
                {
                    new EntitySetSegmentTemplate(entitySet)
                };
                if (castType != null)
                {
                    segments.Add(new CastSegmentTemplate(castCollectionType, entityCollectionType, entitySet));
                }
                segments.Add(CountSegmentTemplate.Instance);

                template = new ODataPathTemplate(segments);
                action.AddSelector("Get", context.Prefix, context.Model, template, context.Options?.RouteOptions);
                return(true);
            }
            else if (actionName == "Post" || actionName == $"Post{entitySet.EntityType().Name}")
            {
                // POST ~/Customers
                IList <ODataSegmentTemplate> segments = new List <ODataSegmentTemplate>
                {
                    new EntitySetSegmentTemplate(entitySet)
                };
                if (castType != null)
                {
                    IEdmCollectionType castCollectionType   = castType.ToCollection(true);
                    IEdmCollectionType entityCollectionType = entitySet.EntityType().ToCollection(true);
                    segments.Add(new CastSegmentTemplate(castCollectionType, entityCollectionType, entitySet));
                }
                ODataPathTemplate template = new ODataPathTemplate(segments);
                action.AddSelector("Post", context.Prefix, context.Model, template, context.Options?.RouteOptions);
                return(true);
            }
            else if (actionName == "Patch" || actionName == $"Patch{entitySet.Name}")
            {
                // PATCH ~/Patch  , ~/PatchCustomers
                IList <ODataSegmentTemplate> segments = new List <ODataSegmentTemplate>
                {
                    new EntitySetSegmentTemplate(entitySet)
                };

                if (castType != null)
                {
                    IEdmCollectionType castCollectionType   = castType.ToCollection(true);
                    IEdmCollectionType entityCollectionType = entitySet.EntityType().ToCollection(true);
                    segments.Add(new CastSegmentTemplate(castCollectionType, entityCollectionType, entitySet));
                }

                ODataPathTemplate template = new ODataPathTemplate(segments);
                action.AddSelector("Patch", context.Prefix, context.Model, template, context.Options?.RouteOptions);
                return(true);
            }

            return(false);
        }