Пример #1
0
        public override void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            _subMapper?.Invoke(this);

            var routes =
                _collections.Select(
                    collection =>
                    CollectionRoute(routeBuilder, collection.Key, collection.Value.Key, collection.Value.Value))
                .ToList();

            AddIncludedActions(routeBuilder, routes);
            routes.AddRange(
                _members.Select(member => MemberRoute(routeBuilder, member.Key, member.Value.Key, member.Value.Value)));

            foreach (var route in routes)
            {
                ConfigureRoute(route);
                routeBuilder.Routes.Add(route);
            }

            if (Mappers.Any())
            {
                var parentIdParameterName = SingularResourceName + Inflector.Capitalize(IdParameterName);
                BasePath = Join(ResourcePath, $"{{{parentIdParameterName}}}");
                var idConstraint = Constraints[IdParameterName];
                if (idConstraint != null)
                {
                    Constraints.Remove(IdParameterName);
                    Constraints.Add(parentIdParameterName, idConstraint);
                }

                AddResourcePath(SingularResourceName);
                RegisterNested(routeBuilder, mapper => mapper.SetParentResources(ResourcePaths));
            }
        }
Пример #2
0
 public void Capitalize()
 {
     foreach (var pair in TestData)
     {
         Assert.AreEqual(Inflector.Capitalize(pair.Key), pair.Value);
     }
 }
Пример #3
0
 public void Singularize()
 {
     foreach (var dictionaryEntry in singularToPlural)
     {
         Assert.Equal(dictionaryEntry.Key, Inflector.Singularize((string)dictionaryEntry.Value));
         Assert.Equal(Inflector.Capitalize((string)dictionaryEntry.Key),
                      Inflector.Singularize(Inflector.Capitalize((string)dictionaryEntry.Value)));
     }
 }
Пример #4
0
 public void Pluralize()
 {
     foreach (DictionaryEntry dictionaryEntry in singularToPlural)
     {
         Assert.AreEqual(dictionaryEntry.Value, Inflector.Pluralize((string)dictionaryEntry.Key));
         Assert.AreEqual(Inflector.Capitalize((string)dictionaryEntry.Value),
                         Inflector.Pluralize(Inflector.Capitalize((string)dictionaryEntry.Key)));
     }
 }
Пример #5
0
 private static void RenderToFile(IViewEngineManager brail, string name, string file, Dictionary <string, object> arguments)
 {
     using (var writer = new StreamWriter(File.Create(file))) {
         brail.Process(
             Inflector.Capitalize(name) + "/Print",
             "Print",
             writer,
             arguments);
     }
 }
Пример #6
0
        private static string ProcessRole(string privileges)
        {
            string role = Roles.Student;
            long   privilegesVal;

            if (long.TryParse(privileges, out privilegesVal))
            {
                if (CheckRole(privilegesVal, RightsFlags.ControlCourse))
                {
                    role = Roles.Owner;
                }
                else if (CheckRole(privilegesVal, RightsFlags.ReadCourse) &&
                         CheckRole(privilegesVal, RightsFlags.UpdateCourse) &&
                         CheckRole(privilegesVal, RightsFlags.GradeAssignment) &&
                         CheckRole(privilegesVal, RightsFlags.GradeForum) &&
                         CheckRole(privilegesVal, RightsFlags.GradeExam) &&
                         CheckRole(privilegesVal, RightsFlags.SetupGradebook) &&
                         CheckRole(privilegesVal, RightsFlags.ReadGradebook) &&
                         CheckRole(privilegesVal, RightsFlags.SubmitFinalGrade) &&
                         CheckRole(privilegesVal, RightsFlags.ReadCourseFull))
                {
                    role = Roles.Teacher;
                }
                else if (CheckRole(privilegesVal, RightsFlags.ReadCourse) &&
                         CheckRole(privilegesVal, RightsFlags.UpdateCourse) &&
                         CheckRole(privilegesVal, RightsFlags.ReadCourseFull))
                {
                    role = Roles.Author;
                }
                else if (CheckRole(privilegesVal, RightsFlags.Participate) &&
                         CheckRole(privilegesVal, RightsFlags.ReadCourse))
                {
                    role = Roles.Student;
                }
                else if (CheckRole(privilegesVal, RightsFlags.ReadCourse))
                {
                    role = Roles.Reader;
                }
            }

            role = Inflector.Capitalize(role);
            return(role);
        }
Пример #7
0
        public override void RegisterRoutes(RouteCollection routeCollection)
        {
            if (subMapper != null)
            {
                subMapper.Invoke(this);
            }

            var routes = collections.Select(collection => CollectionRoute(collection.Key, collection.Value.Key, collection.Value.Value)).ToList();

            AddIncludedActions(routes);

            routes.AddRange(members.Select(member => MemberRoute(member.Key, member.Value.Key, member.Value.Value)));

            if (GenerateFormatRoutes)
            {
                AddFormatRoutes(routes);
            }

            foreach (var route in routes)
            {
                ConfigureRoute(route);
                AppendRouteTo(routeCollection, route);
            }

            if (Mappers.Any())
            {
                string parentIdParameterName = SingularResourceName + Inflector.Capitalize(IdParameterName);
                BasePath = Join(ResourcePath, "{" + parentIdParameterName + "}");
                var idConstraint = Constraints[IdParameterName];
                if (idConstraint != null)
                {
                    Constraints.Remove(IdParameterName);
                    Constraints.Add(parentIdParameterName, idConstraint);
                }

                AddResourcePath(SingularResourceName);
                RegisterNested(routeCollection, mapper => mapper.SetParentResources(ResourcePaths));
            }
        }
Пример #8
0
 /// <summary>
 /// Capitalize the specified string.
 /// </summary>
 /// <param name="source">Source string to capitalize</param>
 /// <returns>Returns the capitalized string</returns>
 public static string Capitalize(this string source)
 {
     return Inflector.Capitalize(source);
 }
Пример #9
0
        public override string SelectAction(ODataPath odataPath, HttpControllerContext context, ILookup <string, HttpActionDescriptor> actionMap)
        {
            var path   = odataPath?.PathTemplate;
            var method = context?.Request?.Method;

            if (path == null || method == null)
            {
                return(null);
            }

            if (_navigationPaths.Contains(path))
            {
                // Standard OData path differ:
                // ~/entityset/key/$links/navigation (OData 3 "link"), ~/entityset/key/navigation/$ref (OData 4 "reference").

                if (method == HttpMethod.Get || method == HttpMethod.Post || method == HttpMethod.Delete)
                {
                    // Add keys to route data, so they will bind to action parameters.
                    if (GetNormalizedKey(odataPath, 1, out var key) && key != 0)
                    {
                        context.RouteData.Values[ODataRouteConstants.Key] = key;
                    }
                    else
                    {
                        throw context.Request.BadRequestException(WebApiGlobal.Error.NoKeyFromPath);
                    }

                    var navPropertyName = (odataPath.Segments[2] as NavigationPathSegment)?.NavigationPropertyName;
                    if (navPropertyName.IsEmpty())
                    {
                        throw context.Request.BadRequestException(WebApiGlobal.Error.NoNavigationFromPath);
                    }

                    // Allow relatedKey = 0 to remove all assignments.
                    if (GetNormalizedKey(odataPath, 3, out var relatedKey))
                    {
                        context.RouteData.Values[ODataRouteConstants.RelatedKey] = relatedKey;
                    }
                    else if (method == HttpMethod.Post)
                    {
                        // relatedKey is mandatory.
                        throw context.Request.BadRequestException(WebApiGlobal.Error.NoRelatedKeyFromPath);
                    }

                    var methodName = Inflector.Capitalize(method.ToString()) + navPropertyName;
                    if (actionMap.Contains(methodName))
                    {
                        return(methodName);
                    }
                }
            }
            else if (_propertyPaths.Contains(path))
            {
                if (method == HttpMethod.Get)
                {
                    if (path.StartsWith("~/entityset/key"))
                    {
                        if (GetNormalizedKey(odataPath, 1, out var key) && key != 0)
                        {
                            context.RouteData.Values[ODataRouteConstants.Key] = key;
                        }
                        else
                        {
                            throw context.Request.BadRequestException(WebApiGlobal.Error.NoKeyFromPath);
                        }
                    }

                    var propertyName = (odataPath.Segments.Last() as PropertyAccessPathSegment)?.PropertyName;
                    if (propertyName.HasValue())
                    {
                        context.RouteData.Values["propertyName"] = propertyName;
                    }
                    else
                    {
                        throw context.Request.BadRequestException(WebApiGlobal.Error.PropertyNotFound.FormatInvariant(string.Empty));
                    }

                    var methodName = Inflector.Capitalize(method.ToString()) + "Property";
                    if (actionMap.Contains(methodName))
                    {
                        return(methodName);
                    }
                }
            }

            // Not a match. We do not support requested path.
            return(null);
        }