Drown your issues in Kool Aid (Updating K Music Store app to use EF/E API surface)

This updates the K version of Music Store to use the updated Entity Framework API surface from the data repro. The changes made previously to the models and controllers in the full .NET version are also included here to bring both versions into line. The code is still non-functional as it was before.
This commit is contained in:
ajcvickers 2014-02-12 09:57:19 -08:00
parent 939f363a0e
commit d1ac4298c2
22 changed files with 4117 additions and 917 deletions

View File

@ -0,0 +1,68 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=EntityFramework/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="EntityFramework"&gt;&lt;HtmlReformatCode&gt;True&lt;/HtmlReformatCode&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_IMPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;ALWAYS_IMPLICIT&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_IMPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;XMLReformatCode&gt;True&lt;/XMLReformatCode&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">EntityFramework</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">EntityFramework</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTILINE_ARGUMENT/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTILINE_CALLS_CHAIN/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTILINE_PARAMETER/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_ATTRIBUTE_STYLE/@EntryValue">SEPARATE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_CHOP_COMPOUND_DO_EXPRESSION/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_CHOP_COMPOUND_IF_EXPRESSION/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_CHOP_COMPOUND_WHILE_EXPRESSION/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_FIXED_BRACES_STYLE/@EntryValue">ALWAYS_ADD</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_FOR_BRACES_STYLE/@EntryValue">ALWAYS_ADD</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_FOREACH_BRACES_STYLE/@EntryValue">ALWAYS_ADD</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_IFELSE_BRACES_STYLE/@EntryValue">ALWAYS_ADD</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_USING_BRACES_STYLE/@EntryValue">ALWAYS_ADD</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_WHILE_BRACES_STYLE/@EntryValue">ALWAYS_ADD</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_NESTED_USINGS_STMT/@EntryValue">True</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CONSTRUCTOR_INITIALIZER_ON_SAME_LINE/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_LINQ_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_WHILE_ON_NEW_LINE/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SIMPLE_EMBEDDED_STATEMENT_STYLE/@EntryValue">LINE_BREAK</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AROUND_MULTIPLICATIVE_OP/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_DECLARATION_LPAR/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_INVOCATION_LPAR/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_BEFORE_BINARY_OPSIGN/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_BEFORE_FIRST_TYPE_PARAMETER_CONSTRAINT/@EntryValue">True</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">140</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_MULTIPLE_TYPE_PARAMEER_CONSTRAINTS_STYLE/@EntryValue">CHOP_ALWAYS</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/JavaScriptCodeFormatting/ALIGN_MULTIPLE_DECLARATION/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/JavaScriptCodeFormatting/JavaScriptFormatOther/ALIGN_MULTIPLE_DECLARATION/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/XmlFormatter/WrapBeforeAttr/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/XmlFormatter/WrapInsideText/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/QualifiedUsingAtNestedScope/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Constructor/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Constructor/Options/=XmlDocumentation/@EntryIndexedValue">True</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Equality/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Equality/Options/=ChangeEquals/@EntryIndexedValue">Side by side</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Equality/Options/=ChangeGetHashCode/@EntryIndexedValue">Side by side</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Equality/Options/=EqualityOperators/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Equality/Options/=ImplementIEquatable/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Equality/Options/=XmlDocumentation/@EntryIndexedValue">False</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Implementations/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Implementations/Options/=WrapInRegion/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Implementations/Options/=XmlDocumentation/@EntryIndexedValue">False</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Overrides/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Overrides/Options/=WrapInRegion/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Overrides/Options/=XmlDocumentation/@EntryIndexedValue">False</s:String>
<s:Boolean x:Key="/Default/CodeStyle/IntroduceVariableUseVar/UseVarForIntroduceVariableRefactoring/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/EventHandlerPatternLong/@EntryValue">$object$_On$event$</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/VBNaming/EventHandlerPatternLong/@EntryValue">$object$_On$event$</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsCodeFormatterSettingsUpgrader/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -1,87 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using MvcMusicStore.Models;
namespace MvcMusicStore.Controllers
{
// [Authorize]
//[Authorize]
public class CheckoutController : Controller
{
MusicStoreEntities storeDB = new MusicStoreEntities();
const string PromoCode = "FREE";
private const string PromoCode = "FREE";
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
//
// GET: /Checkout/
public IActionResult AddressAndPayment()
{
return View();
}
//
// POST: /Checkout/AddressAndPayment
// [HttpPost]
public IActionResult AddressAndPayment(IDictionary<string, string> values /*FormCollection values*/)
//[HttpPost]
public async Task<IActionResult> AddressAndPayment(IDictionary<string, string> values /*FormCollection values*/)
{
var order = new Order();
// TryUpdateModel(order);
//TryUpdateModel(order);
try
if (//ModelState.IsValid &&
string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(values["PromoCode"], PromoCode,
StringComparison.OrdinalIgnoreCase) == false)
{
return View(order);
}
else
{
// order.Username = User.Identity.Name;
order.OrderDate = DateTime.Now;
order.Username = "";//User.Identity.Name;
order.OrderDate = DateTime.Now;
//Add the Order
storeDB.Orders.Add(order);
_storeContext.Orders.Add(order);
//Process the order
var cart = ShoppingCart.GetCart(storeDB, this.Context);
cart.CreateOrder(order);
await ShoppingCart.GetCart(_storeContext, this).CreateOrder(order);
// Save all changes
storeDB.SaveChanges();
//return RedirectToAction("Complete",
// new { id = order.OrderId });
return null;
}
await _storeContext.SaveChangesAsync();
return null;//RedirectToAction("Complete", new { id = order.OrderId });
}
catch
{
//Invalid - redisplay with errors
return View(order);
}
return View(order);
}
//
// GET: /Checkout/Complete
public IActionResult Complete(int id)
public async Task<IActionResult> Complete(int id)
{
// Validate customer owns this order
bool isValid = storeDB.Orders.Any(
o => o.OrderId == id &&
o.Username == /*User.Identity.Name*/ null);
if (isValid)
{
return View(id);
}
else
{
return View("Error");
}
return await _storeContext.Orders.AnyAsync(o => o.OrderId == id && o.Username == "")//User.Identity.Name)
? View(id)
: View("Error");
}
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// _storeContext.Dispose();
// }
// base.Dispose(disposing);
//}
}
}

View File

@ -1,34 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using MvcMusicStore.Models;
namespace MvcMusicStore.Controllers
{
public class HomeController : Controller
{
private MusicStoreEntities storeDB = new MusicStoreEntities();
//
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
// GET: /Home/
public IActionResult Index()
public async Task<IActionResult> Index()
{
// Get most popular albums
var albums = GetTopSellingAlbums(6);
return View(albums);
}
private List<Album> GetTopSellingAlbums(int count)
{
// Group the order details by album and return
// the albums with the highest count
return storeDB.Albums
return View(await _storeContext.Albums
.OrderByDescending(a => a.OrderDetails.Count())
.Take(count)
.ToList();
.Take(6)
.ToListAsync());
}
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// _storeContext.Dispose();
// }
// base.Dispose(disposing);
//}
}
}

View File

@ -1,5 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using MvcMusicStore.Models;
using MvcMusicStore.ViewModels;
@ -7,76 +9,56 @@ namespace MvcMusicStore.Controllers
{
public class ShoppingCartController : Controller
{
MusicStoreEntities storeDB = new MusicStoreEntities();
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
//
// GET: /ShoppingCart/
public IActionResult Index()
public async Task<IActionResult> Index()
{
var cart = ShoppingCart.GetCart(storeDB, this.Context);
var cart = ShoppingCart.GetCart(_storeContext, this);
// Set up our ViewModel
var viewModel = new ShoppingCartViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal()
CartItems = await cart.GetCartItems().ToListAsync(),
CartTotal = await cart.GetTotal()
};
// Return the view
return View(viewModel);
}
//
// GET: /ShoppingCart/AddToCart/5
public IActionResult AddToCart(int id)
public async Task<IActionResult> AddToCart(int id)
{
var cart = ShoppingCart.GetCart(_storeContext, this);
// Retrieve the album from the database
var addedAlbum = storeDB.Albums
.Single(album => album.AlbumId == id);
await cart.AddToCart(await _storeContext.Albums.SingleAsync(a => a.AlbumId == id));
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(storeDB, this.Context);
await _storeContext.SaveChangesAsync();
cart.AddToCart(addedAlbum);
storeDB.SaveChanges();
// Go back to the main store page for more shopping
// return RedirectToAction("Index");
return null;
return null;//RedirectToAction("Index");
}
//
// AJAX: /ShoppingCart/RemoveFromCart/5
// [HttpPost]
public IActionResult RemoveFromCart(int id)
//[HttpPost]
public async Task<IActionResult> RemoveFromCart(int id)
{
// Retrieve the current user's shopping cart
var cart = ShoppingCart.GetCart(storeDB, this.Context);
var cart = ShoppingCart.GetCart(_storeContext, this);
// Get the name of the album to display confirmation
string albumName = storeDB.Carts
.Single(item => item.RecordId == id).Album.Title;
var albumName = await _storeContext.Carts
.Where(i => i.RecordId == id)
.Select(i => i.Album.Title)
.SingleOrDefaultAsync();
// Remove from cart
int itemCount = cart.RemoveFromCart(id);
var itemCount = await cart.RemoveFromCart(id);
storeDB.SaveChanges();
await _storeContext.SaveChangesAsync();
string removed = (itemCount > 0) ? " 1 copy of " : string.Empty;
// Display the confirmation message
var removed = (itemCount > 0) ? " 1 copy of " : string.Empty;
var results = new ShoppingCartRemoveViewModel
{
Message = removed + albumName +
" has been removed from your shopping cart.",
CartTotal = cart.GetTotal(),
CartCount = cart.GetCount(),
Message = removed + albumName + " has been removed from your shopping cart.",
CartTotal = await cart.GetTotal(),
CartCount = await cart.GetCount(),
ItemCount = itemCount,
DeleteId = id
};
@ -84,20 +66,29 @@ namespace MvcMusicStore.Controllers
return Result.Json(results);
}
// [ChildActionOnly]
//[ChildActionOnly]
public IActionResult CartSummary()
{
var cart = ShoppingCart.GetCart(storeDB, this.Context);
var cart = ShoppingCart.GetCart(_storeContext, this);
var cartItems = cart.GetCartItems()
.Select(a => a.Album.Title)
.OrderBy(x => x);
.OrderBy(x => x)
.ToList();
// ViewBag.CartCount = cartItems.Count();
// ViewBag.CartSummary = string.Join("\n", cartItems.Distinct());
ViewBag.CartCount = cartItems.Count();
ViewBag.CartSummary = string.Join("\n", cartItems.Distinct());
// return PartialView("CartSummary");
return null;
return null;//PartialView("CartSummary");
}
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// _storeContext.Dispose();
// }
// base.Dispose(disposing);
//}
}
}

View File

@ -1,58 +1,55 @@
using MvcMusicStore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using MvcMusicStore.Models;
namespace MvcMusicStore.Controllers
{
public class StoreController : Controller
{
MusicStoreEntities storeDB = new MusicStoreEntities();
//
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
// GET: /Store/
public IActionResult Index()
public async Task<IActionResult> Index()
{
var genres = storeDB.Genres.ToList();
return View(genres);
return View(await _storeContext.Genres.ToListAsync());
}
//
// GET: /Store/Browse?genre=Disco
public IActionResult Browse(string genre)
public async Task<IActionResult> Browse(string genre)
{
// Retrieve Genre genre and its Associated associated Albums albums from database
var genreModel = storeDB.Genres.Include("Albums")
.Single(g => g.Name == genre);
return View(genreModel);
return View(await _storeContext.Genres.Include(e => e.Albums).SingleAsync(g => g.Name == genre));
}
public IActionResult Details(int id)
public async Task<IActionResult> Details(int id)
{
var album = storeDB.Albums.Find(id);
var album = await _storeContext.Albums.SingleOrDefaultAsync(a => a.AlbumId == id);
return View(album);
return album != null ? View(album) : (IActionResult)null;//HttpNotFound();
}
// [ChildActionOnly]
//[ChildActionOnly]
public IActionResult GenreMenu()
{
var genres = storeDB.Genres
var genres = _storeContext.Genres
.OrderByDescending(
g => g.Albums.Sum(
a => a.OrderDetails.Sum(
od => od.Quantity)))
a => a.OrderDetails.Sum(
od => od.Quantity)))
.Take(9)
.ToList();
//return PartialView(genres);
return null;
return null; //PartialView(genres);
}
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// _storeContext.Dispose();
// }
// base.Dispose(disposing);
//}
}
}

View File

@ -1,133 +1,141 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using MvcMusicStore.Models;
namespace MvcMusicStore.Controllers
{
// [Authorize(Roles = "Administrator")]
//[Authorize(Roles = "Administrator")]
public class StoreManagerController : Controller
{
private MusicStoreEntities db = new MusicStoreEntities();
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
//
// GET: /StoreManager/
public IActionResult Index()
public async Task<IActionResult> Index()
{
var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist)
.OrderBy(a => a.Price);
return View(albums.ToList());
return View(await _storeContext.Albums
.Include(a => a.Genre)
.Include(a => a.Artist)
.OrderBy(a => a.Price).ToListAsync());
}
//
// GET: /StoreManager/Details/5
public IActionResult Details(int id = 0)
public async Task<IActionResult> Details(int id = 0)
{
Album album = db.Albums.Find(id);
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
if (album == null)
{
//return HttpNotFound();
return null;
return null;//HttpNotFound();
}
return View(album);
}
//
// GET: /StoreManager/Create
public IActionResult Create()
public async Task<IActionResult> Create()
{
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
return View();
return await BuildView(null);
}
//
// POST: /StoreManager/Create
// [HttpPost]
public IActionResult Create(Album album)
//[HttpPost]
public async Task<IActionResult> Create(Album album)
{
if (/*ModelState.IsValid*/true)
if (true)//ModelState.IsValid)
{
db.Albums.Add(album);
db.SaveChanges();
// return RedirectToAction("Index");
return null;
_storeContext.Albums.Add(album);
await _storeContext.SaveChangesAsync();
return null;//RedirectToAction("Index");
}
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
return await BuildView(album);
}
//
// GET: /StoreManager/Edit/5
public IActionResult Edit(int id = 0)
public async Task<IActionResult> Edit(int id = 0)
{
Album album = db.Albums.Find(id);
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
if (album == null)
{
// return HttpNotFound();
return null;
return null;//HttpNotFound();
}
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
return await BuildView(album);
}
//
// POST: /StoreManager/Edit/5
// [HttpPost]
public IActionResult Edit(Album album)
//[HttpPost]
public async Task<IActionResult> Edit(Album album)
{
//if (ModelState.IsValid)
//{
// db.Entry(album).State = EntityState.Modified;
// db.SaveChanges();
// return RedirectToAction("Index");
//}
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
return View(album);
if (true)//ModelState.IsValid)
{
_storeContext.Albums.Update(album);
await _storeContext.SaveChangesAsync();
return null;//RedirectToAction("Index");
}
return await BuildView(album);
}
//
// GET: /StoreManager/Delete/5
public IActionResult Delete(int id = 0)
public async Task<IActionResult> Delete(int id = 0)
{
Album album = db.Albums.Find(id);
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
if (album == null)
{
// return HttpNotFound();
return null;
return null;//HttpNotFound();
}
return View(album);
}
//
// POST: /StoreManager/Delete/5
// [HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
//[HttpPost, ActionName("Delete")]
public async Task<IActionResult> DeleteConfirmed(int id)
{
Album album = db.Albums.Find(id);
db.Albums.Remove(album);
db.SaveChanges();
// return RedirectToAction("Index");
return null;
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
if (album == null)
{
return null;//HttpNotFound();
}
_storeContext.Albums.Remove(album);
await _storeContext.SaveChangesAsync();
return null;//RedirectToAction("Index");
}
protected /*override*/ void Dispose(bool disposing)
private async Task<IActionResult> BuildView(Album album)
{
db.Dispose();
// base.Dispose(disposing);
//ViewBag.GenreId = new SelectList(
// await _storeContext.Genres.ToListAsync(),
// "GenreId",
// "Name",
// album == null ? null : (object)album.GenreId);
//ViewBag.ArtistId = new SelectList(
// await _storeContext.Artists.ToListAsync(),
// "ArtistId",
// "Name",
// album == null ? null : (object)album.ArtistId);
return View(album);
}
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// _storeContext.Dispose();
// }
// base.Dispose(disposing);
//}
}
}

View File

@ -1,26 +1,22 @@
using System.Collections.Generic;
using System.ComponentModel;
// using System.ComponentModel.DataAnnotations;
namespace MvcMusicStore.Models
{
public class Album
{
//[ScaffoldColumn(false)]
public int AlbumId { get; set; }
public int GenreId { get; set; }
public int ArtistId { get; set; }
// [Required]
// [StringLength(160, MinimumLength = 2)]
//[Required]
//[StringLength(160, MinimumLength = 2)]
public string Title { get; set; }
//[Required]
//[Range(0.01, 100.00)]
//[DataType(DataType.Currency)]
public decimal Price { get; set; }

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace MvcMusicStore.Models
namespace MvcMusicStore.Models
{
public class Artist
{

View File

@ -1,19 +1,18 @@
using System;
//using System.ComponentModel.DataAnnotations;
namespace MvcMusicStore.Models
{
public class Cart
{
//[Key]
public int RecordId { get; set; }
public int RecordId { get; set; }
public string CartId { get; set; }
public int AlbumId { get; set; }
public int Count { get; set; }
public int AlbumId { get; set; }
public int Count { get; set; }
//[DataType(DataType.DateTime)]
public DateTime DateCreated { get; set; }
public virtual Album Album { get; set; }
public virtual Album Album { get; set; }
}
}

View File

@ -2,11 +2,11 @@
namespace MvcMusicStore.Models
{
public class Genre
public class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
}

View File

@ -1,40 +1,18 @@
using System;
using System.Collections.Generic;
using Microsoft.Data.Entity;
namespace MvcMusicStore.Models
{
//public class MusicStoreEntities : DbContext
//{
// public DbSet<Album> Albums { get; set; }
// public DbSet<Genre> Genres { get; set; }
// public DbSet<Artist> Artists { get; set; }
// public DbSet<Cart> Carts { get; set; }
// public DbSet<Order> Orders { get; set; }
// public DbSet<OrderDetail> OrderDetails { get; set; }
//}
public class MusicStoreEntities : IDisposable
public class MusicStoreEntities : EntityContext
{
public List<Album> Albums { get; set; }
public List<Genre> Genres { get; set; }
public List<Artist> Artists { get; set; }
public List<Cart> Carts { get; set; }
public List<Order> Orders { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
public void SaveChanges()
public MusicStoreEntities()
: base(null) // TODO: Fix after discussion of which patterns to use here
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
internal object Entry(Album album)
{
throw new NotImplementedException();
}
public EntitySet<Album> Albums { get; set; }
public EntitySet<Genre> Genres { get; set; }
public EntitySet<Artist> Artists { get; set; }
public EntitySet<Cart> Carts { get; set; }
public EntitySet<Order> Orders { get; set; }
}
}

View File

@ -1,12 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel;
//using System.ComponentModel.DataAnnotations;
namespace MvcMusicStore.Models
{
//[Bind(Include = "FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email")]
public class Order
{
public Order()
{
OrderDetails = new List<OrderDetail>();
}
//[ScaffoldColumn(false)]
public int OrderId { get; set; }
@ -59,7 +62,7 @@ namespace MvcMusicStore.Models
//[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// [ScaffoldColumn(false)]
//[ScaffoldColumn(false)]
public decimal Total { get; set; }
public List<OrderDetail> OrderDetails { get; set; }

File diff suppressed because it is too large Load Diff

View File

@ -1,196 +1,159 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
namespace MvcMusicStore.Models
{
public partial class ShoppingCart
public class ShoppingCart
{
MusicStoreEntities _db;
string ShoppingCartId { get; set; }
public ShoppingCart(MusicStoreEntities db)
{
_db = db;
}
public const string CartSessionKey = "CartId";
public static ShoppingCart GetCart(MusicStoreEntities db, HttpContext context)
private readonly MusicStoreEntities _storeContext;
private readonly string _cartId;
private ShoppingCart(MusicStoreEntities storeContext, string cartId)
{
var cart = new ShoppingCart(db);
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
_storeContext = storeContext;
_cartId = cartId;
}
// Helper method to simplify shopping cart calls
public static ShoppingCart GetCart(MusicStoreEntities db, Controller controller)
public static ShoppingCart GetCart(MusicStoreEntities storeContext, Controller controller)
{
return GetCart(db, controller.Context);
return new ShoppingCart(storeContext, GetCartId(controller.Context));
}
public void AddToCart(Album album)
private static string GetCartId(HttpContext context)
{
// Get the matching cart and album instances
var cartItem = _db.Carts.SingleOrDefault(
c => c.CartId == ShoppingCartId
&& c.AlbumId == album.AlbumId);
throw new NotImplementedException();
//if (context.Session[CartSessionKey] == null)
//{
// var username = context.User.Identity.Name;
// context.Session[CartSessionKey] = !string.IsNullOrWhiteSpace(username)
// ? username
// : Guid.NewGuid().ToString();
//}
//return context.Session[CartSessionKey].ToString();
}
public async Task AddToCart(Album album)
{
var cartItem = await GetCartItem(album.AlbumId);
if (cartItem == null)
{
// Create a new cart item if no cart item exists
cartItem = new Cart
{
AlbumId = album.AlbumId,
CartId = ShoppingCartId,
CartId = _cartId,
Count = 1,
DateCreated = DateTime.Now
};
_db.Carts.Add(cartItem);
_storeContext.Carts.Add(cartItem);
}
else
{
// If the item does exist in the cart, then add one to the quantity
cartItem.Count++;
}
}
public int RemoveFromCart(int id)
public async Task<int> RemoveFromCart(int id)
{
// Get the cart
var cartItem = _db.Carts.Single(
cart => cart.CartId == ShoppingCartId
&& cart.RecordId == id);
int itemCount = 0;
var cartItem = await GetCartItem(id);
if (cartItem != null)
{
if (cartItem.Count > 1)
{
cartItem.Count--;
itemCount = cartItem.Count;
}
else
{
_db.Carts.Remove(cartItem);
return --cartItem.Count;
}
_storeContext.Carts.Remove(cartItem);
}
return itemCount;
return 0;
}
public void EmptyCart()
private Task<Cart> GetCartItem(int albumId)
{
var cartItems = _db.Carts.Where(cart => cart.CartId == ShoppingCartId);
foreach (var cartItem in cartItems)
{
_db.Carts.Remove(cartItem);
}
return _storeContext.Carts.SingleOrDefaultAsync(
c => c.CartId == _cartId && c.AlbumId == albumId);
}
public List<Cart> GetCartItems()
public IQueryable<Cart> GetCartItems()
{
return _db.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList();
return _storeContext.Carts.Where(c => c.CartId == _cartId);
}
public int GetCount()
public Task<int> GetCount()
{
// Get the count of each item in the cart and sum them up
int? count = (from cartItems in _db.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count).Sum();
// Return 0 if all entries are null
return count ?? 0;
return _storeContext.Carts
.Where(c => c.CartId == _cartId)
.Select(c => c.Count)
.SumAsync();
}
public decimal GetTotal()
public Task<decimal> GetTotal()
{
// Multiply album price by count of that album to get
// the current price for each of those albums in the cart
// sum all album price totals to get the cart total
decimal? total = (from cartItems in _db.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count * cartItems.Album.Price).Sum();
return total ?? decimal.Zero;
return _storeContext.Carts
.Where(c => c.CartId == _cartId)
.Select(c => c.Count * c.Album.Price)
.SumAsync();
}
public int CreateOrder(Order order)
public async Task<int> CreateOrder(Order order)
{
decimal orderTotal = 0;
var cartItems = GetCartItems();
var cartItems = await _storeContext.Carts
.Where(c => c.CartId == _cartId)
.Include(c => c.Album)
.ToListAsync();
// Iterate over the items in the cart, adding the order details for each
foreach (var item in cartItems)
{
var album = _db.Albums.Find(item.AlbumId);
var orderDetail = new OrderDetail
order.OrderDetails.Add(new OrderDetail
{
AlbumId = item.AlbumId,
OrderId = order.OrderId,
UnitPrice = album.Price,
UnitPrice = item.Album.Price,
Quantity = item.Count,
};
});
// Set the order total of the shopping cart
orderTotal += (item.Count * item.Album.Price);
_db.OrderDetails.Add(orderDetail);
orderTotal += item.Count * item.Album.Price;
}
// Set the order's total to the orderTotal count
order.Total = orderTotal;
// Empty the shopping cart
EmptyCart();
await EmptyCart();
// Return the OrderId as the confirmation number
return order.OrderId;
}
// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContext context)
private async Task EmptyCart()
{
//if (context.Session[CartSessionKey] == null)
//{
// if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
// {
// context.Session[CartSessionKey] = context.User.Identity.Name;
// }
// else
// {
// // Generate a new random GUID using System.Guid class
// Guid tempCartId = Guid.NewGuid();
// // Send tempCartId back to client as a cookie
// context.Session[CartSessionKey] = tempCartId.ToString();
// }
//}
//return context.Session[CartSessionKey].ToString();
return Guid.NewGuid().ToString();
foreach (var cartItem in await _storeContext.Carts.Where(
c => c.CartId == _cartId).ToListAsync())
{
_storeContext.Carts.Remove(cartItem);
}
}
// When a user has logged in, migrate their shopping cart to
// be associated with their username
public void MigrateCart(string userName)
public async Task MigrateCart(string userName)
{
var shoppingCart = _db.Carts.Where(c => c.CartId == ShoppingCartId);
var carts = await _storeContext.Carts.Where(c => c.CartId == _cartId).ToListAsync();
foreach (Cart item in shoppingCart)
foreach (var item in carts)
{
item.CartId = userName;
}
await _storeContext.SaveChangesAsync();
}
}
}

View File

@ -1,8 +1,7 @@
using System;
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MvcMusicStore
{
@ -16,23 +15,4 @@ namespace MvcMusicStore
}
}
}
public static class ListPretendingToBeDbContextExtensions
{
// Mock DbSet (List<T>)
public static T Find<T>(this List<T> list, params object[] keys)
{
return default(T);
}
public static IEnumerable<T> Include<T>(this IEnumerable<T> list, string include)
{
return list;
}
public static IEnumerable<T> Include<T, A>(this IEnumerable<T> list, Func<T, A> projection)
{
return list;
}
}
}

View File

@ -1,11 +1,13 @@
namespace MvcMusicStore.ViewModels
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace MvcMusicStore.ViewModels
{
public class ShoppingCartRemoveViewModel
{
public string Message { get; set; }
public decimal CartTotal { get; set; }
public int CartCount { get; set; }
public int ItemCount { get; set; }
public int DeleteId { get; set; }
public string Message { get; set; }
public decimal CartTotal { get; set; }
public int CartCount { get; set; }
public int ItemCount { get; set; }
public int DeleteId { get; set; }
}
}

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using MvcMusicStore.Models;
namespace MvcMusicStore.ViewModels
@ -6,6 +8,6 @@ namespace MvcMusicStore.ViewModels
public class ShoppingCartViewModel
{
public List<Cart> CartItems { get; set; }
public decimal CartTotal { get; set; }
public decimal CartTotal { get; set; }
}
}
}

View File

@ -2,6 +2,16 @@
"dependencies": {
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.Mvc" : "0.1-alpha-*"
"Microsoft.AspNet.Mvc" : "0.1-alpha-*",
"Microsoft.Data.Entity" : "0.1-alpha-*"
},
"configurations": {
"net45": {
"dependencies": {
"System.Runtime" : "",
"System.Collections" : ""
}
},
"k10": {}
}
}

View File

@ -1,11 +1,8 @@
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using MvcMusicStore.Models;

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcMusicStore.Models
namespace MvcMusicStore.Models
{
public class Artist
{

View File

@ -2,11 +2,11 @@
namespace MvcMusicStore.Models
{
public class Genre
public class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;