Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Testing mb #24

Open
wants to merge 20 commits into
base: views-setup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion CodingEventsDemo/CodingEventsDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>



<ItemGroup>
<Folder Include="Views\Events\" />
<Folder Include="Data\" />
<Folder Include="ViewModels\" />
<Folder Include="Migrations\" />
</ItemGroup>
<ItemGroup>
<None Remove="ViewModels\" />
<None Remove="Pomelo.EntityFrameworkCore.MySql" />
<None Remove="Microsoft.EntityFrameworkCore.Relational" />
<None Remove="Microsoft.EntityFrameworkCore.Design" />
<None Remove="Migrations\" />
<None Remove="Microsoft.EntityFrameworkCore" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.11" />
</ItemGroup>
</Project>
57 changes: 51 additions & 6 deletions CodingEventsDemo/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodingEventsDemo.Data;
using CodingEventsDemo.Models;
using CodingEventsDemo.ViewModels;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
Expand All @@ -10,27 +13,69 @@ namespace coding_events_practice.Controllers
{
public class EventsController : Controller
{
//17.3
private EventDbContext context;

static private List<string> Events = new List<string>();
//17.3
public EventsController(EventDbContext dbContext)
{
context = dbContext;
}

// GET: /<controller>/
public IActionResult Index()
{
ViewBag.events = Events;
List<Event> events = context.Events.ToList();

return View();
return View(events);
}

public IActionResult Add()
{
return View();
AddEventViewModel addEventViewModel = new AddEventViewModel();
return View(addEventViewModel);
}

[HttpPost]
[Route("Events/Add")]
public IActionResult NewEvent(string name)
public IActionResult Add(AddEventViewModel addEventViewModel)
{
if (ModelState.IsValid)
{
Event newEvent = new Event
{
Name = addEventViewModel.Name,
Description = addEventViewModel.Description,
ContactEmail = addEventViewModel.ContactEmail,
Type = addEventViewModel.Type
};
//need to add the context
context.Events.Add(newEvent);
context.SaveChanges();

return Redirect("/Events");
}

return View(addEventViewModel);
}

public IActionResult Delete()
{
ViewBag.events = context.Events.ToList();

return View();
}

[HttpPost]
public IActionResult Delete(int[] eventIds)
{
Events.Add(name);
foreach (int eventId in eventIds)
{
Event theEvent = context.Events.Find(eventId);
context.Events.Remove(theEvent);
}

context.SaveChanges();

return Redirect("/Events");
}
Expand Down
41 changes: 41 additions & 0 deletions CodingEventsDemo/Data/EventData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using CodingEventsDemo.Models;

//namespace CodingEventsDemo.Data
//{
// public class EventData
// {
// //Events => Event
// static private Dictionary<int, Event> Events = new Dictionary<int, Event>();

// // GetAll
// public static IEnumerable<Event> GetAll()
// {
// return Events.Values;
// }

// // Add
// public static void Add(Event newEvent)
// {
// Events.Add(newEvent.ID, newEvent);
// Console.WriteLine(newEvent.ID);
// }

// // Remove
// public static void Remove(int id)
// {
// Events.Remove(id);
// }

// // GetById
// public static Event GetById(int id)
// {
// return Events[id];
// }
// }
//}


// ---- no longer needed once we get to 17.3
20 changes: 20 additions & 0 deletions CodingEventsDemo/Data/EventDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using CodingEventsDemo.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
//using static Google.Protobuf.Collections.MapField<TKey, TValue>;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
//created in 17.2

namespace CodingEventsDemo.Data
{
public class EventDbContext : DbContext
{
public DbSet<Event> Events { get; set; }

public EventDbContext(DbContextOptions<EventDbContext> options)
: base(options)
{
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions CodingEventsDemo/Migrations/20221109212821_AgainMigrating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CodingEventsDemo.Migrations
{
public partial class AgainMigrating : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");

migrationBuilder.CreateTable(
name: "Events",
columns: table => new
{
ID = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Description = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
ContactEmail = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Type = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Events", x => x.ID);
})
.Annotation("MySql:CharSet", "utf8mb4");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Events");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions CodingEventsDemo/Migrations/20221109215108_AgainMigrating4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CodingEventsDemo.Migrations
{
public partial class AgainMigrating4 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "ID",
table: "Events",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.OldAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "ID",
table: "Events",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
}
}
}
Loading