Skip to content

Commit c8f5b1a

Browse files
author
C. Brown
committed
1 parent 95bf8db commit c8f5b1a

14 files changed

+319
-2
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using CheeseMVC.Data;
6+
using Microsoft.AspNetCore.Mvc;
7+
using CheeseMVC.Models;
8+
using CheeseMVC.ViewModels;
9+
10+
11+
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
12+
13+
namespace CheeseMVC.Controllers
14+
{
15+
public class MenuController : Controller
16+
{
17+
18+
private readonly CheeseDbContext context;
19+
20+
public MenuController(CheeseDbContext dbContext)
21+
{
22+
context = dbContext;
23+
}
24+
25+
26+
// GET: /<controller>/
27+
public IActionResult Index()
28+
{
29+
List<Menu> menus = context.Menus.ToList();
30+
31+
return View(menus);
32+
}
33+
public IActionResult Add()
34+
{
35+
ViewBag.Title = "New Menu";
36+
AddMenuViewModel addMenuViewModel = new AddMenuViewModel();
37+
38+
return View(addMenuViewModel);
39+
}
40+
[HttpPost]
41+
public IActionResult Add(AddCategoryViewModel addCategoryViewModel)
42+
{
43+
if (ModelState.IsValid)
44+
{
45+
Menu newMenu = new Menu
46+
{
47+
Name = addCategoryViewModel.Name
48+
};
49+
context.Menus.Add(newMenu);
50+
context.SaveChanges();
51+
return Redirect($"Menu/ViewMenu/{newMenu.ID}");
52+
}
53+
return View(addCategoryViewModel);
54+
}
55+
}
56+
}

src/CheeseMVC/Data/CheeseDbContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ public class CheeseDbContext : DbContext
88
{
99
public DbSet<CheeseCategory> Categories { get; set; }
1010
public DbSet<Cheese> Cheeses { get; set; }
11+
public DbSet<Menu> Menus{ get; set; }
12+
public DbSet<CheeseMenu> CheeseMenus { get; set; }
13+
1114

1215
public CheeseDbContext(DbContextOptions<CheeseDbContext> options)
1316
: base(options)
1417
{ }
1518

19+
protected override void OnModelCreating(ModelBuilder modelBuilder)
20+
{
21+
modelBuilder.Entity<CheeseMenu>()
22+
.HasKey(c => new { c.CheeseID, c.MenuID });
23+
}
24+
1625
}
1726
}

src/CheeseMVC/Migrations/20181210140202_DBInitialization.Designer.cs renamed to src/CheeseMVC/Migrations/20181211073303_DBInitialization.Designer.cs

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CheeseMVC/Migrations/20181210140202_DBInitialization.cs renamed to src/CheeseMVC/Migrations/20181211073303_DBInitialization.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ protected override void Up(MigrationBuilder migrationBuilder)
2222
table.PrimaryKey("PK_Categories", x => x.ID);
2323
});
2424

25+
migrationBuilder.CreateTable(
26+
name: "Menus",
27+
columns: table => new
28+
{
29+
ID = table.Column<int>(nullable: false)
30+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
31+
Name = table.Column<string>(nullable: true)
32+
},
33+
constraints: table =>
34+
{
35+
table.PrimaryKey("PK_Menus", x => x.ID);
36+
});
37+
2538
migrationBuilder.CreateTable(
2639
name: "Cheeses",
2740
columns: table => new
@@ -43,6 +56,35 @@ protected override void Up(MigrationBuilder migrationBuilder)
4356
onDelete: ReferentialAction.Cascade);
4457
});
4558

59+
migrationBuilder.CreateTable(
60+
name: "CheeseMenus",
61+
columns: table => new
62+
{
63+
CheeseID = table.Column<int>(nullable: false),
64+
MenuID = table.Column<int>(nullable: false)
65+
},
66+
constraints: table =>
67+
{
68+
table.PrimaryKey("PK_CheeseMenus", x => new { x.CheeseID, x.MenuID });
69+
table.ForeignKey(
70+
name: "FK_CheeseMenus_Cheeses_CheeseID",
71+
column: x => x.CheeseID,
72+
principalTable: "Cheeses",
73+
principalColumn: "ID",
74+
onDelete: ReferentialAction.Cascade);
75+
table.ForeignKey(
76+
name: "FK_CheeseMenus_Menus_MenuID",
77+
column: x => x.MenuID,
78+
principalTable: "Menus",
79+
principalColumn: "ID",
80+
onDelete: ReferentialAction.Cascade);
81+
});
82+
83+
migrationBuilder.CreateIndex(
84+
name: "IX_CheeseMenus_MenuID",
85+
table: "CheeseMenus",
86+
column: "MenuID");
87+
4688
migrationBuilder.CreateIndex(
4789
name: "IX_Cheeses_CategoryID",
4890
table: "Cheeses",
@@ -51,9 +93,15 @@ protected override void Up(MigrationBuilder migrationBuilder)
5193

5294
protected override void Down(MigrationBuilder migrationBuilder)
5395
{
96+
migrationBuilder.DropTable(
97+
name: "CheeseMenus");
98+
5499
migrationBuilder.DropTable(
55100
name: "Cheeses");
56101

102+
migrationBuilder.DropTable(
103+
name: "Menus");
104+
57105
migrationBuilder.DropTable(
58106
name: "Categories");
59107
}

src/CheeseMVC/Migrations/CheeseDbContextModelSnapshot.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,51 @@ protected override void BuildModel(ModelBuilder modelBuilder)
5050
b.ToTable("Categories");
5151
});
5252

53+
modelBuilder.Entity("CheeseMVC.Models.CheeseMenu", b =>
54+
{
55+
b.Property<int>("CheeseID");
56+
57+
b.Property<int>("MenuID");
58+
59+
b.HasKey("CheeseID", "MenuID");
60+
61+
b.HasIndex("MenuID");
62+
63+
b.ToTable("CheeseMenus");
64+
});
65+
66+
modelBuilder.Entity("CheeseMVC.Models.Menu", b =>
67+
{
68+
b.Property<int>("ID")
69+
.ValueGeneratedOnAdd();
70+
71+
b.Property<string>("Name");
72+
73+
b.HasKey("ID");
74+
75+
b.ToTable("Menus");
76+
});
77+
5378
modelBuilder.Entity("CheeseMVC.Models.Cheese", b =>
5479
{
5580
b.HasOne("CheeseMVC.Models.CheeseCategory", "Category")
5681
.WithMany("Cheeses")
5782
.HasForeignKey("CategoryID")
5883
.OnDelete(DeleteBehavior.Cascade);
5984
});
85+
86+
modelBuilder.Entity("CheeseMVC.Models.CheeseMenu", b =>
87+
{
88+
b.HasOne("CheeseMVC.Models.Cheese", "Cheese")
89+
.WithMany()
90+
.HasForeignKey("CheeseID")
91+
.OnDelete(DeleteBehavior.Cascade);
92+
93+
b.HasOne("CheeseMVC.Models.Menu", "Menu")
94+
.WithMany()
95+
.HasForeignKey("MenuID")
96+
.OnDelete(DeleteBehavior.Cascade);
97+
});
6098
#pragma warning restore 612, 618
6199
}
62100
}

src/CheeseMVC/Models/CheeseMenu.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace CheeseMVC.Models
7+
{
8+
public class CheeseMenu
9+
{
10+
public int MenuID { get; set; }
11+
public Menu Menu { get; set; }
12+
13+
public int CheeseID { get; set; }
14+
public Cheese Cheese { get; set; }
15+
16+
IList<CheeseMenu> CheeseMenus = new List<CheeseMenu>();
17+
}
18+
}

src/CheeseMVC/Models/Menu.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace CheeseMVC.Models
7+
{
8+
public class Menu
9+
{
10+
public int ID { get; set; }
11+
public string Name { get; set; }
12+
13+
IList<CheeseMenu> CheeseMenus = new List<CheeseMenu>();
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
8+
namespace CheeseMVC.ViewModels
9+
{
10+
public class AddMenuViewModel
11+
{
12+
[Required]
13+
[Display(Name="Menu Name")]
14+
public string Name { get; set; }
15+
16+
public AddMenuViewModel () { }
17+
}
18+
}
19+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using CheeseMVC.Models;
6+
7+
namespace CheeseMVC.ViewModels
8+
{
9+
public class ViewMenuViewModel
10+
{
11+
public Menu Menu { get; set; }
12+
public IList<CheeseMenu> CheeseMenu{ get; set; }
13+
}
14+
}

src/CheeseMVC/Views/Menu/Add.cshtml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@model CheeseMVC.ViewModels.AddMenuViewModel
2+
3+
4+
<h3> @ViewBag.Title</h3>
5+
6+
<br />
7+
<form asp-controller= "Menu" asp-action= "Add" method="post">
8+
9+
<label asp-for="Name"></label>
10+
<input asp-for= "Name" />
11+
<span asp-validation-for="Name"></span>
12+
13+
14+
<input type="submit" value=" Add Menu"/>
15+
</form>
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
8+
namespace CheeseMVC.Views.Menu
9+
{
10+
public class AddModel : PageModel
11+
{
12+
public void OnGet()
13+
{
14+
}
15+
}
16+
}

src/CheeseMVC/Views/Menu/Index.cshtml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@model IEnumerable<CheeseMVC.Models.Menu>
2+
3+
<h1>Menus</h1>
4+
5+
<ul>
6+
7+
@foreach(var menu in Model)
8+
{
9+
<li><a asp-controller= "Menu" asp-action= "ViewMenu" asp-route-id="@menu.ID">@menu.Name</a></li>
10+
}
11+
12+
</ul>
13+
14+
<p><a asp-controller="Menu" asp-action=" Add">Add Menu</a></p>

0 commit comments

Comments
 (0)