diff --git a/MvcMusicStore/Controllers/StoreManagerController.cs b/MvcMusicStore/Controllers/StoreManagerController.cs new file mode 100644 index 0000000..bf91417 --- /dev/null +++ b/MvcMusicStore/Controllers/StoreManagerController.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Entity; +using System.Linq; +using System.Net; +using System.Web; +using System.Web.Mvc; +using MvcMusicStore.Data; +using MvcMusicStore.Models; + +namespace MvcMusicStore.Controllers +{ + public class StoreManagerController : Controller + { + private MusicStoreDB db = new MusicStoreDB(); + + // GET: StoreManager + public ActionResult Index() + { + var albums = db.Albums.Include(a => a.Artist).Include(a => a.Genre); + return View(albums.ToList()); + } + + // GET: StoreManager/Details/5 + public ActionResult Details(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + Album album = db.Albums.Find(id); + if (album == null) + { + return HttpNotFound(); + } + return View(album); + } + + // GET: StoreManager/Create + public ActionResult Create() + { + ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name"); + ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name"); + return View(); + } + + // POST: StoreManager/Create + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) + { + if (ModelState.IsValid) + { + db.Albums.Add(album); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); + ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); + return View(album); + } + + // GET: StoreManager/Edit/5 + public ActionResult Edit(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + Album album = db.Albums.Find(id); + if (album == null) + { + return HttpNotFound(); + } + ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); + ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); + return View(album); + } + + // POST: StoreManager/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Edit([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) + { + if (ModelState.IsValid) + { + db.Entry(album).State = EntityState.Modified; + db.SaveChanges(); + return RedirectToAction("Index"); + } + ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); + ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); + return View(album); + } + + // GET: StoreManager/Delete/5 + public ActionResult Delete(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + Album album = db.Albums.Find(id); + if (album == null) + { + return HttpNotFound(); + } + return View(album); + } + + // POST: StoreManager/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public ActionResult DeleteConfirmed(int id) + { + Album album = db.Albums.Find(id); + db.Albums.Remove(album); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + db.Dispose(); + } + base.Dispose(disposing); + } + } +} diff --git a/MvcMusicStore/Data/MusicStoreDB.cs b/MvcMusicStore/Data/MusicStoreDB.cs new file mode 100644 index 0000000..5d7491d --- /dev/null +++ b/MvcMusicStore/Data/MusicStoreDB.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Data.Entity; +using System.Linq; +using System.Web; + +namespace MvcMusicStore.Data +{ + public class MusicStoreDB : DbContext + { + // You can add custom code to this file. Changes will not be overwritten. + // + // If you want Entity Framework to drop and regenerate your database + // automatically whenever you change your model schema, please use data migrations. + // For more information refer to the documentation: + // http://msdn.microsoft.com/en-us/data/jj591621.aspx + + public MusicStoreDB() : base("name=MusicStoreDB") + { + } + + public System.Data.Entity.DbSet Albums { get; set; } + + public System.Data.Entity.DbSet Artists { get; set; } + + public System.Data.Entity.DbSet Genres { get; set; } + } +} diff --git a/MvcMusicStore/MvcMusicStore.csproj b/MvcMusicStore/MvcMusicStore.csproj index c11e3a3..c5a85f9 100644 --- a/MvcMusicStore/MvcMusicStore.csproj +++ b/MvcMusicStore/MvcMusicStore.csproj @@ -45,6 +45,12 @@ 4 + + packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll + + + packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll + @@ -122,6 +128,8 @@ + + Global.asax @@ -166,6 +174,11 @@ + + + + + diff --git a/MvcMusicStore/Views/StoreManager/Create.cshtml b/MvcMusicStore/Views/StoreManager/Create.cshtml new file mode 100644 index 0000000..7d72a18 --- /dev/null +++ b/MvcMusicStore/Views/StoreManager/Create.cshtml @@ -0,0 +1,67 @@ +@model MvcMusicStore.Models.Album + +@{ + ViewBag.Title = "Create"; +} + +

Create

+ +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
+

Album

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) +
+ @Html.LabelFor(model => model.GenreId, "GenreId", htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DropDownList("GenreId", null, htmlAttributes: new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.GenreId, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.ArtistId, "ArtistId", htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DropDownList("ArtistId", null, htmlAttributes: new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.ArtistId, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.AlbumArtUrl, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.AlbumArtUrl, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.AlbumArtUrl, "", new { @class = "text-danger" }) +
+
+ +
+
+ +
+
+
+} + +
+ @Html.ActionLink("Back to List", "Index") +
diff --git a/MvcMusicStore/Views/StoreManager/Delete.cshtml b/MvcMusicStore/Views/StoreManager/Delete.cshtml new file mode 100644 index 0000000..a512248 --- /dev/null +++ b/MvcMusicStore/Views/StoreManager/Delete.cshtml @@ -0,0 +1,64 @@ +@model MvcMusicStore.Models.Album + +@{ + ViewBag.Title = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

Album

+
+
+
+ @Html.DisplayNameFor(model => model.Artist.Name) +
+ +
+ @Html.DisplayFor(model => model.Artist.Name) +
+ +
+ @Html.DisplayNameFor(model => model.Genre.Name) +
+ +
+ @Html.DisplayFor(model => model.Genre.Name) +
+ +
+ @Html.DisplayNameFor(model => model.Title) +
+ +
+ @Html.DisplayFor(model => model.Title) +
+ +
+ @Html.DisplayNameFor(model => model.Price) +
+ +
+ @Html.DisplayFor(model => model.Price) +
+ +
+ @Html.DisplayNameFor(model => model.AlbumArtUrl) +
+ +
+ @Html.DisplayFor(model => model.AlbumArtUrl) +
+ +
+ + @using (Html.BeginForm()) { + @Html.AntiForgeryToken() + +
+ | + @Html.ActionLink("Back to List", "Index") +
+ } +
diff --git a/MvcMusicStore/Views/StoreManager/Details.cshtml b/MvcMusicStore/Views/StoreManager/Details.cshtml new file mode 100644 index 0000000..448e076 --- /dev/null +++ b/MvcMusicStore/Views/StoreManager/Details.cshtml @@ -0,0 +1,58 @@ +@model MvcMusicStore.Models.Album + +@{ + ViewBag.Title = "Details"; +} + +

Details

+ +
+

Album

+
+
+
+ @Html.DisplayNameFor(model => model.Artist.Name) +
+ +
+ @Html.DisplayFor(model => model.Artist.Name) +
+ +
+ @Html.DisplayNameFor(model => model.Genre.Name) +
+ +
+ @Html.DisplayFor(model => model.Genre.Name) +
+ +
+ @Html.DisplayNameFor(model => model.Title) +
+ +
+ @Html.DisplayFor(model => model.Title) +
+ +
+ @Html.DisplayNameFor(model => model.Price) +
+ +
+ @Html.DisplayFor(model => model.Price) +
+ +
+ @Html.DisplayNameFor(model => model.AlbumArtUrl) +
+ +
+ @Html.DisplayFor(model => model.AlbumArtUrl) +
+ +
+
+

+ @Html.ActionLink("Edit", "Edit", new { id = Model.AlbumId }) | + @Html.ActionLink("Back to List", "Index") +

diff --git a/MvcMusicStore/Views/StoreManager/Edit.cshtml b/MvcMusicStore/Views/StoreManager/Edit.cshtml new file mode 100644 index 0000000..6edfb94 --- /dev/null +++ b/MvcMusicStore/Views/StoreManager/Edit.cshtml @@ -0,0 +1,69 @@ +@model MvcMusicStore.Models.Album + +@{ + ViewBag.Title = "Edit"; +} + +

Edit

+ +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
+

Album

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + @Html.HiddenFor(model => model.AlbumId) + +
+ @Html.LabelFor(model => model.GenreId, "GenreId", htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DropDownList("GenreId", null, htmlAttributes: new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.GenreId, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.ArtistId, "ArtistId", htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.DropDownList("ArtistId", null, htmlAttributes: new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.ArtistId, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.AlbumArtUrl, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.AlbumArtUrl, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.AlbumArtUrl, "", new { @class = "text-danger" }) +
+
+ +
+
+ +
+
+
+} + +
+ @Html.ActionLink("Back to List", "Index") +
diff --git a/MvcMusicStore/Views/StoreManager/Index.cshtml b/MvcMusicStore/Views/StoreManager/Index.cshtml new file mode 100644 index 0000000..61dc72e --- /dev/null +++ b/MvcMusicStore/Views/StoreManager/Index.cshtml @@ -0,0 +1,57 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Index"; +} + +

Index

+ +

+ @Html.ActionLink("Create New", "Create") +

+ + + + + + + + + + +@foreach (var item in Model) { + + + + + + + + +} + +
+ @Html.DisplayNameFor(model => model.Artist.Name) + + @Html.DisplayNameFor(model => model.Genre.Name) + + @Html.DisplayNameFor(model => model.Title) + + @Html.DisplayNameFor(model => model.Price) + + @Html.DisplayNameFor(model => model.AlbumArtUrl) +
+ @Html.DisplayFor(modelItem => item.Artist.Name) + + @Html.DisplayFor(modelItem => item.Genre.Name) + + @Html.DisplayFor(modelItem => item.Title) + + @Html.DisplayFor(modelItem => item.Price) + + @Html.DisplayFor(modelItem => item.AlbumArtUrl) + + @Html.ActionLink("Edit", "Edit", new { id=item.AlbumId }) | + @Html.ActionLink("Details", "Details", new { id=item.AlbumId }) | + @Html.ActionLink("Delete", "Delete", new { id=item.AlbumId }) +
diff --git a/MvcMusicStore/Web.config b/MvcMusicStore/Web.config index d246aab..fb176c9 100644 --- a/MvcMusicStore/Web.config +++ b/MvcMusicStore/Web.config @@ -1,9 +1,17 @@ - + + + +
+ + + + @@ -30,7 +38,7 @@ - + @@ -52,4 +60,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/MvcMusicStore/packages.config b/MvcMusicStore/packages.config index 40f85c9..4c54175 100644 --- a/MvcMusicStore/packages.config +++ b/MvcMusicStore/packages.config @@ -2,6 +2,7 @@ +