From 84ad51b9b07cc0f58743be785113a469cea9c831 Mon Sep 17 00:00:00 2001 From: Kenny Young Date: Tue, 28 Aug 2018 20:19:47 -0700 Subject: [PATCH 1/2] Event-based Puzzles Making puzzles be owned by events, as a way to play with object relationships. The "CLICK HERE TO CHECK YOUR DATABASE SETUP" link now points to Events instead of Puzzles, and the Events index has a new link to access that event's puzzle list. I've needed to pass the event ID around, and the event ID should probably be part of some global context, but this is a helpful microcosm of issues we'll have when doing other features so I still think it's a good start. --- ServerCore/Pages/Events/Index.cshtml | 11 ++++++++++- ServerCore/Pages/Index.cshtml | 2 +- ServerCore/Pages/Puzzles/Create.cshtml.cs | 9 +++++++-- ServerCore/Pages/Puzzles/Delete.cshtml | 2 +- ServerCore/Pages/Puzzles/Delete.cshtml.cs | 6 +++--- ServerCore/Pages/Puzzles/Details.cshtml | 2 +- ServerCore/Pages/Puzzles/Details.cshtml.cs | 2 +- ServerCore/Pages/Puzzles/Edit.cshtml | 2 +- ServerCore/Pages/Puzzles/Edit.cshtml.cs | 4 ++-- ServerCore/Pages/Puzzles/Index.cshtml | 2 +- ServerCore/Pages/Puzzles/Index.cshtml.cs | 16 +++++++++++++--- 11 files changed, 41 insertions(+), 17 deletions(-) diff --git a/ServerCore/Pages/Events/Index.cshtml b/ServerCore/Pages/Events/Index.cshtml index ab825f0c..a47b37ee 100644 --- a/ServerCore/Pages/Events/Index.cshtml +++ b/ServerCore/Pages/Events/Index.cshtml @@ -19,6 +19,7 @@ @Html.DisplayNameFor(model => model.Event[0].UrlString) + @Html.DisplayNameFor(model => model.Event[0].IsInternEvent) + @@ -82,6 +86,7 @@ @Html.DisplayFor(modelItem => item.UrlString) + @Html.DisplayFor(modelItem => item.IsInternEvent) + Edit | Details | - Delete + Delete | + Puzzles } diff --git a/ServerCore/Pages/Index.cshtml b/ServerCore/Pages/Index.cshtml index f0a923a4..15f43297 100644 --- a/ServerCore/Pages/Index.cshtml +++ b/ServerCore/Pages/Index.cshtml @@ -71,7 +71,7 @@

Guide to Microsoft Puzzle Events

diff --git a/ServerCore/Pages/Puzzles/Delete.cshtml.cs b/ServerCore/Pages/Puzzles/Delete.cshtml.cs index b7e2867f..945efda2 100644 --- a/ServerCore/Pages/Puzzles/Delete.cshtml.cs +++ b/ServerCore/Pages/Puzzles/Delete.cshtml.cs @@ -29,7 +29,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - Puzzle = await _context.Puzzle.SingleOrDefaultAsync(m => m.ID == id); + Puzzle = await _context.Puzzle.Where(m => m.ID == id).Include(p => p.Event).FirstOrDefaultAsync(); if (Puzzle == null) { @@ -45,7 +45,7 @@ public async Task OnPostAsync(int? id) return NotFound(); } - Puzzle = await _context.Puzzle.FindAsync(id); + Puzzle = await _context.Puzzle.Where(m => m.ID == id).Include(p => p.Event).FirstOrDefaultAsync(); if (Puzzle != null) { @@ -53,7 +53,7 @@ public async Task OnPostAsync(int? id) await _context.SaveChangesAsync(); } - return RedirectToPage("./Index"); + return RedirectToPage("./Index", new { eventid = Puzzle.Event?.ID }); } } } diff --git a/ServerCore/Pages/Puzzles/Details.cshtml b/ServerCore/Pages/Puzzles/Details.cshtml index b8912587..185965bb 100644 --- a/ServerCore/Pages/Puzzles/Details.cshtml +++ b/ServerCore/Pages/Puzzles/Details.cshtml @@ -93,5 +93,5 @@
Edit | - Back to List + Back to List
diff --git a/ServerCore/Pages/Puzzles/Details.cshtml.cs b/ServerCore/Pages/Puzzles/Details.cshtml.cs index 17a66c58..4719cd30 100644 --- a/ServerCore/Pages/Puzzles/Details.cshtml.cs +++ b/ServerCore/Pages/Puzzles/Details.cshtml.cs @@ -28,7 +28,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - Puzzle = await _context.Puzzle.SingleOrDefaultAsync(m => m.ID == id); + Puzzle = await _context.Puzzle.Where(m => m.ID == id).Include(p => p.Event).FirstOrDefaultAsync(); if (Puzzle == null) { diff --git a/ServerCore/Pages/Puzzles/Edit.cshtml b/ServerCore/Pages/Puzzles/Edit.cshtml index 6aa50984..bc4ee83e 100644 --- a/ServerCore/Pages/Puzzles/Edit.cshtml +++ b/ServerCore/Pages/Puzzles/Edit.cshtml @@ -95,7 +95,7 @@ @section Scripts { diff --git a/ServerCore/Pages/Puzzles/Edit.cshtml.cs b/ServerCore/Pages/Puzzles/Edit.cshtml.cs index 03eac2c2..6a532efd 100644 --- a/ServerCore/Pages/Puzzles/Edit.cshtml.cs +++ b/ServerCore/Pages/Puzzles/Edit.cshtml.cs @@ -30,7 +30,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - Puzzle = await _context.Puzzle.SingleOrDefaultAsync(m => m.ID == id); + Puzzle = await _context.Puzzle.Where(m => m.ID == id).Include(p => p.Event).FirstOrDefaultAsync(); if (Puzzle == null) { @@ -64,7 +64,7 @@ public async Task OnPostAsync() } } - return RedirectToPage("./Index"); + return RedirectToPage("./Index", new { eventid = Puzzle.Event?.ID }); } private bool PuzzleExists(int id) diff --git a/ServerCore/Pages/Puzzles/Index.cshtml b/ServerCore/Pages/Puzzles/Index.cshtml index e6f1f995..56546e88 100644 --- a/ServerCore/Pages/Puzzles/Index.cshtml +++ b/ServerCore/Pages/Puzzles/Index.cshtml @@ -8,7 +8,7 @@

Index

- Create New + Create New

diff --git a/ServerCore/Pages/Puzzles/Index.cshtml.cs b/ServerCore/Pages/Puzzles/Index.cshtml.cs index aaec7078..b33cc988 100644 --- a/ServerCore/Pages/Puzzles/Index.cshtml.cs +++ b/ServerCore/Pages/Puzzles/Index.cshtml.cs @@ -19,11 +19,21 @@ public IndexModel(ServerCore.Models.PuzzleServerContext context) _context = context; } - public IList Puzzle { get;set; } + public IList Puzzle { get; set; } - public async Task OnGetAsync() + public int? EventId { get; set; } + + public async Task OnGetAsync(int? eventid) { - Puzzle = await _context.Puzzle.ToListAsync(); + if (eventid != null) + { + Puzzle = await _context.Puzzle.Where((p) => p.Event != null && p.Event.ID == eventid).ToListAsync(); + EventId = eventid; + } + else + { + Puzzle = await _context.Puzzle.ToListAsync(); + } } } } From d7bd232c8b52495b8f64ad144f55f815d765b8d6 Mon Sep 17 00:00:00 2001 From: Kenny Young Date: Tue, 28 Aug 2018 23:50:27 -0700 Subject: [PATCH 2/2] CR --- ServerCore/Pages/Events/Index.cshtml | 122 ++-------------------- ServerCore/Pages/Puzzles/Create.cshtml.cs | 8 +- 2 files changed, 12 insertions(+), 118 deletions(-) diff --git a/ServerCore/Pages/Events/Index.cshtml b/ServerCore/Pages/Events/Index.cshtml index a47b37ee..98e54545 100644 --- a/ServerCore/Pages/Events/Index.cshtml +++ b/ServerCore/Pages/Events/Index.cshtml @@ -13,67 +13,15 @@
- - - - - + + + @@ -86,61 +34,9 @@ - -
- @Html.DisplayNameFor(model => model.Event[0].Name) - - @Html.DisplayNameFor(model => model.Event[0].UrlString) - - @Html.DisplayNameFor(model => model.Event[0].IsInternEvent) - + @Html.DisplayNameFor(model => model.Event[0].Name) + + @Html.DisplayNameFor(model => model.Event[0].UrlString) + + @Html.DisplayNameFor(model => model.Event[0].IsInternEvent) +
@Html.DisplayFor(modelItem => item.UrlString) @Html.DisplayFor(modelItem => item.IsInternEvent) Edit | Details | diff --git a/ServerCore/Pages/Puzzles/Create.cshtml.cs b/ServerCore/Pages/Puzzles/Create.cshtml.cs index 0e888210..bd913293 100644 --- a/ServerCore/Pages/Puzzles/Create.cshtml.cs +++ b/ServerCore/Pages/Puzzles/Create.cshtml.cs @@ -28,17 +28,15 @@ public IActionResult OnGet() [BindProperty] public Puzzle Puzzle { get; set; } - public async Task OnPostAsync(int? eventid) + public async Task OnPostAsync(int eventid) { if (!ModelState.IsValid) { return Page(); } - if (eventid != null) - { - Puzzle.Event = await _context.Event.SingleOrDefaultAsync(m => m.ID == eventid); - } + Puzzle.Event = await _context.Event.SingleOrDefaultAsync(m => m.ID == eventid); + _context.Puzzle.Add(Puzzle); await _context.SaveChangesAsync();