Skip to content

Commit

Permalink
Committing course code
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Sep 30, 2012
0 parents commit a17c083
Show file tree
Hide file tree
Showing 25 changed files with 986 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
@@ -0,0 +1,30 @@
[Bb]in
[Oo]bj
*resharper.user
build/
[Hh]elp/
*.suo
*.sln.cache
_ReSharper.*/
*.user
/Data/Data
/Data/IndexDefinitions/
/Data/Index/
/Data/logs/
/Data/system/
/Data/temp/
*.InstallLog
/test/storage.raven
/Raven.Web/Data/
VERSION
/packages/
ClientBin/
/Raven.Tests.Silverlight.Results.xml
*.g.3.5.csproj
*.g.csproj
*.g.4.csproj
*.docstates
StyleCop.Cache
*.dotCover
RavenDB*.nupkg
*.orig
6 changes: 6 additions & 0 deletions ConsoleApplication1/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
61 changes: 61 additions & 0 deletions ConsoleApplication1/ConsoleApplication1.csproj
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3896CA4A-D4C7-4A90-A7E9-5EC15186FCA4}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleApplication1</RootNamespace>
<AssemblyName>ConsoleApplication1</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Raven.Client.Lightweight">
<HintPath>..\packages\RavenDB.Client.1.2.2099-Unstable\lib\net40\Raven.Client.Lightweight.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
59 changes: 59 additions & 0 deletions ConsoleApplication1/Program.cs
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Shard;
using Raven.Client.Linq;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var shards = new Dictionary<string, IDocumentStore>
{
{ "one", new DocumentStore { Url = "http://localhost:8079", } },
{ "two", new DocumentStore { Url = "http://localhost:8078", } },
};

var shardStrategy = new ShardStrategy(shards)
.ShardingOn<User>()
.ShardingOn<Story>(x => x.UserId);

using (var store = new ShardedDocumentStore(shardStrategy).Initialize())
{
//using (var session = store.OpenSession())
//{
// var user = new User { Name = "Ayende" };
// session.Store(user);
// session.Store(new Story { UserId = user.Id });
// session.SaveChanges();
//}


using (var session = store.OpenSession())
{
var load = session.Query<Story>()
.Where(x => x.UserId == "two/users/1")
.ToList();

Console.WriteLine(load[0].UserId);
}
}
}
}

public class User
{
public string Id { get; set; }
public string Name { get; set; }
}

public class Story
{
public string UserId { get; set; }
}
}
36 changes: 36 additions & 0 deletions ConsoleApplication1/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ConsoleApplication1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConsoleApplication1")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c8163b5f-9769-478d-b4dd-f25e0e4b5ace")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
26 changes: 26 additions & 0 deletions WebStep.sln
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebStep", "WebStep\WebStep.csproj", "{A043D695-5389-4204-84F5-8E409ED9249D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.csproj", "{3896CA4A-D4C7-4A90-A7E9-5EC15186FCA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A043D695-5389-4204-84F5-8E409ED9249D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A043D695-5389-4204-84F5-8E409ED9249D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A043D695-5389-4204-84F5-8E409ED9249D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A043D695-5389-4204-84F5-8E409ED9249D}.Release|Any CPU.Build.0 = Release|Any CPU
{3896CA4A-D4C7-4A90-A7E9-5EC15186FCA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3896CA4A-D4C7-4A90-A7E9-5EC15186FCA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3896CA4A-D4C7-4A90-A7E9-5EC15186FCA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3896CA4A-D4C7-4A90-A7E9-5EC15186FCA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions WebStep/App_Start/FilterConfig.cs
@@ -0,0 +1,13 @@
using System.Web;
using System.Web.Mvc;

namespace WebStep
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}
23 changes: 23 additions & 0 deletions WebStep/App_Start/RouteConfig.cs
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebStep
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
19 changes: 19 additions & 0 deletions WebStep/App_Start/WebApiConfig.cs
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace WebStep
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
44 changes: 44 additions & 0 deletions WebStep/Controllers/CompaniesController.cs
@@ -0,0 +1,44 @@
using WebStep.Models;
using Raven.Client;
using System.Linq;

namespace WebStep.Controllers
{
public class CompaniesController : RavenController
{
public object Search(string q)
{
var query = Session.Query<Company>("companies")
.Search(x => x.Name, q);
var r = query
.ToList();

if(r.Count == 0)
{
var suggest = query.Suggest();
switch (suggest.Suggestions.Length)
{
case 0:
return Json("Could not find any matches");
case 1:
return Search(suggest.Suggestions[0]);
default:
return Json(new
{
DidYouMean = suggest.Suggestions
});
}
}

return Json(new
{
Results = r.Select(x=>new
{
x.Name,
x.Id,
Score = Session.Advanced.GetMetadataFor(x).Value<string>("Temp-Index-Score")
})
});
}
}
}
63 changes: 63 additions & 0 deletions WebStep/Controllers/RavenController.cs
@@ -0,0 +1,63 @@
using System.Net;
using System.Web.Mvc;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Indexes;
using WebStep.Indexes;

namespace WebStep.Controllers
{
public class RavenController : Controller
{
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return base.Json(data, contentType, contentEncoding, JsonRequestBehavior.AllowGet);
}

private static IDocumentStore documentStore;

public static IDocumentStore DocumentStore
{
get
{
if (documentStore == null)
{
lock (typeof(RavenController))
{
if (documentStore != null)
return documentStore;
var store = new DocumentStore
{
ConnectionStringName = "RavenDB",
};

store.RegisterListener(new AuditListener());

documentStore = store.Initialize();
IndexCreation.CreateIndexesAsync(typeof(RavenController).Assembly, documentStore);
}
}
return documentStore;
}
}

public new IDocumentSession Session { get; set; }

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
Session = DocumentStore.OpenSession();
}

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
using (Session)
{
if (Session == null)
return;
if (filterContext.Exception != null)
return;
Session.SaveChanges();
}
}
}
}

0 comments on commit a17c083

Please sign in to comment.