Ejemplo n.º 1
0
        private Stream ProcessDelete()
        {
            if (!this.IsAllowDelete())
            {
                throw new ODataServiceException(HttpStatusCode.BadRequest, "The delete request is not allowed.", null);
            }

            try
            {
                using (DeletionContext.Create())
                {
                    // handle entity
                    var entity = this.HandleEntity();

                    // handle ETag
                    this.HandleETag(entity, false);

                    // delete stream
                    this.DataSource.StreamProvider.DeleteStream(entity);

                    // delete entity
                    this.DataSource.UpdateProvider.Delete(entity);

                    this.DataSource.UpdateProvider.SaveChanges();

                    return(this.HandleResponse(entity, HttpStatusCode.NoContent, false, false));
                }
            }
            catch (Exception)
            {
                this.DataSource.UpdateProvider.ClearChanges();
                throw;
            }
        }
Ejemplo n.º 2
0
        public override void Process(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage)
        {
            if (!this.IsAllowDelete())
            {
                throw new ODataServiceException(HttpStatusCode.BadRequest, "The delete request is not allowed.", null);
            }

            if (this.QueryContext.QueryPath.LastSegment is NavigationPropertyLinkSegment)
            {
                this.ProcessDeleteLink(responseMessage);
                return;
            }

            try
            {
                using (DeletionContext.Create())
                {
                    var targetObject = this.QueryContext.ResolveQuery(this.DataSource);

                    if (targetObject == null)
                    {
                        throw Utility.BuildException(HttpStatusCode.NotFound);
                    }

                    var targetETag = Utility.GetETagValue(targetObject);

                    if (targetETag != null)
                    {
                        string requestETag;
                        if (Utility.TryGetIfMatch(this.RequestHeaders, out requestETag))
                        {
                            if (requestETag == ServiceConstants.ETagValueAsterisk || requestETag == targetETag)
                            {
                                ProcessDelete(targetObject, responseMessage);
                            }
                            else
                            {
                                ProcessPreconditionFailed(responseMessage);
                            }
                        }
                        else if (Utility.TryGetIfNoneMatch(this.RequestHeaders, out requestETag))
                        {
                            if (requestETag == ServiceConstants.ETagValueAsterisk || requestETag == targetETag)
                            {
                                ProcessPreconditionFailed(responseMessage);
                            }
                            else
                            {
                                ProcessDelete(targetObject, responseMessage);
                            }
                        }
                        else
                        {
                            ProcessPreconditionRequired(responseMessage);
                        }
                    }
                    else
                    {
                        ProcessDelete(targetObject, responseMessage);
                    }

                    this.DataSource.UpdateProvider.SaveChanges();
                }
            }
            catch
            {
                this.DataSource.UpdateProvider.ClearChanges();
                throw;
            }
        }