-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored controller and views into generic reusable class
- Loading branch information
1 parent
658371d
commit a152572
Showing
11 changed files
with
173 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace Mule.Controllers | ||
{ | ||
[Route("")] | ||
[Route("apphosts")] | ||
public class AppHostController : ItemController<AppHost> | ||
{ | ||
public AppHostController(IRepository<AppHost> repository) : base(repository) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
68 changes: 35 additions & 33 deletions
68
muleapp/Controllers/AppHostController.cs → muleapp/Controllers/ItemController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,74 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 | ||
|
||
namespace Mule.Controllers | ||
{ | ||
[Route("")] | ||
[Route("apphosts")] | ||
public class AppHostController : Controller | ||
public abstract class ItemController<T> : Controller where T : class | ||
{ | ||
readonly IRepository<AppHost> AppHosts; | ||
public AppHostController(IRepository<AppHost> apphosts) | ||
{ | ||
AppHosts = apphosts; | ||
} | ||
readonly IRepository<T> Items; | ||
|
||
/// <summary> | ||
/// Called before each request | ||
/// </summary> | ||
/// <param name="context"></param> | ||
public override void OnActionExecuting(ActionExecutingContext context) | ||
public ItemController(IRepository<T> repository) | ||
{ | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
ViewData["Title"] = typeof(AppHost).Name; | ||
ViewData["Ponies"] = new Random().NextDouble() > 0.9; | ||
ViewData["Stopwatch"] = sw; | ||
base.OnActionExecuting(context); | ||
Items = repository; | ||
} | ||
|
||
[Route("")] | ||
[Route("index")] | ||
public IActionResult Index() | ||
{ | ||
return View(AppHosts.Read()); | ||
return View("Items/Index",Items.Read()); | ||
} | ||
|
||
[HttpPost] | ||
[Route("create")] | ||
public IActionResult Create([Bind] AppHost item) | ||
public IActionResult Create([Bind] T item) | ||
{ | ||
var existing = (from x in AppHosts.Read() | ||
var existing = (from x in Items.Read() | ||
where x.Equals(item) | ||
select x).FirstOrDefault(); | ||
if (existing != null) | ||
{ | ||
AppHosts.Update(existing, item); | ||
Items.Update(existing, item); | ||
} | ||
else | ||
{ | ||
AppHosts.Create(item); | ||
Items.Create(item); | ||
} | ||
return RedirectToAction("Index"); | ||
} | ||
|
||
[HttpPost] | ||
[Route("delete")] | ||
public IActionResult Delete([Bind] AppHost item) | ||
public IActionResult Delete([Bind] T item) | ||
{ | ||
var existing = from x in AppHosts.Read() | ||
where x.Equals(item) | ||
select x; | ||
foreach(var i in existing) | ||
var existing = from x in Items.Read() | ||
where x.Equals(item) | ||
select x; | ||
foreach (var i in existing) | ||
{ | ||
AppHosts.Delete(i); | ||
Items.Delete(i); | ||
} | ||
return RedirectToAction("Index"); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Called before each request | ||
/// </summary> | ||
/// <param name="context"></param> | ||
public override void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
ViewData["Title"] = typeof(AppHost).Name; | ||
ViewData["Ponies"] = new Random().NextDouble() > 0.9; | ||
ViewData["Stopwatch"] = sw; | ||
base.OnActionExecuting(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.