/// <summary> /// Parses the request URI and message body to create a new entry in the data store. /// </summary> /// <param name="messageBody">Stream containing the new entry to create.</param> /// <returns>Stream containing the new entry if successful, otherwise an error.</returns> public Stream ProcessCreateRequest(Stream messageBody) { object result; IEdmEntitySet targetEntitySet; try { var queryContext = this.GetDefaultQueryContext(); targetEntitySet = queryContext.ResolveEntitySet(); IncomingRequestMessage message = this.GetIncomingRequestMessage(messageBody); result = ProcessPostBody(message, targetEntitySet); } catch (Exception error) { return(this.WriteErrorResponse(400, error)); } return(this.WriteResponse( 200, (writer, writerSettings, responseMessage) => { ODataVersion targetVersion = writerSettings.Version.GetValueOrDefault(); responseMessage.SetHeader("Location", ODataObjectModelConverter.BuildEntryUri(result, targetEntitySet, targetVersion).OriginalString); ResponseWriter.WriteEntry(writer.CreateODataResourceWriter(targetEntitySet), result, targetEntitySet, this.Model, targetVersion, Enumerable.Empty <string>()); })); }
/// <summary> /// Parses incoming feed/entry/property queries, resolves against the data store and formulates the response. /// </summary> /// <returns>Stream containing the query results if successful, otherwise an error.</returns> public Stream ProcessGetQuery(string requestUri) { object queryResults = null; QueryContext queryContext; try { queryContext = this.GetDefaultQueryContext(); queryResults = queryContext.ResolveQuery(this.Model, this.DataContext); } catch (Exception error) { return(this.WriteErrorResponse(400, error)); } return(this.WriteResponse(200, (messageWriter, writerSettings, message) => { IEdmEntitySet entitySet = queryContext.ResolveEntitySet(); ODataPathSegment lastSegment = queryContext.QueryPath.LastSegment; var expandedProperties = Enumerable.Empty <string>(); if (lastSegment is EntitySetSegment) { ODataWriter resultWriter = messageWriter.CreateODataFeedWriter(entitySet); ResponseWriter.WriteFeed(resultWriter, queryResults as IQueryable, entitySet, this.Model, writerSettings.Version.GetValueOrDefault(), expandedProperties); resultWriter.Flush(); } else if (lastSegment is KeySegment) { ODataWriter resultWriter = messageWriter.CreateODataEntryWriter(entitySet); ResponseWriter.WriteEntry(resultWriter, queryResults, entitySet, this.Model, writerSettings.Version.GetValueOrDefault(), expandedProperties); resultWriter.Flush(); } else if (lastSegment is PropertySegment) { ODataProperty property = ODataObjectModelConverter.CreateODataProperty(queryResults, (lastSegment as PropertySegment).Property.Name); messageWriter.WriteProperty(property); } else { throw new ODataErrorException("Unsupported URI segment " + lastSegment.GetType()); } })); }
/// <summary> /// Parses a request to update a property and makes the corresponding change to the /// data store. /// </summary> /// <param name="messageBody">Message body containing the new property value.</param> /// <param name="propertyName">The name of the property to modify.</param> /// <returns>Stream containg the updated entry if successful, otherwise an error.</returns> public Stream ProcessTopLevelPropertyRequest(Stream messageBody, string propertyName) { object result; IEdmEntitySet targetEntitySet; try { // ODL Query fails to parse EntitySet(key)/Property so we remove the property name and parse the rest of the URI. var requestUriWithoutPropertyName = new Uri(this.IncomingRequestUri.OriginalString.Replace(propertyName, string.Empty)); QueryContext queryContext = QueryContext.ParseUri(requestUriWithoutPropertyName, this.Model); targetEntitySet = queryContext.ResolveEntitySet(); var keyValues = queryContext.ResolveKeyValues(); var message = this.GetIncomingRequestMessage(messageBody); using (var reader = new ODataMessageReader(message, this.GetDefaultReaderSettings(), this.Model)) { ODataProperty property = reader.ReadProperty(); this.DataContext.UpdateItem(targetEntitySet, keyValues, property.Name, property.Value); } result = this.DataContext.GetItem(targetEntitySet, keyValues); } catch (Exception error) { return(this.WriteErrorResponse(400, error)); } return(this.WriteResponse( 200, (writer, writerSettings, responseMessage) => { var targetVersion = writerSettings.Version.GetValueOrDefault(); responseMessage.SetHeader("Location", ODataObjectModelConverter.BuildEntryUri(result, targetEntitySet, targetVersion).OriginalString); ResponseWriter.WriteEntry(writer.CreateODataResourceWriter(targetEntitySet), result, targetEntitySet, this.Model, targetVersion, Enumerable.Empty <string>()); })); }