Skip to content

Commit

Permalink
MVC example completed
Browse files Browse the repository at this point in the history
  • Loading branch information
dasiths committed Nov 20, 2016
1 parent 40320ed commit a033957
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void RegisterRoutes(RouteCollection routes)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
defaults: new { controller = "NoteDto", action = "Index", id = UrlParameter.Optional }
);
}
}
Expand Down
30 changes: 0 additions & 30 deletions src/Examples/NEventLite Example MVC/Controllers/HomeController.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using NEventLite.Command_Bus;
using NEventLite_Example.Commands;
using NEventLite_Example.Read_Model;
using NEventLite_Example_MVC.Models;
using WebGrease.Css.Ast;

namespace NEventLite_Example_MVC.Controllers
{
public class NoteDtoController : Controller
{
// GET: NoteDto
public ActionResult Index()
{
return View(GetReadRepository().GetAllNotes().OrderBy(o=> o.CreatedDate).Select(o => new NoteDto(o)));
}

// GET: NoteDto/Create
public ActionResult Create()
{
return View();
}

// POST: NoteDto/Create
[HttpPost]
public async Task<ActionResult> Create(FormCollection collection)
{
try
{
if (ModelState.IsValid)
{
await GetCommandBus().ExecuteAsync(
new CreateNoteCommand(Guid.NewGuid(), Guid.NewGuid(), -1, collection["Title"],
collection["Description"], collection["Category"]));


}

return RedirectToAction("Index");
}
catch
{
return View();
}
}

// GET: NoteDto/Edit/5
public ActionResult Edit(Guid id)
{
var note = new NoteDto(GetReadRepository().GetNote(id));

return View(note);
}

// POST: NoteDto/Edit/5
[HttpPost]
public async Task<ActionResult> Edit(Guid id, int CurrentVersion, FormCollection collection)
{
try
{
if (ModelState.IsValid)
{
await GetCommandBus().ExecuteAsync(
new EditNoteCommand(Guid.NewGuid(), id, CurrentVersion, collection["Title"],
collection["Description"], collection["Category"]));


}

return RedirectToAction("Index");
}
catch
{
return View();
}
}

private MyReadRepository GetReadRepository()
{
return ((MvcApplication)this.HttpContext.ApplicationInstance).GetDependencyResolver().Resolve<MyReadRepository>();
}

private ICommandBus GetCommandBus()
{
return ((MvcApplication)this.HttpContext.ApplicationInstance).GetDependencyResolver().Resolve<ICommandBus>();
}

}
}
33 changes: 33 additions & 0 deletions src/Examples/NEventLite Example MVC/Models/NoteDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NEventLite_Example.Read_Model;

namespace NEventLite_Example_MVC.Models
{
public class NoteDto
{
public Guid Id { get; private set; }
public int CurrentVersion { get; set; }
public DateTime CreatedDate { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Category { get; set; }

public NoteDto(Guid id, DateTime createdDate, string title, string description, string category)
{
Id = id;
CreatedDate = createdDate;
Title = title;
Description = description;
Category = category;
CurrentVersion = 0;
}

public NoteDto(NoteReadModel note) : this(note.Id, note.CreatedDate, note.Title, note.Description, note.Category)
{
this.CurrentVersion = note.CurrentVersion;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\NoteDtoController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\NoteDto.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -158,13 +159,12 @@
<Content Include="Views\_ViewStart.cshtml" />
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\Home\About.cshtml" />
<Content Include="Views\Home\Contact.cshtml" />
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\NoteDto\Index.cshtml" />
<Content Include="Views\NoteDto\Create.cshtml" />
<Content Include="Views\NoteDto\Edit.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<Content Include="fonts\glyphicons-halflings-regular.woff" />
Expand Down
7 changes: 0 additions & 7 deletions src/Examples/NEventLite Example MVC/Views/Home/About.cshtml

This file was deleted.

17 changes: 0 additions & 17 deletions src/Examples/NEventLite Example MVC/Views/Home/Contact.cshtml

This file was deleted.

31 changes: 0 additions & 31 deletions src/Examples/NEventLite Example MVC/Views/Home/Index.cshtml

This file was deleted.

57 changes: 57 additions & 0 deletions src/Examples/NEventLite Example MVC/Views/NoteDto/Create.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@model NEventLite_Example_MVC.Models.NoteDto

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>NoteDto</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
60 changes: 60 additions & 0 deletions src/Examples/NEventLite Example MVC/Views/NoteDto/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@model NEventLite_Example_MVC.Models.NoteDto

@{
ViewBag.Title = "View";
}

<h2>View</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>NoteDto</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.CurrentVersion)


<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Loading

0 comments on commit a033957

Please sign in to comment.