Add shared (collaborative) samples

This commit is contained in:
Frank A. Krueger 2017-11-15 23:03:36 -06:00
parent a43508253e
commit 813b48bbdb
3 changed files with 23 additions and 4 deletions

View File

@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Mvc;
using AspNetCoreMvc.Models; using AspNetCoreMvc.Models;
using Ooui; using Ooui;
using Ooui.AspNetCore; using Ooui.AspNetCore;
using Samples;
using System.Collections.Concurrent;
namespace AspNetCoreMvc.Controllers namespace AspNetCoreMvc.Controllers
{ {
@ -43,10 +45,13 @@ namespace AspNetCoreMvc.Controllers
return samples.ToList (); return samples.ToList ();
}), true); }), true);
static readonly ConcurrentDictionary<string, Element> sharedSamples =
new ConcurrentDictionary<string, Element> ();
public static List<Samples.ISample> Samples => lazySamples.Value; public static List<Samples.ISample> Samples => lazySamples.Value;
[Route("/Samples/Run/{name}")] [Route("/Samples/Run/{name}")]
public IActionResult Run (string name) public IActionResult Run (string name, bool shared)
{ {
if (string.IsNullOrWhiteSpace (name) || name.Length > 32) if (string.IsNullOrWhiteSpace (name) || name.Length > 32)
return BadRequest (); return BadRequest ();
@ -55,7 +60,18 @@ namespace AspNetCoreMvc.Controllers
if (s == null) if (s == null)
return NotFound (); return NotFound ();
return new ElementResult (s.CreateElement (), title: s.Title + " - Ooui Samples"); var element = shared ? GetSharedSample (s) : s.CreateElement ();
return new ElementResult (element, title: s.Title + " - Ooui Samples");
}
private Element GetSharedSample (ISample s)
{
if (sharedSamples.TryGetValue (s.Title, out var e))
return e;
e = s.CreateElement ();
sharedSamples[s.Title] = e;
return e;
} }
} }
} }

View File

@ -17,7 +17,10 @@
<h3>Samples</h3> <h3>Samples</h3>
<ul> <ul>
@foreach (var s in SamplesController.Samples) { @foreach (var s in SamplesController.Samples) {
<li><a asp-area="" asp-controller="Samples" asp-action="Run" asp-route-name="@s.Title">@s.Title</a></li> <li>
<a asp-area="" asp-controller="Samples" asp-action="Run" asp-route-name="@s.Title">@s.Title</a>
(<a asp-area="" asp-controller="Samples" asp-action="Run" asp-route-name="@s.Title" asp-route-shared="@true">Shared</a>)
</li>
} }
</ul> </ul>
</div> </div>

View File

@ -17,7 +17,7 @@ namespace Samples
public Element CreateElement () public Element CreateElement ()
{ {
var heading = new Heading ("Draw"); var heading = new Heading ("Draw");
var subtitle = new Paragraph ("Click to draw a collaborative masterpiece"); var subtitle = new Paragraph ("Click to draw a masterpiece");
var canvas = new Canvas { var canvas = new Canvas {
Width = 320, Width = 320,
Height = 240, Height = 240,