Skip to content

Commit

Permalink
Module 5
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekTrainer committed Jan 28, 2015
1 parent ee39a96 commit d9fade1
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 79 deletions.
33 changes: 33 additions & 0 deletions ChristopherDemos/MusicStore/Controllers/ArtistsController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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!<br />Adam beat you to the punch";
return View(artist);
}
}

protected override void Dispose(bool disposing)
{
repository.Dispose();
base.Dispose(disposing);
}
}
}
6 changes: 6 additions & 0 deletions ChristopherDemos/MusicStore/Models/Artist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Album> Albums { get; set; }
public virtual ArtistDetails ArtistDetails { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions ChristopherDemos/MusicStore/Models/MusicStoreDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class MusicStoreDataContext : DbContext
{
public DbSet<Artist> Artists { get; set; }
public DbSet<Album> Albums { get; set; }

public System.Data.Entity.DbSet<MusicStore.Models.ArtistDetails> ArtistDetails { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,18 @@ public List<Artist> 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<SoloArtist> GetSoloArtists()
{
return DbSet.OfType<SoloArtist>().ToList();
}
}
}
21 changes: 20 additions & 1 deletion ChristopherDemos/MusicStore/Models/Repositories/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

namespace MusicStore.Models.Repositories
{
public class Repository<T> where T : class
public class Repository<T> : IDisposable where T : class
{
private bool disposed = false;
private MusicStoreDataContext context = null;

protected DbSet<T> DbSet
Expand Down Expand Up @@ -41,9 +42,27 @@ public void Add(T entity)
DbSet.Add(entity);
}

public virtual void Update(T entity)
{
context.Entry<T>(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;
}
}
}
}
3 changes: 3 additions & 0 deletions ChristopherDemos/MusicStore/MusicStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Transactions" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
Expand Down Expand Up @@ -180,6 +181,7 @@
<Compile Include="Models\SoloArtist.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
<Compile Include="ViewModels\AlbumCreateViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\bootstrap.css" />
Expand Down Expand Up @@ -240,6 +242,7 @@
<Content Include="Views\Artists\Index.cshtml" />
<Content Include="Views\Artists\Create.cshtml" />
<Content Include="Views\Artists\Details.cshtml" />
<Content Include="Views\Artists\Edit.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
Expand Down
14 changes: 14 additions & 0 deletions ChristopherDemos/MusicStore/ViewModels/AlbumCreateViewModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
49 changes: 49 additions & 0 deletions ChristopherDemos/MusicStore/Views/Artists/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@model MusicStore.Models.Artist

@{
ViewBag.Title = "Edit";
}



<h2>Edit</h2>

@if (!String.IsNullOrEmpty(ViewBag.Message)) {
<h3 style="color:red">@ViewBag.Message</h3>
}

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

<div class="form-horizontal">
<h4>Artist</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ArtistID)

<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
@Html.HiddenFor(model => model.Version)


<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
156 changes: 78 additions & 78 deletions ChristopherDemos/MusicStore/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,82 @@
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MusicStore-20150128102003.mdf;Initial Catalog=aspnet-MusicStore-20150128102003;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MusicStore-20150128102003.mdf;Initial Catalog=aspnet-MusicStore-20150128102003;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

0 comments on commit d9fade1

Please sign in to comment.