diff --git a/MultipleChoiceTrainer/Controllers/CategoriesController.cs b/MultipleChoiceTrainer/Controllers/CategoriesController.cs new file mode 100644 index 0000000..305713b --- /dev/null +++ b/MultipleChoiceTrainer/Controllers/CategoriesController.cs @@ -0,0 +1,118 @@ +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.DataModels; + +namespace MultipleChoiceTrainer.Controllers +{ + public class CategoriesController : Controller + { + private readonly ApplicationDbContext _context; + + public CategoriesController(ApplicationDbContext context) + { + _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() + { + return View(); + } + + // POST: Categories/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")] Category category) + { + if (ModelState.IsValid) + { + _context.Add(category); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index), "Home"); + } + return View(category); + } + + // GET: Categories/Edit/5 + public async Task Edit(int? id) + { + if (id == null) + { + return NotFound(); + } + + var category = await _context.Categories.FindAsync(id); + if (category == null) + { + return NotFound(); + } + return View(category); + } + + // POST: Categories/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")] Category category) + { + if (id != category.Id) + { + return NotFound(); + } + + if (ModelState.IsValid) + { + try + { + _context.Update(category); + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!CategoryExists(category.Id)) + { + return NotFound(); + } + else + { + throw; + } + } + return RedirectToAction(nameof(Index), "Home"); + } + return View(category); + } + + private bool CategoryExists(int id) + { + return _context.Categories.Any(e => e.Id == id); + } + } +} diff --git a/MultipleChoiceTrainer/Models/DataModels/Category.cs b/MultipleChoiceTrainer/Models/DataModels/Category.cs index 31f2553..795fbb2 100644 --- a/MultipleChoiceTrainer/Models/DataModels/Category.cs +++ b/MultipleChoiceTrainer/Models/DataModels/Category.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -8,7 +9,10 @@ namespace MultipleChoiceTrainer.Models.DataModels public class Category { public int Id { get; set; } + + [Display(Name="Name")] public string Name { get; set; } + [Display(Name = "Beschreibung")] public string Description { get; set; } public ICollection
Sections { get; set; } = new HashSet
(); diff --git a/MultipleChoiceTrainer/Models/DataModels/Section.cs b/MultipleChoiceTrainer/Models/DataModels/Section.cs index 4e838ae..da06fa6 100644 --- a/MultipleChoiceTrainer/Models/DataModels/Section.cs +++ b/MultipleChoiceTrainer/Models/DataModels/Section.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -8,9 +9,13 @@ namespace MultipleChoiceTrainer.Models.DataModels public class Section { public int Id { get; set; } + + [Display(Name = "Name")] public string Name { get; set; } + [Display(Name = "Beschreibung")] public string Description { get; set; } + public Category Category { get; set; } public int CategoryId { get; set; } diff --git a/MultipleChoiceTrainer/MultipleChoiceTrainer.csproj b/MultipleChoiceTrainer/MultipleChoiceTrainer.csproj index a04606c..f478dff 100644 --- a/MultipleChoiceTrainer/MultipleChoiceTrainer.csproj +++ b/MultipleChoiceTrainer/MultipleChoiceTrainer.csproj @@ -10,8 +10,10 @@ + + diff --git a/MultipleChoiceTrainer/Views/Categories/Create.cshtml b/MultipleChoiceTrainer/Views/Categories/Create.cshtml new file mode 100644 index 0000000..24d25ee --- /dev/null +++ b/MultipleChoiceTrainer/Views/Categories/Create.cshtml @@ -0,0 +1,33 @@ +@model MultipleChoiceTrainer.Models.DataModels.Category + +@{ + ViewData["Title"] = "Neuer Kurs"; +} +

Kurs erstellen

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

Kurs bearbeiten

+
+
+
+
+
+ +
+ + + +
+
+ + + +
+
+ + Abbrechen +
+
+
+
+ +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml index 8921b43..51471a2 100644 --- a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml +++ b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml @@ -1,20 +1,20 @@ @model IList -

Kategorie hinzufügen

+

Kurs hinzufügen

@foreach (var category in Model) { - + + + @foreach (var sub in category.Sections) { - + } } - - - - -
category.Name
+ + @category.Name @category.Description +
@sub.Name
@sub.Name
Hundetrainer
Lektion 1 - Der Hund
Lektion 2 - Der Schwanz
Touristikmanagement
Lektion 1 - Urlaub
Lektion 2 - Reisen mit dem kranken Hund
Ernährungsberatung
Lektion 1 - Zu viel essen - ist das möglich?
\ No newline at end of file