diff --git a/MultipleChoiceTrainer/Controllers/CategoriesController.cs b/MultipleChoiceTrainer/Controllers/CategoriesController.cs index 305713b..eb5ed08 100644 --- a/MultipleChoiceTrainer/Controllers/CategoriesController.cs +++ b/MultipleChoiceTrainer/Controllers/CategoriesController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using MultipleChoiceTrainer.Data; +using MultipleChoiceTrainer.Models; using MultipleChoiceTrainer.Models.DataModels; namespace MultipleChoiceTrainer.Controllers @@ -19,24 +20,6 @@ namespace MultipleChoiceTrainer.Controllers _context = context; } - // GET: Categories/Details/5 - public async Task Details(int? id) - { - if (id == null) - { - return NotFound(); - } - - var category = await _context.Categories - .FirstOrDefaultAsync(m => m.Id == id); - if (category == null) - { - return NotFound(); - } - - return View(category); - } - // GET: Categories/Create public IActionResult Create() { @@ -48,15 +31,15 @@ namespace MultipleChoiceTrainer.Controllers // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public async Task Create([Bind("Id,Name,Description")] Category category) + public async Task Create(CategoryEditViewModel editViewModel) { if (ModelState.IsValid) { - _context.Add(category); + _context.Add(editViewModel.Entity); await _context.SaveChangesAsync(); - return RedirectToAction(nameof(Index), "Home"); + return RedirectToAction(nameof(Index), "Home", new { categoryId = editViewModel.Entity.Id }); } - return View(category); + return View(editViewModel); } // GET: Categories/Edit/5 @@ -72,7 +55,7 @@ namespace MultipleChoiceTrainer.Controllers { return NotFound(); } - return View(category); + return View(new CategoryEditViewModel() { Entity = category }); } // POST: Categories/Edit/5 @@ -80,23 +63,18 @@ namespace MultipleChoiceTrainer.Controllers // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(int id, [Bind("Id,Name,Description")] Category category) + public async Task Edit(CategoryEditViewModel editViewModel) { - if (id != category.Id) - { - return NotFound(); - } - if (ModelState.IsValid) { try { - _context.Update(category); + _context.Update(editViewModel.Entity); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { - if (!CategoryExists(category.Id)) + if (!CategoryExists(editViewModel.Entity.Id)) { return NotFound(); } @@ -105,9 +83,9 @@ namespace MultipleChoiceTrainer.Controllers throw; } } - return RedirectToAction(nameof(Index), "Home"); + return RedirectToAction(nameof(Index), "Home", new { categoryId = editViewModel.Entity.Id }); } - return View(category); + return View(editViewModel); } private bool CategoryExists(int id) diff --git a/MultipleChoiceTrainer/Controllers/HomeController.cs b/MultipleChoiceTrainer/Controllers/HomeController.cs index 959d57d..df4ccde 100644 --- a/MultipleChoiceTrainer/Controllers/HomeController.cs +++ b/MultipleChoiceTrainer/Controllers/HomeController.cs @@ -25,12 +25,13 @@ namespace MultipleChoiceTrainer.Controllers this.context = context; } - public IActionResult Index() + public IActionResult Index(int? categoryId) { if (signInManager.IsSignedIn(User)) { var categories = context.Categories.Include(e => e.Sections).ThenInclude(e => e.Questions).ToList(); - return View(categories); + var selectedCategory = categoryId.HasValue ? categoryId.Value : categories.First().Id; + return View(new HomeViewModel() { Categories = categories, SelectedCategoryId = selectedCategory }); } return View(); } diff --git a/MultipleChoiceTrainer/Controllers/SectionsController.cs b/MultipleChoiceTrainer/Controllers/SectionsController.cs new file mode 100644 index 0000000..3e676e5 --- /dev/null +++ b/MultipleChoiceTrainer/Controllers/SectionsController.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using MultipleChoiceTrainer.Data; +using MultipleChoiceTrainer.Models; +using MultipleChoiceTrainer.Models.DataModels; + +namespace MultipleChoiceTrainer.Controllers +{ + public class SectionsController : Controller + { + private readonly ApplicationDbContext _context; + + public SectionsController(ApplicationDbContext context) + { + _context = context; + } + + // GET: Sections/Create + public IActionResult Create(int? categoryId) + { + if(!categoryId.HasValue) + { + categoryId = _context.Categories.First().Id; + } + + ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", categoryId); + return View(); + } + + // POST: Sections/Create + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Create([Bind("Id,Name,Description,CategoryId")] Section section) + { + if (ModelState.IsValid) + { + _context.Add(section); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId }); + } + ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId); + return View(section); + } + + // GET: Sections/Edit/5 + public async Task Edit(int? id) + { + if (id == null) + { + return NotFound(); + } + + var section = await _context.Sections.FindAsync(id); + if (section == null) + { + return NotFound(); + } + ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId); + return View(section); + } + + // POST: Sections/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Edit(int id, [Bind("Id,Name,Description,CategoryId")] Section section) + { + if (id != section.Id) + { + return NotFound(); + } + + if (ModelState.IsValid) + { + try + { + _context.Update(section); + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!SectionExists(section.Id)) + { + return NotFound(); + } + else + { + throw; + } + } + return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId }); + } + ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId); + return View(section); + } + + + private bool SectionExists(int id) + { + return _context.Sections.Any(e => e.Id == id); + } + } +} diff --git a/MultipleChoiceTrainer/Models/AbstractEditViewModel.cs b/MultipleChoiceTrainer/Models/AbstractEditViewModel.cs new file mode 100644 index 0000000..0017288 --- /dev/null +++ b/MultipleChoiceTrainer/Models/AbstractEditViewModel.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace MultipleChoiceTrainer.Models +{ + public class AbstractEditViewModel + where T: new() + { + public T Entity { get; set; } + public int? PreviousCategoryId { get; set; } + + public SelectList Assignables { get; set; } + } +} \ No newline at end of file diff --git a/MultipleChoiceTrainer/Models/CategoryEditViewModel.cs b/MultipleChoiceTrainer/Models/CategoryEditViewModel.cs new file mode 100644 index 0000000..1ede2fb --- /dev/null +++ b/MultipleChoiceTrainer/Models/CategoryEditViewModel.cs @@ -0,0 +1,12 @@ +using MultipleChoiceTrainer.Models.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MultipleChoiceTrainer.Models +{ + public class CategoryEditViewModel: AbstractEditViewModel + { + } +} diff --git a/MultipleChoiceTrainer/Models/DataModels/Section.cs b/MultipleChoiceTrainer/Models/DataModels/Section.cs index da06fa6..188499f 100644 --- a/MultipleChoiceTrainer/Models/DataModels/Section.cs +++ b/MultipleChoiceTrainer/Models/DataModels/Section.cs @@ -15,8 +15,9 @@ namespace MultipleChoiceTrainer.Models.DataModels [Display(Name = "Beschreibung")] public string Description { get; set; } - + [Display(Name = "Kurs")] public Category Category { get; set; } + [Display(Name = "Kurs")] public int CategoryId { get; set; } public ICollection Questions { get; set; } = new HashSet(); diff --git a/MultipleChoiceTrainer/Models/HomeViewModel.cs b/MultipleChoiceTrainer/Models/HomeViewModel.cs new file mode 100644 index 0000000..543b464 --- /dev/null +++ b/MultipleChoiceTrainer/Models/HomeViewModel.cs @@ -0,0 +1,14 @@ +using MultipleChoiceTrainer.Models.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MultipleChoiceTrainer.Models +{ + public class HomeViewModel + { + public IList Categories { get; set; } + public int SelectedCategoryId { get; set; } + } +} diff --git a/MultipleChoiceTrainer/Models/SectionEditViewModel.cs b/MultipleChoiceTrainer/Models/SectionEditViewModel.cs new file mode 100644 index 0000000..1c67ce6 --- /dev/null +++ b/MultipleChoiceTrainer/Models/SectionEditViewModel.cs @@ -0,0 +1,12 @@ +using MultipleChoiceTrainer.Models.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MultipleChoiceTrainer.Models +{ + public class SectionEditViewModel: AbstractEditViewModel
+ { + } +} diff --git a/MultipleChoiceTrainer/Views/Categories/Create.cshtml b/MultipleChoiceTrainer/Views/Categories/Create.cshtml index 24d25ee..a80d56c 100644 --- a/MultipleChoiceTrainer/Views/Categories/Create.cshtml +++ b/MultipleChoiceTrainer/Views/Categories/Create.cshtml @@ -1,4 +1,4 @@ -@model MultipleChoiceTrainer.Models.DataModels.Category +@model MultipleChoiceTrainer.Models.CategoryEditViewModel @{ ViewData["Title"] = "Neuer Kurs"; @@ -10,14 +10,14 @@
- - - + + +
- - - + + +
diff --git a/MultipleChoiceTrainer/Views/Categories/Edit.cshtml b/MultipleChoiceTrainer/Views/Categories/Edit.cshtml index a1f1bcf..7b2633a 100644 --- a/MultipleChoiceTrainer/Views/Categories/Edit.cshtml +++ b/MultipleChoiceTrainer/Views/Categories/Edit.cshtml @@ -1,25 +1,25 @@ -@model MultipleChoiceTrainer.Models.DataModels.Category +@model MultipleChoiceTrainer.Models.CategoryEditViewModel @{ ViewData["Title"] = "Kurs bearbeiten"; } -

Kurs bearbeiten

+

Kurs bearbeiten


- +
- - - + + +
- - - + + +
diff --git a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml index 51471a2..bc1aa78 100644 --- a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml +++ b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml @@ -1,20 +1,47 @@ -@model IList -

Kurs hinzufügen

- - @foreach (var category in Model) - { - - - - @foreach (var sub in category.Sections) - { - - } - } - - - -
- - @category.Name @category.Description -
@sub.Name
Hundetrainer
Lektion 1 - Der Hund
Lektion 2 - Der Schwanz
\ No newline at end of file +@model MultipleChoiceTrainer.Models.HomeViewModel +

Kursübersicht

+
+
+ +
+
+
+ @foreach (var category in Model.Categories) + { +
+

@category.Name @category.Description

+ +

+ Kurs bearbeiten     + Lektion hinzufügen     + Quiz starten +

+ +
+ @foreach (var sub in category.Sections) + { + + } +
+ +
+ } +
+
+
\ No newline at end of file diff --git a/MultipleChoiceTrainer/Views/Sections/Create.cshtml b/MultipleChoiceTrainer/Views/Sections/Create.cshtml new file mode 100644 index 0000000..17aff85 --- /dev/null +++ b/MultipleChoiceTrainer/Views/Sections/Create.cshtml @@ -0,0 +1,37 @@ +@model MultipleChoiceTrainer.Models.DataModels.Section + +@{ + ViewData["Title"] = "Lektion anlegen"; +} + +

Lektion anlegen

+
+
+
+ +
+
+ + + +
+
+ + + +
+
+ + +
+
+ + Abbrechen +
+ +
+
+ +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/MultipleChoiceTrainer/Views/Sections/Edit.cshtml b/MultipleChoiceTrainer/Views/Sections/Edit.cshtml new file mode 100644 index 0000000..c3674d9 --- /dev/null +++ b/MultipleChoiceTrainer/Views/Sections/Edit.cshtml @@ -0,0 +1,39 @@ +@model MultipleChoiceTrainer.Models.DataModels.Section + +@{ + ViewData["Title"] = "Lektion bearbeiten"; +} + +

Lektion bearbeiten

+
+
+
+
+
+ +
+ + + +
+
+ + + +
+
+ + + +
+
+ + Abbrechen +
+
+
+
+ +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +}