public override OM2MResponsePrimitive DoDelete(OM2MRequestPrimitive request)
        {
            using (var db = CreateDbContext())
            {
                var query = db.Resources.OfType <OM2MAccessControlPolicyEntity>()
                            .Where(x => x.ResourceId == request.TargetId)
                            .Include(x => x.SelfPrivileges);

                if (query.Count() == 0)
                {
                    throw new OM2MNotFoundException("Resource not found.");
                }

                var entity = query.First();

                CheckSelfACP(entity, request.From, OM2MOperation.Delete);

                var subs = db.Resources.OfType <OM2MSubscriptionEntity>()
                           .Where(x => x.ParentId == entity.ResourceId).ToList();

                CseService.NotifyDeletion(subs, entity);

                db.Resources.Remove(entity);
                db.SaveChanges();

                var response = new OM2MResponsePrimitive(CseConfig, request);

                response.ResponseStatusCode = OM2MResponseStatusCode.Deleted;

                return(response);
            }
        }
示例#2
0
        public override OM2MResponsePrimitive DoDelete(OM2MRequestPrimitive request)
        {
            using (var db = CreateDbContext())
            {
                var entity = db.Resources.Find(request.TargetId) as OM2MAEEntity;

                if (entity == null)
                {
                    throw new OM2MNotFoundException("Resource not found.");
                }

                var acpList = new List <OM2MAccessControlPolicyEntity>();
                foreach (var i in entity.AccessControlPolicyIds)
                {
                    var queryAcp = db.Resources.Where(x => x.ResourceId == i)
                                   .OfType <OM2MAccessControlPolicyEntity>()
                                   .Include(x => x.PrivilegesCore);

                    if (queryAcp.Count() == 0)
                    {
                        // Damaged
                        continue;
                    }
                    acpList.Add(queryAcp.First());
                }

                CheckACP(acpList, request.From, OM2MOperation.Delete);

                db.Entry(entity).Collection(x => x.Resources).Load();
                var subs = entity.Resources.OfType <OM2MSubscriptionEntity>().ToList();
                CseService.NotifyDeletion(subs, entity);

                db.Resources.Remove(entity);

                var uriMapEntity = db.UriMaps.Find(entity.HierarchicalUri);
                db.UriMaps.Remove(uriMapEntity);

                db.SaveChanges();

                var response = new OM2MResponsePrimitive(CseConfig, request);

                response.ResponseStatusCode = OM2MResponseStatusCode.Deleted;

                return(response);
            }
        }