From 813b48bbdbfefda0cc8655a203571debaef75b35 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Wed, 15 Nov 2017 23:03:36 -0600 Subject: [PATCH] Add shared (collaborative) samples --- .../Controllers/SamplesController.cs | 20 +++++++++++++++++-- .../AspNetCoreMvc/Views/Home/Index.cshtml | 5 ++++- Samples/DrawSample.cs | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/PlatformSamples/AspNetCoreMvc/Controllers/SamplesController.cs b/PlatformSamples/AspNetCoreMvc/Controllers/SamplesController.cs index fc46088..8fdb508 100644 --- a/PlatformSamples/AspNetCoreMvc/Controllers/SamplesController.cs +++ b/PlatformSamples/AspNetCoreMvc/Controllers/SamplesController.cs @@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Mvc; using AspNetCoreMvc.Models; using Ooui; using Ooui.AspNetCore; +using Samples; +using System.Collections.Concurrent; namespace AspNetCoreMvc.Controllers { @@ -43,10 +45,13 @@ namespace AspNetCoreMvc.Controllers return samples.ToList (); }), true); + static readonly ConcurrentDictionary sharedSamples = + new ConcurrentDictionary (); + public static List Samples => lazySamples.Value; [Route("/Samples/Run/{name}")] - public IActionResult Run (string name) + public IActionResult Run (string name, bool shared) { if (string.IsNullOrWhiteSpace (name) || name.Length > 32) return BadRequest (); @@ -55,7 +60,18 @@ namespace AspNetCoreMvc.Controllers if (s == null) 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; } } } diff --git a/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml b/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml index d7f81f0..6fb9f7f 100644 --- a/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml +++ b/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml @@ -17,7 +17,10 @@

Samples

diff --git a/Samples/DrawSample.cs b/Samples/DrawSample.cs index 5e70226..4adead0 100644 --- a/Samples/DrawSample.cs +++ b/Samples/DrawSample.cs @@ -17,7 +17,7 @@ namespace Samples public Element CreateElement () { 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 { Width = 320, Height = 240,