diff --git a/ChristopherDemos/MusicStore/Controllers/ArtistsController.cs b/ChristopherDemos/MusicStore/Controllers/ArtistsController.cs index d327232..6d1a23b 100644 --- a/ChristopherDemos/MusicStore/Controllers/ArtistsController.cs +++ b/ChristopherDemos/MusicStore/Controllers/ArtistsController.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Data.Entity.Core; using System.Linq; +using System.Transactions; using System.Web; using System.Web.Mvc; using MusicStore.Models; @@ -43,5 +45,36 @@ public ActionResult Create(Artist artist) repository.SaveChanges(); return RedirectToAction("Index"); } + + public ActionResult Edit(int id) + { + var artist = repository.Get(id); + if(artist == null) return HttpNotFound(); + else return View(artist); + } + + [HttpPost()] + public ActionResult Edit(Artist artist) + { + if (!ModelState.IsValid) return View(artist); + + try { + using (TransactionScope scope = new TransactionScope()) { + repository.Update(artist); + repository.SaveChanges(); + scope.Complete(); + } + return RedirectToAction("Index"); + } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException) { + ViewBag.Message = "Sorry, that didn't work!
Adam beat you to the punch"; + return View(artist); + } + } + + protected override void Dispose(bool disposing) + { + repository.Dispose(); + base.Dispose(disposing); + } } } \ No newline at end of file diff --git a/ChristopherDemos/MusicStore/Models/Artist.cs b/ChristopherDemos/MusicStore/Models/Artist.cs index e9a16e0..5ad9ba7 100644 --- a/ChristopherDemos/MusicStore/Models/Artist.cs +++ b/ChristopherDemos/MusicStore/Models/Artist.cs @@ -16,6 +16,12 @@ public class Artist [StringLength(100, MinimumLength = 2)] public string Name { get; set; } + //[Timestamp()] + //public byte[] RowVersion { get; set; } + + [ConcurrencyCheck()] + public int Version { get; set; } + public virtual List Albums { get; set; } public virtual ArtistDetails ArtistDetails { get; set; } diff --git a/ChristopherDemos/MusicStore/Models/MusicStoreDataContext.cs b/ChristopherDemos/MusicStore/Models/MusicStoreDataContext.cs index dc58b0a..6296910 100644 --- a/ChristopherDemos/MusicStore/Models/MusicStoreDataContext.cs +++ b/ChristopherDemos/MusicStore/Models/MusicStoreDataContext.cs @@ -11,5 +11,7 @@ public class MusicStoreDataContext : DbContext { public DbSet Artists { get; set; } public DbSet Albums { get; set; } + + public System.Data.Entity.DbSet ArtistDetails { get; set; } } } diff --git a/ChristopherDemos/MusicStore/Models/Repositories/ArtistRepository.cs b/ChristopherDemos/MusicStore/Models/Repositories/ArtistRepository.cs index 4c8bd77..6057191 100644 --- a/ChristopherDemos/MusicStore/Models/Repositories/ArtistRepository.cs +++ b/ChristopherDemos/MusicStore/Models/Repositories/ArtistRepository.cs @@ -13,5 +13,18 @@ public List GetByName(String name) return DbSet.Where(a => a.Name.Contains(name)).ToList(); } + public override void Update(Artist entity) + { + base.Update(entity); + SaveChanges(); + entity.Version++; + base.Update(entity); + SaveChanges(); + } + + public List GetSoloArtists() + { + return DbSet.OfType().ToList(); + } } } diff --git a/ChristopherDemos/MusicStore/Models/Repositories/Repository.cs b/ChristopherDemos/MusicStore/Models/Repositories/Repository.cs index d7197dc..d281145 100644 --- a/ChristopherDemos/MusicStore/Models/Repositories/Repository.cs +++ b/ChristopherDemos/MusicStore/Models/Repositories/Repository.cs @@ -7,8 +7,9 @@ namespace MusicStore.Models.Repositories { - public class Repository where T : class + public class Repository : IDisposable where T : class { + private bool disposed = false; private MusicStoreDataContext context = null; protected DbSet DbSet @@ -41,9 +42,27 @@ public void Add(T entity) DbSet.Add(entity); } + public virtual void Update(T entity) + { + context.Entry(entity).State = EntityState.Modified; + } + + public void Delete(int id) + { + DbSet.Remove(DbSet.Find(id)); + } + public void SaveChanges() { context.SaveChanges(); } + + public void Dispose() + { + if(!disposed) { + context.Dispose(); + disposed = true; + } + } } } diff --git a/ChristopherDemos/MusicStore/MusicStore.csproj b/ChristopherDemos/MusicStore/MusicStore.csproj index df1bcc1..779a57a 100644 --- a/ChristopherDemos/MusicStore/MusicStore.csproj +++ b/ChristopherDemos/MusicStore/MusicStore.csproj @@ -43,6 +43,7 @@ + @@ -180,6 +181,7 @@ + @@ -240,6 +242,7 @@ + diff --git a/ChristopherDemos/MusicStore/ViewModels/AlbumCreateViewModel.cs b/ChristopherDemos/MusicStore/ViewModels/AlbumCreateViewModel.cs new file mode 100644 index 0000000..8109a41 --- /dev/null +++ b/ChristopherDemos/MusicStore/ViewModels/AlbumCreateViewModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MusicStore.ViewModels +{ + public class AlbumCreateViewModel + { + public string AlbumTitle { get; set; } + public string ArtistName { get; set; } + } +} diff --git a/ChristopherDemos/MusicStore/Views/Artists/Edit.cshtml b/ChristopherDemos/MusicStore/Views/Artists/Edit.cshtml new file mode 100644 index 0000000..3fd3f07 --- /dev/null +++ b/ChristopherDemos/MusicStore/Views/Artists/Edit.cshtml @@ -0,0 +1,49 @@ +@model MusicStore.Models.Artist + +@{ + ViewBag.Title = "Edit"; +} + + + +

Edit

+ +@if (!String.IsNullOrEmpty(ViewBag.Message)) { +

@ViewBag.Message

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

Artist

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + @Html.HiddenFor(model => model.ArtistID) + +
+ @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) +
+
+ @Html.HiddenFor(model => model.Version) + + +
+
+ +
+
+
+} + +
+ @Html.ActionLink("Back to List", "Index") +
+ +@section Scripts { + @Scripts.Render("~/bundles/jqueryval") +} diff --git a/ChristopherDemos/MusicStore/Web.config b/ChristopherDemos/MusicStore/Web.config index 2ae0398..808ccc9 100644 --- a/ChristopherDemos/MusicStore/Web.config +++ b/ChristopherDemos/MusicStore/Web.config @@ -4,82 +4,82 @@ http://go.microsoft.com/fwlink/?LinkId=301880 --> - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file