コード例 #1
0
        public void GetResourceType__Given_WovenModel__Then_GetType()
        {
            var config = new ModelConfiguration(typeof(TestWovenModel));

            var subject = new ModelRegistry(new[] { config }, new JsonSerializerSettings());

            var result1 = subject.GetResourceType(typeof(TestWovenModel));
            var result2 = subject.GetResourceType <TestWovenModel>();

            Assert.Equal("testWovenModel", result1);
            Assert.Equal("testWovenModel", result2);
        }
コード例 #2
0
ファイル: Session.cs プロジェクト: engenb/Argo
        public async Task Delete <TModel>(Guid id)
        {
            var resourceType = ModelRegistry.GetResourceType <TModel>();
            var request      = HttpRequestBuilder.DeleteResource(resourceType, id);
            var response     = await HttpClient.SendAsync(request);

            HttpResponseListener.DeleteResource(response.StatusCode, id, resourceType);
            response.CheckStatusCode();
            var model = Cache.Retrieve <TModel>(id);

            if (model != null)
            {
                ModelRegistry.DetachModel(model);
            }
            Cache.Remove(id);
        }
コード例 #3
0
ファイル: Session.cs プロジェクト: engenb/Argo
        public Resource BuildModelResource(object model)
        {
            var resourceType = ModelRegistry.GetResourceType(model.GetType());

            // attributes
            var modelAttributes = ModelRegistry.GetAttributeValues(model);

            foreach (var attr in modelAttributes.Properties().ToArray())
            {
                if (attr.Value.Type == JTokenType.Null)
                {
                    attr.Remove();
                }
            }

            // relationships
            // Note: this process sets unset model Ids in order to create relationships
            var relationships = ModelRegistry.GetRelationshipValues(model);

            foreach (var rtln in relationships.ToList())
            {
                if (rtln.Value.Data.Type == JTokenType.Null)
                {
                    relationships.Remove(rtln);
                }
            }

            var modelMeta = ModelRegistry.GetMetaValues(model);

            foreach (var meta in modelMeta.Properties().ToArray())
            {
                if (meta.Value.Type == JTokenType.Null)
                {
                    meta.Remove();
                }
            }

            return(new Resource
            {
                Id = ModelRegistry.GetId(model),
                Type = resourceType,
                Attributes = modelAttributes.HasValues ? modelAttributes : null,
                Relationships = relationships.Any() ? relationships : null,
                Meta = modelMeta.HasValues ? modelMeta : null
            });
        }
コード例 #4
0
ファイル: Session.cs プロジェクト: engenb/Argo
        public IEnumerable <TRltn> GetRelated <TParent, TRltn>(
            Guid id,
            Expression <Func <TParent, IEnumerable <TRltn> > > relationship)
        {
            var modelType = typeof(TParent);

            if (!(relationship.Body is MemberExpression mExpression))
            {
                throw new NotSupportedException();
            }
            var attr = mExpression.Member.CustomAttributes
                       .SingleOrDefault(a => a.AttributeType == typeof(HasManyAttribute));

            if (attr == null)
            {
                throw new RelationshipNotRegisteredExecption(mExpression.Member.Name, modelType);
            }

            var resourceType = ModelRegistry.GetResourceType(typeof(TParent));
            var rltnName     = mExpression.Member.GetJsonName(typeof(HasManyAttribute));

            var request  = HttpRequestBuilder.GetRelated(id, resourceType, rltnName);
            var response = HttpClient.SendAsync(request).GetAwaiter().GetResult();

            HttpResponseListener.GetRelated(response.StatusCode, id, resourceType, rltnName);
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(Enumerable.Empty <TRltn>());
            }

            response.CheckStatusCode();

            var root    = response.GetContentModel <ResourceRootCollection>(JsonSettings).GetAwaiter().GetResult();
            var related = root.Data?
                          .Select(CreateResourceModel)
                          .Cast <TRltn>()
                          .ToArray();

            return(related);
        }
コード例 #5
0
ファイル: Session.cs プロジェクト: engenb/Argo
        public async Task <TModel> Get <TModel>(Guid id)
        {
            ThrowIfDisposed();

            var model = Cache.Retrieve <TModel>(id);

            if (model != null)
            {
                return(model);
            }

            var resourceType = ModelRegistry.GetResourceType <TModel>();
            var include      = ModelRegistry.GetInclude <TModel>();
            var request      = HttpRequestBuilder.GetResource(id, resourceType, include);
            var response     = await HttpClient.SendAsync(request).ConfigureAwait(false);

            HttpResponseListener.GetResource(response.StatusCode, id, resourceType);
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(default(TModel)); // null
            }
            response.CheckStatusCode();

            var root = await response.GetContentModel <ResourceRootSingle>(JsonSettings);

            model = CreateResourceModel <TModel>(root.Data);
            Cache.Update(id, model);
            if (root.Included != null)
            {
                await Task.WhenAll(root.Included.Select(x => Task.Run(() =>
                {
                    var includedModel = CreateResourceModel(x);
                    Cache.Update(x.Id, includedModel);
                })));
            }
            return(model);
        }