Skip to content

Commit

Permalink
Merge branch 'edit_survey'
Browse files Browse the repository at this point in the history
  • Loading branch information
oharab committed Aug 14, 2011
2 parents caa04cb + 1b00644 commit 166321a
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/Core/Repositories/ISurveyRepository.cs
Expand Up @@ -9,5 +9,6 @@ public interface ISurveyRepository
Survey GetSurvey(int Id);
IList<Survey> ListSurveys();
Survey Create(string name, string title, string description);
void Update(Survey survey);
}
}
Expand Up @@ -15,14 +15,14 @@ public class FluentConfigurationBuilder : IConfigurationBuilder
{
public global::NHibernate.Cfg.Configuration GetConfiguration(IConfiguration config)
{

var defaultConfigurationBuilder = new DefaultConfigurationBuilder();
var configuration = defaultConfigurationBuilder.GetConfiguration(config);
Fluently.Configure(configuration)
.Database(
SQLiteConfiguration
.Standard
.ConnectionString(c => c.FromConnectionStringWithKey("OpenSurveyConnectionString"))
.ShowSql()
)
.Mappings(
m => m.AutoMappings.Add(
Expand Down
Expand Up @@ -5,16 +5,19 @@
using Castle.Facilities.NHibernateIntegration;
using OpenSurvey.Core.Repositories;
using OpenSurvey.Persistence.NHibernate.Repositories;
using Castle.Facilities.AutoTx;

public class NHibernateInstaller : IWindsorInstaller
{
public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
container
.AddFacility<TransactionFacility>("autoTxFacility")
.AddFacility<NHibernateFacility>("nhibnernateFacility", cfg => cfg.ConfigurationBuilder<FluentConfigurationBuilder>())
.Register(
Component.For<ISurveyRepository>()
.ImplementedBy<NHibernateSurveyRepository>()
.LifeStyle.PerWebRequest
)
;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Persistence.NHibernate/Persistence.NHibernate.csproj
Expand Up @@ -31,10 +31,17 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Facilities.AutoTx">
<HintPath>..\..\libs\Castle.Facilities.NHibernateIntegration\Castle.Facilities.AutoTx.dll</HintPath>
</Reference>
<Reference Include="Castle.Facilities.NHibernateIntegration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\libs\Castle.Facilities.NHibernateIntegration\Castle.Facilities.NHibernateIntegration.dll</HintPath>
</Reference>
<Reference Include="Castle.Services.Transaction, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\libs\Castle.Facilities.NHibernateIntegration\Castle.Services.Transaction.dll</HintPath>
</Reference>
<Reference Include="FluentNHibernate">
<HintPath>..\..\libs\FluentNHibernate\FluentNHibernate.dll</HintPath>
</Reference>
Expand Down
Expand Up @@ -8,11 +8,13 @@
using OpenSurvey.Core.Model;
using Castle.Facilities.NHibernateIntegration;
using global::NHibernate;

using Castle.Services.Transaction;


public class NHibernateSurveyRepository : ISurveyRepository
{
private readonly ISessionManager sessionManager;

public NHibernateSurveyRepository(ISessionManager sessionManager)
{
this.sessionManager = sessionManager;
Expand All @@ -22,9 +24,11 @@ public IList<Survey> ListSurveys()
{
using (ISession session = sessionManager.OpenSession())
{

return session.CreateCriteria<Survey>()
.List<Survey>();
}

}

public Survey GetSurvey(int id)
Expand All @@ -39,14 +43,22 @@ public Survey GetSurvey(int id)
public Survey Create(string name, string title, string description)
{
using (ISession session = sessionManager.OpenSession())
using (var t=session.BeginTransaction())
{
var s = new Survey { Name = name, Title = title, Description = description };
session.SaveOrUpdate(s);
t.Commit();
return s;
}
}

public void Update(Survey survey)
{
using (ISession session = sessionManager.OpenSession())
using(var t=session.BeginTransaction())
{
using (session.BeginTransaction())
{
var s = new Survey { Name = name, Title = title, Description = description };
session.SaveOrUpdate(s);
session.Transaction.Commit();
return s;
}
session.SaveOrUpdate(survey);
t.Commit();
}
}
}
Expand Down
Binary file modified src/Web/App_Data/MySQLite.db
Binary file not shown.
45 changes: 8 additions & 37 deletions src/Web/Configuration.cs
Expand Up @@ -4,18 +4,16 @@
using OpenRasta.Configuration;
using OpenSurvey.Web.Resources;
using OpenSurvey.Web.Handlers;
using Castle.Windsor;
using OpenRasta.DI;
using OpenRasta.DI.Windsor;
using OpenSurvey.Persistence.NHibernate.Configuration;
using Castle.MicroKernel.Registration;
using OpenRasta.Web.UriDecorators;


public class Configuration : IConfigurationSource, IDependencyResolverAccessor
public class Configuration : IConfigurationSource
{
public void Configure()
{
using (OpenRastaConfiguration.Manual)
{
ResourceSpace.Uses.UriDecorator<HttpMethodOverrideUriDecorator>();
ResourceSpace.Has
.ResourcesOfType<HomeResource>()
.AtUri("/home")
Expand All @@ -28,45 +26,18 @@ public void Configure()
.AtUri("/survey/new")
.HandledBy<NewSurveyHandler>()
.RenderedByAspx("~/Views/AddSurvey.aspx");

ResourceSpace.Has
.ResourcesOfType<SurveyResource>()
.AtUri("/survey/{id}")
.And.AtUri("/survey/{id}/edit").Named("edit")
.HandledBy<SurveyHandler>()
.RenderedByAspx(new
{
index="~/Views/ShowSurvey.aspx"
index = "~/Views/ShowSurvey.aspx",
edit = "~/Views/EditSurvey.aspx"
});
}
}

IWindsorContainer container;
public IWindsorContainer Container
{
get
{
if (container == null)
container = ConfigureContainer();
return container;
}
}

private IWindsorContainer ConfigureContainer()
{
container = new WindsorContainer()
.Install(new NHibernateInstaller())
.Register(AllTypes.FromThisAssembly()
.Where(t=>t.Namespace.EndsWith(".Resources"))
)

;

return container;
}

public IDependencyResolver Resolver
{
get { return new WindsorDependencyResolver(Container); }
}
}
}
1 change: 1 addition & 0 deletions src/Web/Global.asax
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="OpenSurvey.Web.Global" Language="C#" %>
43 changes: 43 additions & 0 deletions src/Web/Global.asax.cs
@@ -0,0 +1,43 @@
namespace OpenSurvey.Web
{
using System;
using Castle.Windsor;
using OpenRasta.DI;
using Castle.Facilities.Logging;
using Castle.Services.Logging.Log4netIntegration;
using OpenSurvey.Persistence.NHibernate.Configuration;
using Castle.MicroKernel.Registration;
using OpenRasta.DI.Windsor;

public class Global : System.Web.HttpApplication, IContainerAccessor, IDependencyResolverAccessor
{

IWindsorContainer container;
public IWindsorContainer Container
{
get
{
if (container == null)
container = ConfigureContainer();
return container;
}
}

private IWindsorContainer ConfigureContainer()
{
container = new WindsorContainer()
.AddFacility<LoggingFacility>("loggingFacility", f => f.LogUsing<Log4netFactory>().WithAppConfig())
.Install(new NHibernateInstaller())
.Register(AllTypes.FromThisAssembly()
.Where(t => t.Namespace.EndsWith(".Resources"))
)
;
return container;
}

public IDependencyResolver Resolver
{
get { return new WindsorDependencyResolver(Container); }
}
}
}
16 changes: 14 additions & 2 deletions src/Web/Handlers/SurveyHandler.cs
Expand Up @@ -14,12 +14,24 @@ public SurveyHandler(ISurveyRepository repository)
this.repository = repository;
}

public SurveyResource Get(int id)
public OperationResult Get(int id)
{
var s = repository.GetSurvey(id);
return new SurveyResource { Name = s.Name, Title = s.Title, Description = s.Description };
if (s == null)
return new OperationResult.NotFound();
return new OperationResult.OK( new SurveyResource { Id=s.Id, Name = s.Name, Title = s.Title, Description = s.Description });
}

public OperationResult Put(SurveyResource resource)
{
var s = repository.GetSurvey(resource.Id);
s.Name = resource.Name;
s.Title = resource.Title;
s.Description = resource.Description;
repository.Update(s);

return new OperationResult.SeeOther { RedirectLocation = resource.CreateUri() };
}

}
}
25 changes: 25 additions & 0 deletions src/Web/Views/EditSurvey.aspx
@@ -0,0 +1,25 @@
<%@ Page Language="C#" Inherits="ResourceView<SurveyResource>" MasterPageFile="~/Views/Survey.Master"
Title="Home Page" %>

<script runat="server">
protected void Page_Load()
{
Page.Title = Resource.Title;
}
</script>
<asp:Content ContentPlaceHolderID="Content" ID="content" runat="server">
<div>
<% using (
scope(Xhtml.Form(Resource).Method("put")))
{
using (scope(p))
{
%>
<input type="text" name="Name" value="<%= Resource.Name %>" />
<input type="text" name="Title" value="<%= Resource.Title %>" />
<textarea name="Description" rows="4" cols="20"><%= Resource.Description %></textarea>
<input type="submit" value="Save" /> <a href="<%= Resource.CreateUri() %>">Cancel</a>
<% }
} %>
</div>
</asp:Content>
9 changes: 7 additions & 2 deletions src/Web/Views/ShowSurvey.aspx
@@ -1,10 +1,15 @@
<%@ Page Language="C#" Inherits="ResourceView<SurveyResource>" MasterPageFile="~/Views/Survey.Master" Title="Home Page" %>

<script runat="server">
protected void Page_Load()
{
Page.Title = Resource.Title;
}
</script>
<asp:Content ContentPlaceHolderID="Content" ID="content" runat="server">
<div>
<h1><%= Resource.Title %></h1>
<p><%= Resource.Description %></p>

<a href="<%= Resource.CreateUri() %>">Permalink</a>
<a href="<%= Resource.CreateUri("edit") %>">Edit</a>
</div>
</asp:Content>
31 changes: 31 additions & 0 deletions src/Web/Web.config
@@ -1,5 +1,34 @@
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net debug="true">
<appender name="consoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="OutputDebugStringAppender" />
</root>

<logger name="NHibernate">
<level value="DEBUG" />
</logger>

<logger name="NHibernate.SQL">
<level value="DEBUG" />
</logger>

</log4net>
<connectionStrings>
<add name="OpenSurveyConnectionString" connectionString="data source=|DataDirectory|MySQLite.db;" providerName="System.Data.EntityClient" />
</connectionStrings>
Expand All @@ -19,6 +48,8 @@
<add verb="*" path="*.rastahook" type="OpenRasta.Hosting.AspNet.OpenRastaHandler, OpenRasta.Hosting.AspNet"/>
</httpHandlers>
<httpModules>
<add name="NHibernate Session" type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule, Castle.Facilities.NHibernateIntegration" />
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
<add name="RastaModule" type="OpenRasta.Hosting.AspNet.OpenRastaModule, OpenRasta.Hosting.AspNet"/>
</httpModules>
</system.web>
Expand Down
11 changes: 11 additions & 0 deletions src/Web/Web.csproj
Expand Up @@ -33,6 +33,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Facilities.Logging">
<HintPath>..\..\libs\Castle.Facilities.NHibernateIntegration\Castle.Facilities.Logging.dll</HintPath>
</Reference>
<Reference Include="Castle.Services.Logging.Log4netIntegration">
<HintPath>..\..\libs\Castle.Facilities.NHibernateIntegration\Castle.Services.Logging.Log4netIntegration.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
Expand All @@ -53,7 +59,9 @@
<ItemGroup>
<Content Include="Content\style.css" />
<Content Include="default.aspx" />
<Content Include="Global.asax" />
<Content Include="Views\AddSurvey.aspx" />
<Content Include="Views\EditSurvey.aspx" />
<Content Include="Views\ShowSurvey.aspx" />
<Content Include="Views\HomeView.aspx" />
<Content Include="Web.config" />
Expand All @@ -69,6 +77,9 @@
<Link>Properties\CommonInfo.cs</Link>
</Compile>
<Compile Include="Configuration.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Handlers\HomeHandler.cs" />
<Compile Include="Handlers\NewSurveyHandler.cs" />
<Compile Include="Handlers\SurveyHandler.cs" />
Expand Down

0 comments on commit 166321a

Please sign in to comment.