public async Task <IDictionary <Type, Guid> > WhoAmIAsync()
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                if (!_currentIdentityProvider.IsAuthenticated)
                {
                    return(new Dictionary <Type, Guid>());
                }

                Identity identity = await _identityQueryRepository.GetByIdAsync(_currentIdentityProvider.Id);

                if (identity == null || identity.Session.IsRevoked)
                {
                    throw new BusinessApplicationException(ExceptionType.Unauthorized, "Current identity token is not valid");
                }

                DictionaryBuilder <Type, Guid> whoAmI = DictionaryBuilder <Type, Guid> .Create();

                whoAmI.Add(typeof(Identity), identity.Id);

                if (_currentUserProvider.Id.HasValue)
                {
                    User user = await _userQueryRepository.GetByIdAsync(_currentUserProvider.Id.Value);

                    if (user != null)
                    {
                        whoAmI.Add(typeof(User), user.Id);
                    }
                }

                transaction.Commit();

                return(whoAmI.Build());
            }
        }
Exemple #2
0
        public static FormattedArrayResponse Create(BuiltCollectionResource builtCollectionResource, HttpRequest httpRequest)
        {
            bool includeExtendedMeta = httpRequest.Headers.ContainsKey(HeaderType.ExtendedMeta);

            DictionaryBuilder <string, object> metaBuilder = DictionaryBuilder <string, object> .Create();

            metaBuilder.Add(MetaType.TotalResults, builtCollectionResource.TotalResults);

            if (builtCollectionResource.BuiltResources.Any(
                    r => r.Properties.Any(p => p.Constraints.IsSortable ?? false)))
            {
                DictionaryBuilder <string, object> sortBuilder = DictionaryBuilder <string, object> .Create();

                sortBuilder.Add(MetaType.Values, builtCollectionResource.BuiltResources.First().Properties.Where(p => p.Constraints.IsSortable ?? false).Select(p => p.Name.ToCamelCase()));

                metaBuilder.Add(MetaType.Sort, sortBuilder.Build());
            }

            return(new FormattedArrayResponse
            {
                Data = builtCollectionResource.BuiltResources.Select(r =>
                {
                    DictionaryBuilder <string, object> attributeBuilder = DictionaryBuilder <string, object> .Create();

                    foreach (Property builtResourceProperty in r.Properties)
                    {
                        attributeBuilder.Add(builtResourceProperty.Name, builtResourceProperty.Value);
                    }

                    return new Data
                    {
                        Type = GetResourceTypeName(r.Type),
                        Id = r.Id,
                        Attributes = attributeBuilder.Build(),
                        Links = _buildLinks(r.Links),
                        Meta = _buildMeta(r.Properties, includeExtendedMeta)
                    };
                }
                                                                     ),
                Links = _buildLinks(builtCollectionResource.Links),
                Meta = metaBuilder.Build()
            });
        }
Exemple #3
0
        private static IDictionary <string, object> _buildMeta(IEnumerable <Property> properties, bool includeExtendedMeta)
        {
            DictionaryBuilder <string, object> metaBuilder = DictionaryBuilder <string, object> .Create();

            foreach (Property property in properties)
            {
                metaBuilder.Add(property.Name, _buildPropertyMeta(property, includeExtendedMeta));
            }

            return(metaBuilder.Build());
        }
Exemple #4
0
        private IDictionary <string, object> _getAdditionalParams(ISortTemplate sortTemplate)
        {
            DictionaryBuilder <string, object> dictionaryBuilder = DictionaryBuilder <string, object> .Create();

            List <string> handledProperties = new List <string>
            {
                nameof(sortTemplate.Sort)
            };

            foreach (PropertyInfo propertyInfo in sortTemplate.GetType().GetProperties().Where(p => !handledProperties.Contains(p.Name)))
            {
                dictionaryBuilder.Add(propertyInfo.Name, propertyInfo.GetValue(sortTemplate).ToString());
            }

            return(dictionaryBuilder.Build());
        }
Exemple #5
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override IDictionary <K, V> ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                             out bool success)
        {
            if (input.IsNull())
            {
                success = true;
                return(null);
            }

            var keyConvertor   = context.Get <K>();
            var valueConvertor = context.Get <V>();


            var builder = new DictionaryBuilder(context, outputType, keyConvertor, valueConvertor);

            if (builder.TryCreateInstance() == false)
            {
                success = false;
                return(null);
            }
            var mapper = new Mapper(input);

            if (mapper.Error != null)
            {
                context.AddException(mapper.Error);
                success = false;
                return(null);
            }

            while (mapper.MoveNext())
            {
                if (builder.Add(mapper.Key, mapper.Value) == false)
                {
                    success = false;
                    return(null);
                }
            }

            success = true;
            return(builder.Instance);
        }
Exemple #6
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        /// <returns> </returns>
        protected override IDictionary ChangeTypeImpl(ConvertContext context, object input, Type outputType,
                                                      out bool success)
        {
            if ((input == null) || input is DBNull)
            {
                success = true;
                return(null);
            }

            var builder = new DictionaryBuilder(outputType, context);

            if (builder.TryCreateInstance() == false)
            {
                success = false;
                return(null);
            }

            var mapper = new Mapper(input);

            if (mapper.Error != null)
            {
                context.AddException(mapper.Error);
                success = false;
                return(null);
            }

            while (mapper.MoveNext())
            {
                if (builder.Add(mapper.Key, mapper.Value) == false)
                {
                    success = false;
                    return(null);
                }
            }

            success = true;
            return(builder.Instance);
        }
Exemple #7
0
        public static FormattedResponse Create(BuiltResource builtResource, HttpRequest httpRequest)
        {
            bool includeExtendedMeta = httpRequest.Headers.ContainsKey(HeaderType.ExtendedMeta);

            DictionaryBuilder <string, object> attributeBuilder = DictionaryBuilder <string, object> .Create();

            foreach (Property builtResourceProperty in builtResource.Properties)
            {
                attributeBuilder.Add(builtResourceProperty.Name, builtResourceProperty.Value);
            }

            return(new FormattedResponse
            {
                Data = new Data
                {
                    Type = GetResourceTypeName(builtResource.Type),
                    Id = builtResource.Id,
                    Attributes = attributeBuilder.Build(),
                    Meta = _buildMeta(builtResource.Properties, includeExtendedMeta)
                },
                Links = _buildLinks(builtResource.Links)
            });
        }
Exemple #8
0
        private static IDictionary <string, Link> _buildLinks(Links.Links links)
        {
            DictionaryBuilder <string, Link> linkBuilder = DictionaryBuilder <string, Link> .Create();

            if (links.Self != null)
            {
                linkBuilder.Add(links.Self.Name, LinkFactory.Create(links.Self));
            }

            if (links.PagingLinks != null)
            {
                if (links.PagingLinks.First != null)
                {
                    linkBuilder.Add(links.PagingLinks.First.Name, LinkFactory.Create(links.PagingLinks.First));
                }

                if (links.PagingLinks.Previous != null)
                {
                    linkBuilder.Add(links.PagingLinks.Previous.Name, LinkFactory.Create(links.PagingLinks.Previous));
                }

                if (links.PagingLinks.Next != null)
                {
                    linkBuilder.Add(links.PagingLinks.Next.Name, LinkFactory.Create(links.PagingLinks.Next));
                }

                if (links.PagingLinks.Last != null)
                {
                    linkBuilder.Add(links.PagingLinks.Last.Name, LinkFactory.Create(links.PagingLinks.Last));
                }
            }

            if (links.RelatedLinks != null)
            {
                foreach (ILink relatedLink in links.RelatedLinks)
                {
                    linkBuilder.Add(relatedLink.Name, LinkFactory.Create(relatedLink));
                }
            }

            return(linkBuilder.Build());
        }
Exemple #9
0
        private static IDictionary <string, object> _buildPropertyMeta(Property property, bool includeExtendedMeta)
        {
            DictionaryBuilder <string, object> propertyMetaBuilder = DictionaryBuilder <string, object> .Create();

            propertyMetaBuilder.Add(MetaType.Type, property.FieldType ?? FieldTypeMapper.GetFieldType(property.Type));

            if (property.Constraints != null)
            {
                if (property.Constraints.IsHidden.HasValue)
                {
                    propertyMetaBuilder.Add(MetaType.IsHidden, property.Constraints.IsHidden);
                }

                if (property.Constraints.IsReadonly.HasValue)
                {
                    propertyMetaBuilder.Add(MetaType.IsReadonly, property.Constraints.IsReadonly);
                }
            }

            if (property.ValidationConstraints != null && includeExtendedMeta)
            {
                if (property.ValidationConstraints.IsRequired.HasValue)
                {
                    propertyMetaBuilder.Add(MetaType.IsRequired, property.ValidationConstraints.IsRequired);
                }

                if (property.ValidationConstraints.MaxLength.HasValue)
                {
                    propertyMetaBuilder.Add(MetaType.MaxLength, property.ValidationConstraints.MaxLength);
                }

                if (property.ValidationConstraints.MinLenth.HasValue)
                {
                    propertyMetaBuilder.Add(MetaType.MinLenth, property.ValidationConstraints.MinLenth);
                }
            }

            return(propertyMetaBuilder.Build());
        }