From 7f5392366654799ff5246cd7978c93ffed77583c Mon Sep 17 00:00:00 2001 From: saranell Date: Wed, 16 Nov 2022 18:03:16 -0600 Subject: [PATCH 01/11] 17.4 exercises --- CodingEventsDemo/CodingEventsDemo.csproj | 18 ++++- CodingEventsDemo/CodingEventsDemo.sln | 25 +++++++ .../Controllers/EventCategoryController.cs | 30 +++++++++ .../Controllers/EventsController.cs | 1 - CodingEventsDemo/Data/EventDbContext.cs | 2 + ...6234855_EventCategoryMigration.Designer.cs | 62 +++++++++++++++++ .../20221116234855_EventCategoryMigration.cs | 67 +++++++++++++++++++ .../Migrations/EventDbContextModelSnapshot.cs | 24 +++++-- CodingEventsDemo/Models/EventCategory.cs | 20 ++++++ .../Properties/launchSettings.json | 4 +- CodingEventsDemo/Startup.cs | 3 +- .../Views/EventCategory/Index.cshtml | 15 +++++ CodingEventsDemo/appsettings.json | 2 +- 13 files changed, 261 insertions(+), 12 deletions(-) create mode 100644 CodingEventsDemo/CodingEventsDemo.sln create mode 100644 CodingEventsDemo/Controllers/EventCategoryController.cs create mode 100644 CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs create mode 100644 CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs create mode 100644 CodingEventsDemo/Models/EventCategory.cs create mode 100644 CodingEventsDemo/Views/EventCategory/Index.cshtml diff --git a/CodingEventsDemo/CodingEventsDemo.csproj b/CodingEventsDemo/CodingEventsDemo.csproj index 43e0f7d0..a5286743 100644 --- a/CodingEventsDemo/CodingEventsDemo.csproj +++ b/CodingEventsDemo/CodingEventsDemo.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net6 @@ -10,12 +10,24 @@ + - - + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all + + diff --git a/CodingEventsDemo/CodingEventsDemo.sln b/CodingEventsDemo/CodingEventsDemo.sln new file mode 100644 index 00000000..2acb6130 --- /dev/null +++ b/CodingEventsDemo/CodingEventsDemo.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 25.0.1703.6 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodingEventsDemo", "CodingEventsDemo.csproj", "{33BFA655-C28C-4D2C-AD92-7D9E1D909008}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {33BFA655-C28C-4D2C-AD92-7D9E1D909008}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33BFA655-C28C-4D2C-AD92-7D9E1D909008}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33BFA655-C28C-4D2C-AD92-7D9E1D909008}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33BFA655-C28C-4D2C-AD92-7D9E1D909008}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4099042F-0400-4C61-BBA5-6463A59889B8} + EndGlobalSection +EndGlobal diff --git a/CodingEventsDemo/Controllers/EventCategoryController.cs b/CodingEventsDemo/Controllers/EventCategoryController.cs new file mode 100644 index 00000000..b6f9df48 --- /dev/null +++ b/CodingEventsDemo/Controllers/EventCategoryController.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CodingEventsDemo.Data; +using CodingEventsDemo.Models; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace CodingEventsDemo.Controllers +{ + public class EventCategoryController : Controller + { + private EventDbContext context { get; set; } + + public EventCategoryController(EventDbContext dbContext) + { + context = dbContext; + } + + // GET: // + [HttpGet] + public IActionResult Index() + { + ViewBag.title = "All Categories"; + List categories = context.Categories.ToList(); + return View(categories); + } + } +} + diff --git a/CodingEventsDemo/Controllers/EventsController.cs b/CodingEventsDemo/Controllers/EventsController.cs index 3fd6eb14..aef560ec 100644 --- a/CodingEventsDemo/Controllers/EventsController.cs +++ b/CodingEventsDemo/Controllers/EventsController.cs @@ -13,7 +13,6 @@ namespace coding_events_practice.Controllers { public class EventsController : Controller { - private EventDbContext context; public EventsController(EventDbContext dbContext) diff --git a/CodingEventsDemo/Data/EventDbContext.cs b/CodingEventsDemo/Data/EventDbContext.cs index 4d7f60d1..06e42719 100644 --- a/CodingEventsDemo/Data/EventDbContext.cs +++ b/CodingEventsDemo/Data/EventDbContext.cs @@ -8,6 +8,8 @@ public class EventDbContext : DbContext { public DbSet Events { get; set; } + public DbSet Categories { get; set; } + public EventDbContext(DbContextOptions options) : base(options) { } diff --git a/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs b/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs new file mode 100644 index 00000000..5f847f1f --- /dev/null +++ b/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs @@ -0,0 +1,62 @@ +// +using CodingEventsDemo.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CodingEventsDemo.Migrations +{ + [DbContext(typeof(EventDbContext))] + [Migration("20221116234855_EventCategoryMigration")] + partial class EventCategoryMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ContactEmail") + .HasColumnType("longtext"); + + b.Property("Description") + .HasColumnType("longtext"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.Property("Type") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Events"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs b/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs new file mode 100644 index 00000000..12266ef4 --- /dev/null +++ b/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs @@ -0,0 +1,67 @@ +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace CodingEventsDemo.Migrations +{ + public partial class EventCategoryMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Categories", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + Name = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + }, + constraints: table => + { + table.PrimaryKey("PK_Categories", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Categories"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Events", + type: "longtext CHARACTER SET utf8mb4", + nullable: true, + oldClrType: typeof(string), + oldType: "longtext", + oldNullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + .OldAnnotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.AlterColumn( + name: "Description", + table: "Events", + type: "longtext CHARACTER SET utf8mb4", + nullable: true, + oldClrType: typeof(string), + oldType: "longtext", + oldNullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + .OldAnnotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.AlterColumn( + name: "ContactEmail", + table: "Events", + type: "longtext CHARACTER SET utf8mb4", + nullable: true, + oldClrType: typeof(string), + oldType: "longtext", + oldNullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + .OldAnnotation("MySql:CharSet", "utf8mb4"); + } + } +} diff --git a/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs b/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs index 5f9989c3..7ae55706 100644 --- a/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs +++ b/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs @@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +#nullable disable + namespace CodingEventsDemo.Migrations { [DbContext(typeof(EventDbContext))] @@ -13,7 +15,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "3.1.5") + .HasAnnotation("ProductVersion", "6.0.1") .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("CodingEventsDemo.Models.Event", b => @@ -23,13 +25,13 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("int"); b.Property("ContactEmail") - .HasColumnType("longtext CHARACTER SET utf8mb4"); + .HasColumnType("longtext"); b.Property("Description") - .HasColumnType("longtext CHARACTER SET utf8mb4"); + .HasColumnType("longtext"); b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); + .HasColumnType("longtext"); b.Property("Type") .HasColumnType("int"); @@ -38,6 +40,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("Events"); }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); #pragma warning restore 612, 618 } } diff --git a/CodingEventsDemo/Models/EventCategory.cs b/CodingEventsDemo/Models/EventCategory.cs new file mode 100644 index 00000000..1e967ce3 --- /dev/null +++ b/CodingEventsDemo/Models/EventCategory.cs @@ -0,0 +1,20 @@ +using System; +namespace CodingEventsDemo.Models +{ + public class EventCategory + { + public int Id { get; set; } + + public string Name { get; set; } + + public EventCategory() + { + } + + public EventCategory(string name) + { + Name = name; + } + } +} + diff --git a/CodingEventsDemo/Properties/launchSettings.json b/CodingEventsDemo/Properties/launchSettings.json index 0b123df6..6049ed67 100644 --- a/CodingEventsDemo/Properties/launchSettings.json +++ b/CodingEventsDemo/Properties/launchSettings.json @@ -18,10 +18,10 @@ "CodingEventsDemo": { "commandName": "Project", "launchBrowser": true, + "applicationUrl": "http://localhost:23921", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "https://localhost:5001;http://localhost:5000" + } } } } \ No newline at end of file diff --git a/CodingEventsDemo/Startup.cs b/CodingEventsDemo/Startup.cs index 07282c05..74a73c98 100644 --- a/CodingEventsDemo/Startup.cs +++ b/CodingEventsDemo/Startup.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using static System.Net.Mime.MediaTypeNames; namespace CodingEventsDemo { @@ -27,7 +28,7 @@ public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext(options => - options.UseMySql(Configuration.GetConnectionString("DefaultConnection"))); + options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), new MySqlServerVersion(new Version()))); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/CodingEventsDemo/Views/EventCategory/Index.cshtml b/CodingEventsDemo/Views/EventCategory/Index.cshtml new file mode 100644 index 00000000..d0e9cc48 --- /dev/null +++ b/CodingEventsDemo/Views/EventCategory/Index.cshtml @@ -0,0 +1,15 @@ +@model List + +

Event Categories

+ + + + + + @foreach(EventCategory category in Model) + { + + + + } +
Category Name
@category.Name
\ No newline at end of file diff --git a/CodingEventsDemo/appsettings.json b/CodingEventsDemo/appsettings.json index aad2f680..1cad0ac9 100644 --- a/CodingEventsDemo/appsettings.json +++ b/CodingEventsDemo/appsettings.json @@ -8,6 +8,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "server=localhost;userid=coding_events;password=Learn2code!;database=coding_events;" + "DefaultConnection": "server=localhost;userid=coding_events;password=LCWomen+;database=coding_events;" } } From 2f68b58869edaa41d764ec75a1fe6c88f41e4593 Mon Sep 17 00:00:00 2001 From: saranell Date: Wed, 16 Nov 2022 18:41:07 -0600 Subject: [PATCH 02/11] 17.5 studio --- .../Controllers/EventCategoryController.cs | 27 +++++++++++++++++++ .../ViewModels/AddEventCategoryViewModel.cs | 17 ++++++++++++ .../Views/EventCategory/Create.cshtml | 12 +++++++++ CodingEventsDemo/Views/Shared/_Layout.cshtml | 10 +++++++ 4 files changed, 66 insertions(+) create mode 100644 CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs create mode 100644 CodingEventsDemo/Views/EventCategory/Create.cshtml diff --git a/CodingEventsDemo/Controllers/EventCategoryController.cs b/CodingEventsDemo/Controllers/EventCategoryController.cs index b6f9df48..69f99ad5 100644 --- a/CodingEventsDemo/Controllers/EventCategoryController.cs +++ b/CodingEventsDemo/Controllers/EventCategoryController.cs @@ -3,6 +3,7 @@ using System.Linq; using CodingEventsDemo.Data; using CodingEventsDemo.Models; +using CodingEventsDemo.ViewModels; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -25,6 +26,32 @@ public IActionResult Index() List categories = context.Categories.ToList(); return View(categories); } + + [HttpGet] + public IActionResult Create() + { + AddEventCategoryViewModel addEventCategoryViewModel = new AddEventCategoryViewModel(); + return View(addEventCategoryViewModel); + } + + [HttpPost] + public IActionResult ProcessCreateEventCategoryForm(AddEventCategoryViewModel addEventCategoryViewModel) + { + if (ModelState.IsValid) + { + EventCategory newCategory = new EventCategory + { + Name = addEventCategoryViewModel.Name + }; + + context.Categories.Add(newCategory); + context.SaveChanges(); + + return Redirect("/EventCategory"); + } + + return View("Create", addEventCategoryViewModel); + } } } diff --git a/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs b/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs new file mode 100644 index 00000000..0af3b0e2 --- /dev/null +++ b/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace CodingEventsDemo.ViewModels +{ + public class AddEventCategoryViewModel + { + [Required(ErrorMessage = "Event name is required.")] + [StringLength(20, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 20 characters long.")] + public string Name { get; set; } + + public AddEventCategoryViewModel() + { + } + } +} + diff --git a/CodingEventsDemo/Views/EventCategory/Create.cshtml b/CodingEventsDemo/Views/EventCategory/Create.cshtml new file mode 100644 index 00000000..4a4ba489 --- /dev/null +++ b/CodingEventsDemo/Views/EventCategory/Create.cshtml @@ -0,0 +1,12 @@ +@model CodingEventsDemo.ViewModels.AddEventCategoryViewModel + +

Add New Category

+ +
+
+ + + +
+ +
\ No newline at end of file diff --git a/CodingEventsDemo/Views/Shared/_Layout.cshtml b/CodingEventsDemo/Views/Shared/_Layout.cshtml index f42a68a7..d7a3d7b7 100644 --- a/CodingEventsDemo/Views/Shared/_Layout.cshtml +++ b/CodingEventsDemo/Views/Shared/_Layout.cshtml @@ -20,6 +20,16 @@ From d19466dc399fa773770a4f96863ee608c0dbe58c Mon Sep 17 00:00:00 2001 From: saranell Date: Wed, 16 Nov 2022 18:41:07 -0600 Subject: [PATCH 03/11] 17.5 studio --- .../Controllers/EventCategoryController.cs | 1 + .../ViewModels/AddEventCategoryViewModel.cs | 9 +++++++-- .../Views/EventCategory/Create.cshtml | 16 ++++++++-------- CodingEventsDemo/Views/Shared/_Layout.cshtml | 1 + 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CodingEventsDemo/Controllers/EventCategoryController.cs b/CodingEventsDemo/Controllers/EventCategoryController.cs index 4ee1e946..e1113e43 100644 --- a/CodingEventsDemo/Controllers/EventCategoryController.cs +++ b/CodingEventsDemo/Controllers/EventCategoryController.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using CodingEventsDemo.Data; using CodingEventsDemo.Models; +using CodingEventsDemo.ViewModels; using Microsoft.AspNetCore.Mvc; using CodingEventsDemo.ViewModels; diff --git a/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs b/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs index bf455328..0af3b0e2 100644 --- a/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs +++ b/CodingEventsDemo/ViewModels/AddEventCategoryViewModel.cs @@ -5,8 +5,13 @@ namespace CodingEventsDemo.ViewModels { public class AddEventCategoryViewModel { - [Required(ErrorMessage = "Name is required!")] - [StringLength(20, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 20 characters long")] + [Required(ErrorMessage = "Event name is required.")] + [StringLength(20, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 20 characters long.")] public string Name { get; set; } + + public AddEventCategoryViewModel() + { + } } } + diff --git a/CodingEventsDemo/Views/EventCategory/Create.cshtml b/CodingEventsDemo/Views/EventCategory/Create.cshtml index abca1ce7..4a4ba489 100644 --- a/CodingEventsDemo/Views/EventCategory/Create.cshtml +++ b/CodingEventsDemo/Views/EventCategory/Create.cshtml @@ -2,11 +2,11 @@

Add New Category

-
-
- - - -
- -
+
+
+ + + +
+ +
\ No newline at end of file diff --git a/CodingEventsDemo/Views/Shared/_Layout.cshtml b/CodingEventsDemo/Views/Shared/_Layout.cshtml index bd93997b..d7a3d7b7 100644 --- a/CodingEventsDemo/Views/Shared/_Layout.cshtml +++ b/CodingEventsDemo/Views/Shared/_Layout.cshtml @@ -19,6 +19,7 @@
- - + +
From 825e824354fc0c3f728c5ea1fa46a6379e2188f9 Mon Sep 17 00:00:00 2001 From: saranell Date: Tue, 22 Nov 2022 15:27:19 -0600 Subject: [PATCH 06/11] coding events 5.1 --- CodingEventsDemo/CodingEventsDemo.csproj | 5 ++ .../Controllers/EventCategoryController.cs | 1 - .../Controllers/EventsController.cs | 11 +-- ...0200622195148_InitialMigration.Designer.cs | 46 ------------- .../20200622195148_InitialMigration.cs | 33 --------- ...00625184338_MyInitialMigration.Designer.cs | 46 ------------- .../20200625184338_MyInitialMigration.cs | 17 ----- ...200625205525_CategoryMigration.Designer.cs | 60 ----------------- .../20200625205525_CategoryMigration.cs | 30 --------- ...6234855_EventCategoryMigration.Designer.cs | 62 ----------------- .../20221116234855_EventCategoryMigration.cs | 67 ------------------- .../Migrations/EventDbContextModelSnapshot.cs | 36 +++++++--- .../ViewModels/AddEventViewModel.cs | 2 +- CodingEventsDemo/Views/Events/Add.cshtml | 2 +- CodingEventsDemo/Views/Events/Index.cshtml | 4 +- 15 files changed, 44 insertions(+), 378 deletions(-) delete mode 100644 CodingEventsDemo/Migrations/20200622195148_InitialMigration.Designer.cs delete mode 100644 CodingEventsDemo/Migrations/20200622195148_InitialMigration.cs delete mode 100644 CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.Designer.cs delete mode 100644 CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.cs delete mode 100644 CodingEventsDemo/Migrations/20200625205525_CategoryMigration.Designer.cs delete mode 100644 CodingEventsDemo/Migrations/20200625205525_CategoryMigration.cs delete mode 100644 CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs delete mode 100644 CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs diff --git a/CodingEventsDemo/CodingEventsDemo.csproj b/CodingEventsDemo/CodingEventsDemo.csproj index 5ebe8278..3876c903 100644 --- a/CodingEventsDemo/CodingEventsDemo.csproj +++ b/CodingEventsDemo/CodingEventsDemo.csproj @@ -16,9 +16,14 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + diff --git a/CodingEventsDemo/Controllers/EventCategoryController.cs b/CodingEventsDemo/Controllers/EventCategoryController.cs index e1113e43..924ee974 100644 --- a/CodingEventsDemo/Controllers/EventCategoryController.cs +++ b/CodingEventsDemo/Controllers/EventCategoryController.cs @@ -6,7 +6,6 @@ using CodingEventsDemo.Models; using CodingEventsDemo.ViewModels; using Microsoft.AspNetCore.Mvc; -using CodingEventsDemo.ViewModels; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 diff --git a/CodingEventsDemo/Controllers/EventsController.cs b/CodingEventsDemo/Controllers/EventsController.cs index 14ad6fca..a4f55c79 100644 --- a/CodingEventsDemo/Controllers/EventsController.cs +++ b/CodingEventsDemo/Controllers/EventsController.cs @@ -6,6 +6,7 @@ using CodingEventsDemo.Models; using CodingEventsDemo.ViewModels; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -23,7 +24,9 @@ public EventsController(EventDbContext dbContext) // GET: // public IActionResult Index() { - List events = context.Events.ToList(); + List events = context.Events + .Include(e => e.Category) + .ToList(); return View(events); } @@ -31,7 +34,7 @@ public IActionResult Index() public IActionResult Add() { List categories = context.Categories.ToList(); - AddEventViewModel addEventViewModel = new AddEventViewModel(); + AddEventViewModel addEventViewModel = new AddEventViewModel(categories); return View(addEventViewModel); } @@ -41,13 +44,13 @@ public IActionResult Add(AddEventViewModel addEventViewModel) { if (ModelState.IsValid) { - EventCategory theCategory = context.Categories.Find(addEventViewModel.CategoryId); + EventCategory category = context.Categories.Find(addEventViewModel.CategoryId); Event newEvent = new Event { Name = addEventViewModel.Name, Description = addEventViewModel.Description, ContactEmail = addEventViewModel.ContactEmail, - Category = theCategory + Category = category }; context.Events.Add(newEvent); diff --git a/CodingEventsDemo/Migrations/20200622195148_InitialMigration.Designer.cs b/CodingEventsDemo/Migrations/20200622195148_InitialMigration.Designer.cs deleted file mode 100644 index 824d92dc..00000000 --- a/CodingEventsDemo/Migrations/20200622195148_InitialMigration.Designer.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -using CodingEventsDemo.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace CodingEventsDemo.Migrations -{ - [DbContext(typeof(EventDbContext))] - [Migration("20200622195148_InitialMigration")] - partial class InitialMigration - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "3.1.5") - .HasAnnotation("Relational:MaxIdentifierLength", 64); - - modelBuilder.Entity("CodingEventsDemo.Models.Event", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.Property("ContactEmail") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Description") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Type") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Events"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/CodingEventsDemo/Migrations/20200622195148_InitialMigration.cs b/CodingEventsDemo/Migrations/20200622195148_InitialMigration.cs deleted file mode 100644 index ad1c4792..00000000 --- a/CodingEventsDemo/Migrations/20200622195148_InitialMigration.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace CodingEventsDemo.Migrations -{ - public partial class InitialMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Events", - columns: table => new - { - Id = table.Column(nullable: false) - .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), - Name = table.Column(nullable: true), - Description = table.Column(nullable: true), - ContactEmail = table.Column(nullable: true), - Type = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Events", x => x.Id); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Events"); - } - } -} diff --git a/CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.Designer.cs b/CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.Designer.cs deleted file mode 100644 index 9571e202..00000000 --- a/CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.Designer.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -using CodingEventsDemo.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace CodingEventsDemo.Migrations -{ - [DbContext(typeof(EventDbContext))] - [Migration("20200625184338_MyInitialMigration")] - partial class MyInitialMigration - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "3.1.5") - .HasAnnotation("Relational:MaxIdentifierLength", 64); - - modelBuilder.Entity("CodingEventsDemo.Models.Event", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.Property("ContactEmail") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Description") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Type") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Events"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.cs b/CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.cs deleted file mode 100644 index 470e8717..00000000 --- a/CodingEventsDemo/Migrations/20200625184338_MyInitialMigration.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -namespace CodingEventsDemo.Migrations -{ - public partial class MyInitialMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/CodingEventsDemo/Migrations/20200625205525_CategoryMigration.Designer.cs b/CodingEventsDemo/Migrations/20200625205525_CategoryMigration.Designer.cs deleted file mode 100644 index 0001be8f..00000000 --- a/CodingEventsDemo/Migrations/20200625205525_CategoryMigration.Designer.cs +++ /dev/null @@ -1,60 +0,0 @@ -// -using CodingEventsDemo.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace CodingEventsDemo.Migrations -{ - [DbContext(typeof(EventDbContext))] - [Migration("20200625205525_CategoryMigration")] - partial class CategoryMigration - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "3.1.5") - .HasAnnotation("Relational:MaxIdentifierLength", 64); - - modelBuilder.Entity("CodingEventsDemo.Models.Event", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.Property("ContactEmail") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Description") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Type") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Events"); - }); - - modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.HasKey("Id"); - - b.ToTable("Categories"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/CodingEventsDemo/Migrations/20200625205525_CategoryMigration.cs b/CodingEventsDemo/Migrations/20200625205525_CategoryMigration.cs deleted file mode 100644 index b0ff5016..00000000 --- a/CodingEventsDemo/Migrations/20200625205525_CategoryMigration.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace CodingEventsDemo.Migrations -{ - public partial class CategoryMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Categories", - columns: table => new - { - Id = table.Column(nullable: false) - .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), - Name = table.Column(nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Categories", x => x.Id); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Categories"); - } - } -} diff --git a/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs b/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs deleted file mode 100644 index 5f847f1f..00000000 --- a/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.Designer.cs +++ /dev/null @@ -1,62 +0,0 @@ -// -using CodingEventsDemo.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace CodingEventsDemo.Migrations -{ - [DbContext(typeof(EventDbContext))] - [Migration("20221116234855_EventCategoryMigration")] - partial class EventCategoryMigration - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "6.0.1") - .HasAnnotation("Relational:MaxIdentifierLength", 64); - - modelBuilder.Entity("CodingEventsDemo.Models.Event", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.Property("ContactEmail") - .HasColumnType("longtext"); - - b.Property("Description") - .HasColumnType("longtext"); - - b.Property("Name") - .HasColumnType("longtext"); - - b.Property("Type") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Events"); - }); - - modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - b.Property("Name") - .HasColumnType("longtext"); - - b.HasKey("Id"); - - b.ToTable("Categories"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs b/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs deleted file mode 100644 index 12266ef4..00000000 --- a/CodingEventsDemo/Migrations/20221116234855_EventCategoryMigration.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace CodingEventsDemo.Migrations -{ - public partial class EventCategoryMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Categories", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), - Name = table.Column(type: "longtext", nullable: true) - .Annotation("MySql:CharSet", "utf8mb4") - }, - constraints: table => - { - table.PrimaryKey("PK_Categories", x => x.Id); - }) - .Annotation("MySql:CharSet", "utf8mb4"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Categories"); - - migrationBuilder.AlterColumn( - name: "Name", - table: "Events", - type: "longtext CHARACTER SET utf8mb4", - nullable: true, - oldClrType: typeof(string), - oldType: "longtext", - oldNullable: true) - .Annotation("MySql:CharSet", "utf8mb4") - .OldAnnotation("MySql:CharSet", "utf8mb4"); - - migrationBuilder.AlterColumn( - name: "Description", - table: "Events", - type: "longtext CHARACTER SET utf8mb4", - nullable: true, - oldClrType: typeof(string), - oldType: "longtext", - oldNullable: true) - .Annotation("MySql:CharSet", "utf8mb4") - .OldAnnotation("MySql:CharSet", "utf8mb4"); - - migrationBuilder.AlterColumn( - name: "ContactEmail", - table: "Events", - type: "longtext CHARACTER SET utf8mb4", - nullable: true, - oldClrType: typeof(string), - oldType: "longtext", - oldNullable: true) - .Annotation("MySql:CharSet", "utf8mb4") - .OldAnnotation("MySql:CharSet", "utf8mb4"); - } - } -} diff --git a/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs b/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs index 87e3e7e4..0efc66c7 100644 --- a/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs +++ b/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs @@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +#nullable disable + namespace CodingEventsDemo.Migrations { [DbContext(typeof(EventDbContext))] @@ -13,7 +15,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "3.1.5") + .HasAnnotation("ProductVersion", "6.0.1") .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("CodingEventsDemo.Models.Event", b => @@ -22,20 +24,22 @@ protected override void BuildModel(ModelBuilder modelBuilder) .ValueGeneratedOnAdd() .HasColumnType("int"); + b.Property("CategoryId") + .HasColumnType("int"); + b.Property("ContactEmail") - .HasColumnType("longtext CHARACTER SET utf8mb4"); + .HasColumnType("longtext"); b.Property("Description") - .HasColumnType("longtext CHARACTER SET utf8mb4"); + .HasColumnType("longtext"); b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); - - b.Property("Type") - .HasColumnType("int"); + .HasColumnType("longtext"); b.HasKey("Id"); + b.HasIndex("CategoryId"); + b.ToTable("Events"); }); @@ -46,12 +50,28 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("int"); b.Property("Name") - .HasColumnType("longtext CHARACTER SET utf8mb4"); + .HasColumnType("longtext"); b.HasKey("Id"); b.ToTable("Categories"); }); + + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => + { + b.HasOne("CodingEventsDemo.Models.EventCategory", "Category") + .WithMany("events") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Navigation("events"); + }); #pragma warning restore 612, 618 } } diff --git a/CodingEventsDemo/ViewModels/AddEventViewModel.cs b/CodingEventsDemo/ViewModels/AddEventViewModel.cs index 846e7563..33fe73a0 100644 --- a/CodingEventsDemo/ViewModels/AddEventViewModel.cs +++ b/CodingEventsDemo/ViewModels/AddEventViewModel.cs @@ -36,7 +36,7 @@ public AddEventViewModel(List categories) Value = category.Id.ToString(), Text = category.Name } - ); ; + ); } } public AddEventViewModel() diff --git a/CodingEventsDemo/Views/Events/Add.cshtml b/CodingEventsDemo/Views/Events/Add.cshtml index 7c56b8ab..a04fe957 100644 --- a/CodingEventsDemo/Views/Events/Add.cshtml +++ b/CodingEventsDemo/Views/Events/Add.cshtml @@ -18,7 +18,7 @@
- +
diff --git a/CodingEventsDemo/Views/Events/Index.cshtml b/CodingEventsDemo/Views/Events/Index.cshtml index 4dc26085..bbd6b3f6 100644 --- a/CodingEventsDemo/Views/Events/Index.cshtml +++ b/CodingEventsDemo/Views/Events/Index.cshtml @@ -32,7 +32,7 @@ else Contact Email - Event Type + Category @foreach (var evt in Model) @@ -42,7 +42,7 @@ else @evt.Name @evt.Description @evt.ContactEmail - @evt.Type + @evt.Category.Name } From 6c8c7c0274d7a0a4e641ffaef9640e58b6664533 Mon Sep 17 00:00:00 2001 From: saranell Date: Tue, 22 Nov 2022 15:27:55 -0600 Subject: [PATCH 07/11] coding events 5.3 --- .../20221121172651_MigrationsRedo.Designer.cs | 80 +++++++++++++++++++ .../20221121172651_MigrationsRedo.cs | 71 ++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.Designer.cs create mode 100644 CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.cs diff --git a/CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.Designer.cs b/CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.Designer.cs new file mode 100644 index 00000000..f3f556c7 --- /dev/null +++ b/CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.Designer.cs @@ -0,0 +1,80 @@ +// +using CodingEventsDemo.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CodingEventsDemo.Migrations +{ + [DbContext(typeof(EventDbContext))] + [Migration("20221121172651_MigrationsRedo")] + partial class MigrationsRedo + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("ContactEmail") + .HasColumnType("longtext"); + + b.Property("Description") + .HasColumnType("longtext"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Events"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => + { + b.HasOne("CodingEventsDemo.Models.EventCategory", "Category") + .WithMany("events") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Navigation("events"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.cs b/CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.cs new file mode 100644 index 00000000..bea90c1a --- /dev/null +++ b/CodingEventsDemo/Migrations/20221121172651_MigrationsRedo.cs @@ -0,0 +1,71 @@ +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace CodingEventsDemo.Migrations +{ + public partial class MigrationsRedo : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterDatabase() + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "Categories", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + Name = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + }, + constraints: table => + { + table.PrimaryKey("PK_Categories", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "Events", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + Name = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + Description = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + ContactEmail = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + CategoryId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Events", x => x.Id); + table.ForeignKey( + name: "FK_Events_Categories_CategoryId", + column: x => x.CategoryId, + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_Events_CategoryId", + table: "Events", + column: "CategoryId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Events"); + + migrationBuilder.DropTable( + name: "Categories"); + } + } +} From b003b122466a321c49efaadf566ca73c152fc361 Mon Sep 17 00:00:00 2001 From: saranell Date: Tue, 22 Nov 2022 16:58:49 -0600 Subject: [PATCH 08/11] 6.1 code along --- .../Controllers/EventsController.cs | 11 +++++ CodingEventsDemo/Controllers/TagController.cs | 46 +++++++++++++++++++ CodingEventsDemo/Data/EventDbContext.cs | 1 + CodingEventsDemo/Models/Tag.cs | 25 ++++++++++ .../ViewModels/EventDetailViewModel.cs | 22 +++++++++ CodingEventsDemo/Views/Events/Detail.cshtml | 18 ++++++++ 6 files changed, 123 insertions(+) create mode 100644 CodingEventsDemo/Controllers/TagController.cs create mode 100644 CodingEventsDemo/Models/Tag.cs create mode 100644 CodingEventsDemo/ViewModels/EventDetailViewModel.cs create mode 100644 CodingEventsDemo/Views/Events/Detail.cshtml diff --git a/CodingEventsDemo/Controllers/EventsController.cs b/CodingEventsDemo/Controllers/EventsController.cs index a4f55c79..6e34e3bd 100644 --- a/CodingEventsDemo/Controllers/EventsController.cs +++ b/CodingEventsDemo/Controllers/EventsController.cs @@ -82,5 +82,16 @@ public IActionResult Delete(int[] eventIds) return Redirect("/Events"); } + + // /Events/Detail/X + public IActionResult Detail(int id) + { + Event theEvent = context.Events + .Include(e => e.Category) + .Single(e => e.Id == id); + + EventDetailViewModel viewModel = new EventDetailViewModel(theEvent); + return View(viewModel); + } } } diff --git a/CodingEventsDemo/Controllers/TagController.cs b/CodingEventsDemo/Controllers/TagController.cs new file mode 100644 index 00000000..83dfa2fd --- /dev/null +++ b/CodingEventsDemo/Controllers/TagController.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CodingEventsDemo.Data; +using CodingEventsDemo.Models; +using Microsoft.AspNetCore.Mvc; + +namespace CodingEventsDemo.Controllers +{ + public class TagController : Controller + { + private EventDbContext context; + + public TagController(EventDbContext dbContext) + { + context = dbContext; + } + + // GET: // + public IActionResult Index() + { + List tags = context.Tags.ToList(); + return View(tags); + } + + public IActionResult Add() + { + Tag tag = new Tag(); + return View(tag); + } + + [HttpPost] + public IActionResult Add(Tag tag) + { + if (ModelState.IsValid) + { + context.Tags.Add(tag); + context.SaveChanges(); + return Redirect("/Tag/"); + } + + return View("Add", tag); + } + } +} + diff --git a/CodingEventsDemo/Data/EventDbContext.cs b/CodingEventsDemo/Data/EventDbContext.cs index f6f10043..72aaa021 100644 --- a/CodingEventsDemo/Data/EventDbContext.cs +++ b/CodingEventsDemo/Data/EventDbContext.cs @@ -7,6 +7,7 @@ public class EventDbContext : DbContext { public DbSet Events { get; set; } public DbSet Categories { get; set; } + public DbSet Tags { get; set; } public EventDbContext(DbContextOptions options) : base(options) diff --git a/CodingEventsDemo/Models/Tag.cs b/CodingEventsDemo/Models/Tag.cs new file mode 100644 index 00000000..1f86c184 --- /dev/null +++ b/CodingEventsDemo/Models/Tag.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace CodingEventsDemo.Models +{ + public class Tag + { + public int Id { get; set; } + + [Required(ErrorMessage = "Name is required")] + [StringLength(50, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 50 characters long")] + public string Name { get; set; } + + public Tag(string name) + { + Name = name; + } + + public Tag() + { + } + } +} + diff --git a/CodingEventsDemo/ViewModels/EventDetailViewModel.cs b/CodingEventsDemo/ViewModels/EventDetailViewModel.cs new file mode 100644 index 00000000..ba3a0d43 --- /dev/null +++ b/CodingEventsDemo/ViewModels/EventDetailViewModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using CodingEventsDemo.Models; + +namespace CodingEventsDemo.ViewModels +{ + public class EventDetailViewModel + { + public string Name { get; set; } + public string Description { get; set; } + public string ContactEmail { get; set; } + public string CategoryName { get; set; } + + public EventDetailViewModel(Event theEvent) + { + Name = theEvent.Name; + Description = theEvent.Description; + ContactEmail = theEvent.ContactEmail; + CategoryName = theEvent.Category.Name; + } + } +} \ No newline at end of file diff --git a/CodingEventsDemo/Views/Events/Detail.cshtml b/CodingEventsDemo/Views/Events/Detail.cshtml new file mode 100644 index 00000000..e5acbbba --- /dev/null +++ b/CodingEventsDemo/Views/Events/Detail.cshtml @@ -0,0 +1,18 @@ +@model CodingEventsDemo.ViewModels.EventDetailViewModel + +

@Model.Name

+ + + + + + + + + + + + + + +
Description@Model.Description
Contact Email@Model.ContactEmail
Category@Model.CategoryName
\ No newline at end of file From d88cc18b98d049a05b2a2e0eadfffc87faa8929c Mon Sep 17 00:00:00 2001 From: saranell Date: Tue, 22 Nov 2022 17:45:32 -0600 Subject: [PATCH 09/11] added tag view --- CodingEventsDemo/CodingEventsDemo.csproj | 2 ++ CodingEventsDemo/Views/Tag/Add.cshtml | 12 ++++++++++++ CodingEventsDemo/Views/Tag/Index.cshtml | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 CodingEventsDemo/Views/Tag/Add.cshtml create mode 100644 CodingEventsDemo/Views/Tag/Index.cshtml diff --git a/CodingEventsDemo/CodingEventsDemo.csproj b/CodingEventsDemo/CodingEventsDemo.csproj index 3876c903..94a90a29 100644 --- a/CodingEventsDemo/CodingEventsDemo.csproj +++ b/CodingEventsDemo/CodingEventsDemo.csproj @@ -12,11 +12,13 @@ + + diff --git a/CodingEventsDemo/Views/Tag/Add.cshtml b/CodingEventsDemo/Views/Tag/Add.cshtml new file mode 100644 index 00000000..a42587e4 --- /dev/null +++ b/CodingEventsDemo/Views/Tag/Add.cshtml @@ -0,0 +1,12 @@ +@model CodingEventsDemo.Models.Tag + +

Add New Tag

+ +
+
+ + + +
+ +
\ No newline at end of file diff --git a/CodingEventsDemo/Views/Tag/Index.cshtml b/CodingEventsDemo/Views/Tag/Index.cshtml new file mode 100644 index 00000000..275569fa --- /dev/null +++ b/CodingEventsDemo/Views/Tag/Index.cshtml @@ -0,0 +1,24 @@ +@model List + +

All Event Tags

+ +@if (Model.Count == 0) +{ +

No tags yet!

+} +else +{ + + + + + @foreach (Tag tag in Model) + { + + + + + } +
Id + Tag Name
@tag.Id@tag.Name
+} \ No newline at end of file From 85db4aaf39a4e420b85bbf83b12bac5de190b0e6 Mon Sep 17 00:00:00 2001 From: saranell Date: Tue, 22 Nov 2022 17:55:53 -0600 Subject: [PATCH 10/11] 6.2 EventTag model --- CodingEventsDemo/Data/EventDbContext.cs | 6 + .../20221122235334_AddsEventTag.Designer.cs | 130 ++++++++++++++++++ .../Migrations/20221122235334_AddsEventTag.cs | 67 +++++++++ .../Migrations/EventDbContextModelSnapshot.cs | 50 +++++++ CodingEventsDemo/Models/EventTag.cs | 17 +++ 5 files changed, 270 insertions(+) create mode 100644 CodingEventsDemo/Migrations/20221122235334_AddsEventTag.Designer.cs create mode 100644 CodingEventsDemo/Migrations/20221122235334_AddsEventTag.cs create mode 100644 CodingEventsDemo/Models/EventTag.cs diff --git a/CodingEventsDemo/Data/EventDbContext.cs b/CodingEventsDemo/Data/EventDbContext.cs index 72aaa021..686dd4bf 100644 --- a/CodingEventsDemo/Data/EventDbContext.cs +++ b/CodingEventsDemo/Data/EventDbContext.cs @@ -8,10 +8,16 @@ public class EventDbContext : DbContext public DbSet Events { get; set; } public DbSet Categories { get; set; } public DbSet Tags { get; set; } + public DbSet EventTags { get; set; } public EventDbContext(DbContextOptions options) : base(options) { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(et => new { et.EventId, et.TagId }); + } } } diff --git a/CodingEventsDemo/Migrations/20221122235334_AddsEventTag.Designer.cs b/CodingEventsDemo/Migrations/20221122235334_AddsEventTag.Designer.cs new file mode 100644 index 00000000..ccfc9569 --- /dev/null +++ b/CodingEventsDemo/Migrations/20221122235334_AddsEventTag.Designer.cs @@ -0,0 +1,130 @@ +// +using CodingEventsDemo.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CodingEventsDemo.Migrations +{ + [DbContext(typeof(EventDbContext))] + [Migration("20221122235334_AddsEventTag")] + partial class AddsEventTag + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("ContactEmail") + .HasColumnType("longtext"); + + b.Property("Description") + .HasColumnType("longtext"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Events"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventTag", b => + { + b.Property("EventId") + .HasColumnType("int"); + + b.Property("TagId") + .HasColumnType("int"); + + b.HasKey("EventId", "TagId"); + + b.HasIndex("TagId"); + + b.ToTable("EventTags"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.Tag", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.HasKey("Id"); + + b.ToTable("Tags"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => + { + b.HasOne("CodingEventsDemo.Models.EventCategory", "Category") + .WithMany("events") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventTag", b => + { + b.HasOne("CodingEventsDemo.Models.Event", "Event") + .WithMany() + .HasForeignKey("EventId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodingEventsDemo.Models.Tag", "Tag") + .WithMany() + .HasForeignKey("TagId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Event"); + + b.Navigation("Tag"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => + { + b.Navigation("events"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CodingEventsDemo/Migrations/20221122235334_AddsEventTag.cs b/CodingEventsDemo/Migrations/20221122235334_AddsEventTag.cs new file mode 100644 index 00000000..5e914e37 --- /dev/null +++ b/CodingEventsDemo/Migrations/20221122235334_AddsEventTag.cs @@ -0,0 +1,67 @@ +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace CodingEventsDemo.Migrations +{ + public partial class AddsEventTag : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Tags", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + Name = table.Column(type: "varchar(50)", maxLength: 50, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4") + }, + constraints: table => + { + table.PrimaryKey("PK_Tags", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "EventTags", + columns: table => new + { + EventId = table.Column(type: "int", nullable: false), + TagId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_EventTags", x => new { x.EventId, x.TagId }); + table.ForeignKey( + name: "FK_EventTags_Events_EventId", + column: x => x.EventId, + principalTable: "Events", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_EventTags_Tags_TagId", + column: x => x.TagId, + principalTable: "Tags", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_EventTags_TagId", + table: "EventTags", + column: "TagId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "EventTags"); + + migrationBuilder.DropTable( + name: "Tags"); + } + } +} diff --git a/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs b/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs index 0efc66c7..f62063e9 100644 --- a/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs +++ b/CodingEventsDemo/Migrations/EventDbContextModelSnapshot.cs @@ -57,6 +57,37 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("Categories"); }); + modelBuilder.Entity("CodingEventsDemo.Models.EventTag", b => + { + b.Property("EventId") + .HasColumnType("int"); + + b.Property("TagId") + .HasColumnType("int"); + + b.HasKey("EventId", "TagId"); + + b.HasIndex("TagId"); + + b.ToTable("EventTags"); + }); + + modelBuilder.Entity("CodingEventsDemo.Models.Tag", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("varchar(50)"); + + b.HasKey("Id"); + + b.ToTable("Tags"); + }); + modelBuilder.Entity("CodingEventsDemo.Models.Event", b => { b.HasOne("CodingEventsDemo.Models.EventCategory", "Category") @@ -68,6 +99,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("Category"); }); + modelBuilder.Entity("CodingEventsDemo.Models.EventTag", b => + { + b.HasOne("CodingEventsDemo.Models.Event", "Event") + .WithMany() + .HasForeignKey("EventId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodingEventsDemo.Models.Tag", "Tag") + .WithMany() + .HasForeignKey("TagId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Event"); + + b.Navigation("Tag"); + }); + modelBuilder.Entity("CodingEventsDemo.Models.EventCategory", b => { b.Navigation("events"); diff --git a/CodingEventsDemo/Models/EventTag.cs b/CodingEventsDemo/Models/EventTag.cs new file mode 100644 index 00000000..a39705e7 --- /dev/null +++ b/CodingEventsDemo/Models/EventTag.cs @@ -0,0 +1,17 @@ +using System; +namespace CodingEventsDemo.Models +{ + public class EventTag + { + public int EventId { get; set; } + public Event Event { get; set; } + + public int TagId { get; set; } + public Tag Tag { get; set; } + + public EventTag() + { + } + } +} + From f2f45d209f0638a10e42f01d693edaa00d7745bc Mon Sep 17 00:00:00 2001 From: saranell Date: Wed, 23 Nov 2022 14:52:20 -0600 Subject: [PATCH 11/11] 6.3 adding a tag to an event --- CodingEventsDemo/Controllers/TagController.cs | 34 +++++++++++++++++ .../ViewModels/AddEventTagViewModel.cs | 37 +++++++++++++++++++ CodingEventsDemo/Views/Events/Index.cshtml | 2 +- CodingEventsDemo/Views/Tag/AddEvent.cshtml | 13 +++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 CodingEventsDemo/ViewModels/AddEventTagViewModel.cs create mode 100644 CodingEventsDemo/Views/Tag/AddEvent.cshtml diff --git a/CodingEventsDemo/Controllers/TagController.cs b/CodingEventsDemo/Controllers/TagController.cs index 83dfa2fd..1fb4ef56 100644 --- a/CodingEventsDemo/Controllers/TagController.cs +++ b/CodingEventsDemo/Controllers/TagController.cs @@ -3,6 +3,7 @@ using System.Linq; using CodingEventsDemo.Data; using CodingEventsDemo.Models; +using CodingEventsDemo.ViewModels; using Microsoft.AspNetCore.Mvc; namespace CodingEventsDemo.Controllers @@ -41,6 +42,39 @@ public IActionResult Add(Tag tag) return View("Add", tag); } + + public IActionResult AddEvent(int id) + { + Event theEvent = context.Events.Find(id); + List possibleTags = context.Tags.ToList(); + + AddEventTagViewModel viewModel = new AddEventTagViewModel(theEvent, possibleTags); + + return View(viewModel); + } + + [HttpPost] + public IActionResult AddEvent(AddEventTagViewModel viewModel) + { + if (ModelState.IsValid) + { + int eventId = viewModel.EventId; + int tagId = viewModel.TagId; + + EventTag eventTag = new EventTag + { + EventId = eventId, + TagId = tagId + }; + + context.EventTags.Add(eventTag); + context.SaveChanges(); + + return Redirect("/Events/Detail/" + eventId); + } + + return View(viewModel); + } } } diff --git a/CodingEventsDemo/ViewModels/AddEventTagViewModel.cs b/CodingEventsDemo/ViewModels/AddEventTagViewModel.cs new file mode 100644 index 00000000..7c618198 --- /dev/null +++ b/CodingEventsDemo/ViewModels/AddEventTagViewModel.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using CodingEventsDemo.Models; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace CodingEventsDemo.ViewModels +{ + public class AddEventTagViewModel + { + public int EventId { get; set; } + public Event Event { get; set; } + + public List Tags { get; set; } + + public int TagId { get; set; } + + public AddEventTagViewModel(Event theEvent, List possibleTags) + { + Tags = new List(); + foreach (var tag in possibleTags) + { + Tags.Add(new SelectListItem + { + Value = tag.Id.ToString(), + Text = tag.Name + }); + } + + Event = theEvent; + } + + public AddEventTagViewModel() + { + } + } +} + diff --git a/CodingEventsDemo/Views/Events/Index.cshtml b/CodingEventsDemo/Views/Events/Index.cshtml index bbd6b3f6..4d99091c 100644 --- a/CodingEventsDemo/Views/Events/Index.cshtml +++ b/CodingEventsDemo/Views/Events/Index.cshtml @@ -39,7 +39,7 @@ else { @evt.Id - @evt.Name + @evt.Name @evt.Description @evt.ContactEmail @evt.Category.Name diff --git a/CodingEventsDemo/Views/Tag/AddEvent.cshtml b/CodingEventsDemo/Views/Tag/AddEvent.cshtml new file mode 100644 index 00000000..d8d9162e --- /dev/null +++ b/CodingEventsDemo/Views/Tag/AddEvent.cshtml @@ -0,0 +1,13 @@ +@model CodingEventsDemo.ViewModels.AddEventTagViewModel + +

Add Tag to Event: @Model.Event.Name

+ +
+ +
+ + + +
+ +