示例#1
0
        public ThingProfile()
        {
            CreateMap <Entities.IThing, Model.IThing>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => ThingIdHelper.Create(src.Fqdn, src.US, true)))
            ;
            CreateMap <Model.IThing, Entities.IThing>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.Fqdn, opt => opt.MapFrom(src => src.Id != null ? ThingIdHelper.GetFQDN(src.Id) : null))
            .ForMember(dest => dest.US, opt => opt.MapFrom(src => src.Id != null ? ThingIdHelper.GetUniqueString(src.Id) : null))
            ;

            CreateMap <Model.BaseThing, Entities.BaseThing>()
            .IncludeBase <Model.IThing, Entities.IThing>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            ;

            CreateMap <Entities.BaseThing, Model.BaseThing>()
            .IncludeBase <Entities.IThing, Model.IThing>()
            ;

            CreateMap <Entities.RegularThing, Model.RegularThing>()
            .IncludeBase <Entities.IThing, Model.IThing>()
            ;

            CreateMap <Model.RegularThing, Entities.RegularThing>()
            .IncludeBase <Model.IThing, Entities.IThing>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            ;
        }
示例#2
0
        public IActionResult GetRelations([FromBody] GetRelationsRequest value)
        {
            var session = this.GetSession(value.Session, true);

            T2D.Entities.BaseThing thing =
                this.Find(value.ThingId)
                .Include(t => t.ThingAttributes)
                .Include(t => t.ThingRelations)
                .FirstOrDefault()
            ;

            if (thing == null)
            {
                return(BadRequest($"Thing '{value.ThingId}' do not exists."));
            }

            var role = this.RoleMapper.EnumToEntity(value.Role);

            //TODO: check that session has right to
            if (!AttributeSecurity.QueryRelationsRight(thing, session, role))
            {
                return(BadRequest($"Not enough priviledges to query relations for thing {value.ThingId}."));
            }


            var ret = new GetRelationsResponse();

            ret.RelationThings = new List <GetRelationsResponse.RelationsThings>();

            foreach (var group in thing.ThingRelations.GroupBy(tr => tr.RelationId))
            {
                var rt = new GetRelationsResponse.RelationsThings
                {
                    Relation = RelationMapper.FromEntityId(group.Key).ToString(),
                    Things   = new List <GetRelationsResponse.RelationsThings.IdTitle>(),
                };
                foreach (var th in group)
                {
                    var thingIdTitle = new GetRelationsResponse.RelationsThings.IdTitle {
                        ThingId = ThingIdHelper.Create(th.Thing2_Fqdn, th.Thing2_US)
                    };
                    var thing2 = this.Find(th.Thing2_Fqdn, th.Thing2_US);
                    if (thing2 != null)
                    {
                        thingIdTitle.Title = thing2.Title;
                    }
                    rt.Things.Add(thingIdTitle);
                }
                ret.RelationThings.Add(rt);
            }
            return(Ok(ret));
        }