|
using System;
using System.Net;
using Newtonsoft.Json.Linq;
using Raven.Database.Data;
using Raven.Database.Indexing;
using System.Linq;
namespace Raven.Server.Responders
{
public class Index : RequestResponder
{
public override string UrlPattern
{
get { return @"/indexes/(.+)"; }
}
public override string[] SupportedVerbs
{
get { return new[] {"GET", "PUT", "DELETE"}; }
}
public override void Respond(HttpListenerContext context)
{
var match = urlMatcher.Match(context.Request.Url.LocalPath);
var index = match.Groups[1].Value;
switch (context.Request.HttpMethod)
{
case "GET":
OnGet(context, index);
break;
case "PUT":
Put(context, index);
break;
case "DELETE":
context.SetStatusToDeleted();
Database.DeleteIndex(index);
break;
}
}
private void Put(HttpListenerContext context, string index)
{
var data = context.ReadJsonObject<IndexDefinition>();
if (data.Map == null)
{
context.SetStatusToBadRequest();
context.Write("Expected json document with 'Map' property");
return;
}
context.SetStatusToCreated("/indexes/" + index);
context.WriteJson(new { index = Database.PutIndex(index, data) });
}
private void OnGet(HttpListenerContext context, string index)
{
var definition = context.Request.QueryString["definition"];
if ("yes".Equals(definition, StringComparison.InvariantCultureIgnoreCase))
{
context.WriteJson(new {index = Database.IndexDefinitionStorage.GetIndexDefinition(index)});
}
else
{
context.WriteJson(Database.Query(index, new IndexQuery
{
Query = context.Request.QueryString["query"],
Start = context.GetStart(),
PageSize = context.GetPageSize(),
FieldsToFetch = context.Request.QueryString.GetValues("fetch"),
SortedFields = context.Request.QueryString.GetValues("sort")
.EmptyIfNull()
.Select(x => new SortedField(x))
.ToArray()
}));
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.