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:
parent
939f363a0e
commit
d1ac4298c2
|
|
@ -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"><?xml version="1.0" encoding="utf-16"?><Profile name="EntityFramework"><HtmlReformatCode>True</HtmlReformatCode><CSArrangeThisQualifier>True</CSArrangeThisQualifier><CSRemoveCodeRedundancies>True</CSRemoveCodeRedundancies><CSUseAutoProperty>True</CSUseAutoProperty><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSUseVar><BehavourStyle>CAN_CHANGE_TO_IMPLICIT</BehavourStyle><LocalVariableStyle>ALWAYS_IMPLICIT</LocalVariableStyle><ForeachVariableStyle>ALWAYS_IMPLICIT</ForeachVariableStyle></CSUseVar><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><XMLReformatCode>True</XMLReformatCode><CSUpdateFileHeader>True</CSUpdateFileHeader><CSharpFormatDocComments>True</CSharpFormatDocComments></Profile></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"><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></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>
|
||||||
|
|
@ -1,87 +1,65 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using MvcMusicStore.Models;
|
using MvcMusicStore.Models;
|
||||||
|
|
||||||
namespace MvcMusicStore.Controllers
|
namespace MvcMusicStore.Controllers
|
||||||
{
|
{
|
||||||
// [Authorize]
|
//[Authorize]
|
||||||
public class CheckoutController : Controller
|
public class CheckoutController : Controller
|
||||||
{
|
{
|
||||||
MusicStoreEntities storeDB = new MusicStoreEntities();
|
private const string PromoCode = "FREE";
|
||||||
const string PromoCode = "FREE";
|
|
||||||
|
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /Checkout/
|
// GET: /Checkout/
|
||||||
|
|
||||||
public IActionResult AddressAndPayment()
|
public IActionResult AddressAndPayment()
|
||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// POST: /Checkout/AddressAndPayment
|
// POST: /Checkout/AddressAndPayment
|
||||||
|
//[HttpPost]
|
||||||
// [HttpPost]
|
public async Task<IActionResult> AddressAndPayment(IDictionary<string, string> values /*FormCollection values*/)
|
||||||
public IActionResult AddressAndPayment(IDictionary<string, string> values /*FormCollection values*/)
|
|
||||||
{
|
{
|
||||||
var order = new Order();
|
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,
|
order.Username = "";//User.Identity.Name;
|
||||||
StringComparison.OrdinalIgnoreCase) == false)
|
order.OrderDate = DateTime.Now;
|
||||||
{
|
|
||||||
return View(order);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// order.Username = User.Identity.Name;
|
|
||||||
order.OrderDate = DateTime.Now;
|
|
||||||
|
|
||||||
//Add the Order
|
_storeContext.Orders.Add(order);
|
||||||
storeDB.Orders.Add(order);
|
|
||||||
|
|
||||||
//Process the order
|
await ShoppingCart.GetCart(_storeContext, this).CreateOrder(order);
|
||||||
var cart = ShoppingCart.GetCart(storeDB, this.Context);
|
|
||||||
cart.CreateOrder(order);
|
|
||||||
|
|
||||||
// Save all changes
|
await _storeContext.SaveChangesAsync();
|
||||||
storeDB.SaveChanges();
|
|
||||||
|
|
||||||
//return RedirectToAction("Complete",
|
|
||||||
// new { id = order.OrderId });
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return null;//RedirectToAction("Complete", new { id = order.OrderId });
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
return View(order);
|
||||||
//Invalid - redisplay with errors
|
|
||||||
return View(order);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /Checkout/Complete
|
// GET: /Checkout/Complete
|
||||||
|
public async Task<IActionResult> Complete(int id)
|
||||||
public IActionResult Complete(int id)
|
|
||||||
{
|
{
|
||||||
// Validate customer owns this order
|
return await _storeContext.Orders.AnyAsync(o => o.OrderId == id && o.Username == "")//User.Identity.Name)
|
||||||
bool isValid = storeDB.Orders.Any(
|
? View(id)
|
||||||
o => o.OrderId == id &&
|
: View("Error");
|
||||||
o.Username == /*User.Identity.Name*/ null);
|
|
||||||
|
|
||||||
if (isValid)
|
|
||||||
{
|
|
||||||
return View(id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return View("Error");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//protected override void Dispose(bool disposing)
|
||||||
|
//{
|
||||||
|
// if (disposing)
|
||||||
|
// {
|
||||||
|
// _storeContext.Dispose();
|
||||||
|
// }
|
||||||
|
// base.Dispose(disposing);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,34 +1,31 @@
|
||||||
using System.Collections.Generic;
|
using System.Linq;
|
||||||
using System.Linq;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using MvcMusicStore.Models;
|
using MvcMusicStore.Models;
|
||||||
|
|
||||||
namespace MvcMusicStore.Controllers
|
namespace MvcMusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
private MusicStoreEntities storeDB = new MusicStoreEntities();
|
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
|
||||||
//
|
|
||||||
// GET: /Home/
|
// GET: /Home/
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
public IActionResult Index()
|
|
||||||
{
|
{
|
||||||
// Get most popular albums
|
return View(await _storeContext.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
|
|
||||||
.OrderByDescending(a => a.OrderDetails.Count())
|
.OrderByDescending(a => a.OrderDetails.Count())
|
||||||
.Take(count)
|
.Take(6)
|
||||||
.ToList();
|
.ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//protected override void Dispose(bool disposing)
|
||||||
|
//{
|
||||||
|
// if (disposing)
|
||||||
|
// {
|
||||||
|
// _storeContext.Dispose();
|
||||||
|
// }
|
||||||
|
// base.Dispose(disposing);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using MvcMusicStore.Models;
|
using MvcMusicStore.Models;
|
||||||
using MvcMusicStore.ViewModels;
|
using MvcMusicStore.ViewModels;
|
||||||
|
|
||||||
|
|
@ -7,76 +9,56 @@ namespace MvcMusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class ShoppingCartController : Controller
|
public class ShoppingCartController : Controller
|
||||||
{
|
{
|
||||||
MusicStoreEntities storeDB = new MusicStoreEntities();
|
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /ShoppingCart/
|
// GET: /ShoppingCart/
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
public IActionResult Index()
|
|
||||||
{
|
{
|
||||||
var cart = ShoppingCart.GetCart(storeDB, this.Context);
|
var cart = ShoppingCart.GetCart(_storeContext, this);
|
||||||
|
|
||||||
// Set up our ViewModel
|
|
||||||
var viewModel = new ShoppingCartViewModel
|
var viewModel = new ShoppingCartViewModel
|
||||||
{
|
{
|
||||||
CartItems = cart.GetCartItems(),
|
CartItems = await cart.GetCartItems().ToListAsync(),
|
||||||
CartTotal = cart.GetTotal()
|
CartTotal = await cart.GetTotal()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return the view
|
|
||||||
return View(viewModel);
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /ShoppingCart/AddToCart/5
|
// GET: /ShoppingCart/AddToCart/5
|
||||||
|
public async Task<IActionResult> AddToCart(int id)
|
||||||
public IActionResult AddToCart(int id)
|
|
||||||
{
|
{
|
||||||
|
var cart = ShoppingCart.GetCart(_storeContext, this);
|
||||||
|
|
||||||
// Retrieve the album from the database
|
await cart.AddToCart(await _storeContext.Albums.SingleAsync(a => a.AlbumId == id));
|
||||||
var addedAlbum = storeDB.Albums
|
|
||||||
.Single(album => album.AlbumId == id);
|
|
||||||
|
|
||||||
// Add it to the shopping cart
|
await _storeContext.SaveChangesAsync();
|
||||||
var cart = ShoppingCart.GetCart(storeDB, this.Context);
|
|
||||||
|
|
||||||
cart.AddToCart(addedAlbum);
|
return null;//RedirectToAction("Index");
|
||||||
|
|
||||||
storeDB.SaveChanges();
|
|
||||||
|
|
||||||
// Go back to the main store page for more shopping
|
|
||||||
// return RedirectToAction("Index");
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// AJAX: /ShoppingCart/RemoveFromCart/5
|
// AJAX: /ShoppingCart/RemoveFromCart/5
|
||||||
|
//[HttpPost]
|
||||||
// [HttpPost]
|
public async Task<IActionResult> RemoveFromCart(int id)
|
||||||
public IActionResult RemoveFromCart(int id)
|
|
||||||
{
|
{
|
||||||
// Retrieve the current user's shopping cart
|
var cart = ShoppingCart.GetCart(_storeContext, this);
|
||||||
var cart = ShoppingCart.GetCart(storeDB, this.Context);
|
|
||||||
|
|
||||||
// Get the name of the album to display confirmation
|
var albumName = await _storeContext.Carts
|
||||||
string albumName = storeDB.Carts
|
.Where(i => i.RecordId == id)
|
||||||
.Single(item => item.RecordId == id).Album.Title;
|
.Select(i => i.Album.Title)
|
||||||
|
.SingleOrDefaultAsync();
|
||||||
|
|
||||||
// Remove from cart
|
var itemCount = await cart.RemoveFromCart(id);
|
||||||
int itemCount = cart.RemoveFromCart(id);
|
|
||||||
|
|
||||||
storeDB.SaveChanges();
|
await _storeContext.SaveChangesAsync();
|
||||||
|
|
||||||
string removed = (itemCount > 0) ? " 1 copy of " : string.Empty;
|
var removed = (itemCount > 0) ? " 1 copy of " : string.Empty;
|
||||||
|
|
||||||
// Display the confirmation message
|
|
||||||
|
|
||||||
var results = new ShoppingCartRemoveViewModel
|
var results = new ShoppingCartRemoveViewModel
|
||||||
{
|
{
|
||||||
Message = removed + albumName +
|
Message = removed + albumName + " has been removed from your shopping cart.",
|
||||||
" has been removed from your shopping cart.",
|
CartTotal = await cart.GetTotal(),
|
||||||
CartTotal = cart.GetTotal(),
|
CartCount = await cart.GetCount(),
|
||||||
CartCount = cart.GetCount(),
|
|
||||||
ItemCount = itemCount,
|
ItemCount = itemCount,
|
||||||
DeleteId = id
|
DeleteId = id
|
||||||
};
|
};
|
||||||
|
|
@ -84,20 +66,29 @@ namespace MvcMusicStore.Controllers
|
||||||
return Result.Json(results);
|
return Result.Json(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
// [ChildActionOnly]
|
//[ChildActionOnly]
|
||||||
public IActionResult CartSummary()
|
public IActionResult CartSummary()
|
||||||
{
|
{
|
||||||
var cart = ShoppingCart.GetCart(storeDB, this.Context);
|
var cart = ShoppingCart.GetCart(_storeContext, this);
|
||||||
|
|
||||||
var cartItems = cart.GetCartItems()
|
var cartItems = cart.GetCartItems()
|
||||||
.Select(a => a.Album.Title)
|
.Select(a => a.Album.Title)
|
||||||
.OrderBy(x => x);
|
.OrderBy(x => x)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
// ViewBag.CartCount = cartItems.Count();
|
ViewBag.CartCount = cartItems.Count();
|
||||||
// ViewBag.CartSummary = string.Join("\n", cartItems.Distinct());
|
ViewBag.CartSummary = string.Join("\n", cartItems.Distinct());
|
||||||
|
|
||||||
// return PartialView("CartSummary");
|
return null;//PartialView("CartSummary");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//protected override void Dispose(bool disposing)
|
||||||
|
//{
|
||||||
|
// if (disposing)
|
||||||
|
// {
|
||||||
|
// _storeContext.Dispose();
|
||||||
|
// }
|
||||||
|
// base.Dispose(disposing);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,58 +1,55 @@
|
||||||
using MvcMusicStore.Models;
|
using System.Linq;
|
||||||
using System;
|
using System.Threading.Tasks;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
|
using MvcMusicStore.Models;
|
||||||
|
|
||||||
namespace MvcMusicStore.Controllers
|
namespace MvcMusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class StoreController : Controller
|
public class StoreController : Controller
|
||||||
{
|
{
|
||||||
MusicStoreEntities storeDB = new MusicStoreEntities();
|
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
|
||||||
//
|
|
||||||
// GET: /Store/
|
// GET: /Store/
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
public IActionResult Index()
|
|
||||||
{
|
{
|
||||||
var genres = storeDB.Genres.ToList();
|
return View(await _storeContext.Genres.ToListAsync());
|
||||||
|
|
||||||
return View(genres);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /Store/Browse?genre=Disco
|
// GET: /Store/Browse?genre=Disco
|
||||||
|
public async Task<IActionResult> Browse(string genre)
|
||||||
public IActionResult Browse(string genre)
|
|
||||||
{
|
{
|
||||||
// Retrieve Genre genre and its Associated associated Albums albums from database
|
return View(await _storeContext.Genres.Include(e => e.Albums).SingleAsync(g => g.Name == genre));
|
||||||
var genreModel = storeDB.Genres.Include("Albums")
|
|
||||||
.Single(g => g.Name == genre);
|
|
||||||
|
|
||||||
return View(genreModel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
public IActionResult GenreMenu()
|
||||||
{
|
{
|
||||||
var genres = storeDB.Genres
|
var genres = _storeContext.Genres
|
||||||
.OrderByDescending(
|
.OrderByDescending(
|
||||||
g => g.Albums.Sum(
|
g => g.Albums.Sum(
|
||||||
a => a.OrderDetails.Sum(
|
a => a.OrderDetails.Sum(
|
||||||
od => od.Quantity)))
|
od => od.Quantity)))
|
||||||
.Take(9)
|
.Take(9)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
//return PartialView(genres);
|
return null; //PartialView(genres);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//protected override void Dispose(bool disposing)
|
||||||
|
//{
|
||||||
|
// if (disposing)
|
||||||
|
// {
|
||||||
|
// _storeContext.Dispose();
|
||||||
|
// }
|
||||||
|
// base.Dispose(disposing);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,133 +1,141 @@
|
||||||
using System;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using MvcMusicStore.Models;
|
using MvcMusicStore.Models;
|
||||||
|
|
||||||
namespace MvcMusicStore.Controllers
|
namespace MvcMusicStore.Controllers
|
||||||
{
|
{
|
||||||
// [Authorize(Roles = "Administrator")]
|
//[Authorize(Roles = "Administrator")]
|
||||||
public class StoreManagerController : Controller
|
public class StoreManagerController : Controller
|
||||||
{
|
{
|
||||||
private MusicStoreEntities db = new MusicStoreEntities();
|
private readonly MusicStoreEntities _storeContext = new MusicStoreEntities();
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /StoreManager/
|
// GET: /StoreManager/
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
public IActionResult Index()
|
|
||||||
{
|
{
|
||||||
var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist)
|
return View(await _storeContext.Albums
|
||||||
.OrderBy(a => a.Price);
|
.Include(a => a.Genre)
|
||||||
return View(albums.ToList());
|
.Include(a => a.Artist)
|
||||||
|
.OrderBy(a => a.Price).ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /StoreManager/Details/5
|
// GET: /StoreManager/Details/5
|
||||||
|
public async Task<IActionResult> Details(int id = 0)
|
||||||
public IActionResult Details(int id = 0)
|
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Find(id);
|
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
|
||||||
|
|
||||||
if (album == null)
|
if (album == null)
|
||||||
{
|
{
|
||||||
//return HttpNotFound();
|
return null;//HttpNotFound();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return View(album);
|
return View(album);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /StoreManager/Create
|
// GET: /StoreManager/Create
|
||||||
|
public async Task<IActionResult> Create()
|
||||||
public IActionResult Create()
|
|
||||||
{
|
{
|
||||||
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
|
return await BuildView(null);
|
||||||
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
|
|
||||||
return View();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// POST: /StoreManager/Create
|
// POST: /StoreManager/Create
|
||||||
|
//[HttpPost]
|
||||||
// [HttpPost]
|
public async Task<IActionResult> Create(Album album)
|
||||||
public IActionResult Create(Album album)
|
|
||||||
{
|
{
|
||||||
if (/*ModelState.IsValid*/true)
|
if (true)//ModelState.IsValid)
|
||||||
{
|
{
|
||||||
db.Albums.Add(album);
|
_storeContext.Albums.Add(album);
|
||||||
db.SaveChanges();
|
|
||||||
// return RedirectToAction("Index");
|
await _storeContext.SaveChangesAsync();
|
||||||
return null;
|
|
||||||
|
return null;//RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
return await BuildView(album);
|
||||||
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
|
||||||
return View(album);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /StoreManager/Edit/5
|
// GET: /StoreManager/Edit/5
|
||||||
|
public async Task<IActionResult> Edit(int id = 0)
|
||||||
public IActionResult Edit(int id = 0)
|
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Find(id);
|
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
|
||||||
if (album == null)
|
if (album == null)
|
||||||
{
|
{
|
||||||
// return HttpNotFound();
|
return null;//HttpNotFound();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
|
||||||
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
return await BuildView(album);
|
||||||
return View(album);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// POST: /StoreManager/Edit/5
|
// POST: /StoreManager/Edit/5
|
||||||
|
//[HttpPost]
|
||||||
// [HttpPost]
|
public async Task<IActionResult> Edit(Album album)
|
||||||
public IActionResult Edit(Album album)
|
|
||||||
{
|
{
|
||||||
//if (ModelState.IsValid)
|
if (true)//ModelState.IsValid)
|
||||||
//{
|
{
|
||||||
// db.Entry(album).State = EntityState.Modified;
|
_storeContext.Albums.Update(album);
|
||||||
// db.SaveChanges();
|
|
||||||
// return RedirectToAction("Index");
|
await _storeContext.SaveChangesAsync();
|
||||||
//}
|
|
||||||
//ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
return null;//RedirectToAction("Index");
|
||||||
//ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
}
|
||||||
return View(album);
|
|
||||||
|
return await BuildView(album);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// GET: /StoreManager/Delete/5
|
// GET: /StoreManager/Delete/5
|
||||||
|
public async Task<IActionResult> Delete(int id = 0)
|
||||||
public IActionResult Delete(int id = 0)
|
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Find(id);
|
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
|
||||||
if (album == null)
|
if (album == null)
|
||||||
{
|
{
|
||||||
// return HttpNotFound();
|
return null;//HttpNotFound();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return View(album);
|
return View(album);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// POST: /StoreManager/Delete/5
|
// POST: /StoreManager/Delete/5
|
||||||
|
//[HttpPost, ActionName("Delete")]
|
||||||
// [HttpPost, ActionName("Delete")]
|
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||||
public IActionResult DeleteConfirmed(int id)
|
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Find(id);
|
var album = await _storeContext.Albums.SingleOrDefaultAsync(e => e.AlbumId == id);
|
||||||
db.Albums.Remove(album);
|
if (album == null)
|
||||||
db.SaveChanges();
|
{
|
||||||
// return RedirectToAction("Index");
|
return null;//HttpNotFound();
|
||||||
return null;
|
}
|
||||||
|
|
||||||
|
_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();
|
//ViewBag.GenreId = new SelectList(
|
||||||
// base.Dispose(disposing);
|
// 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);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,26 +1,22 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
|
||||||
// using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
namespace MvcMusicStore.Models
|
||||||
{
|
{
|
||||||
public class Album
|
public class Album
|
||||||
{
|
{
|
||||||
//[ScaffoldColumn(false)]
|
//[ScaffoldColumn(false)]
|
||||||
|
|
||||||
public int AlbumId { get; set; }
|
public int AlbumId { get; set; }
|
||||||
|
|
||||||
public int GenreId { get; set; }
|
public int GenreId { get; set; }
|
||||||
|
|
||||||
public int ArtistId { get; set; }
|
public int ArtistId { get; set; }
|
||||||
|
|
||||||
// [Required]
|
//[Required]
|
||||||
// [StringLength(160, MinimumLength = 2)]
|
//[StringLength(160, MinimumLength = 2)]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
|
||||||
//[Required]
|
//[Required]
|
||||||
//[Range(0.01, 100.00)]
|
//[Range(0.01, 100.00)]
|
||||||
|
|
||||||
//[DataType(DataType.Currency)]
|
//[DataType(DataType.Currency)]
|
||||||
public decimal Price { get; set; }
|
public decimal Price { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,4 @@
|
||||||
using System;
|
namespace MvcMusicStore.Models
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
|
||||||
{
|
{
|
||||||
public class Artist
|
public class Artist
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,18 @@
|
||||||
using System;
|
using System;
|
||||||
//using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
namespace MvcMusicStore.Models
|
||||||
{
|
{
|
||||||
public class Cart
|
public class Cart
|
||||||
{
|
{
|
||||||
//[Key]
|
//[Key]
|
||||||
public int RecordId { get; set; }
|
public int RecordId { get; set; }
|
||||||
public string CartId { get; set; }
|
public string CartId { get; set; }
|
||||||
public int AlbumId { get; set; }
|
public int AlbumId { get; set; }
|
||||||
public int Count { get; set; }
|
public int Count { get; set; }
|
||||||
|
|
||||||
//[DataType(DataType.DateTime)]
|
//[DataType(DataType.DateTime)]
|
||||||
public DateTime DateCreated { get; set; }
|
public DateTime DateCreated { get; set; }
|
||||||
|
|
||||||
public virtual Album Album { get; set; }
|
public virtual Album Album { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
namespace MvcMusicStore.Models
|
||||||
{
|
{
|
||||||
public class Genre
|
public class Genre
|
||||||
{
|
{
|
||||||
public int GenreId { get; set; }
|
public int GenreId { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
public List<Album> Albums { get; set; }
|
public List<Album> Albums { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,18 @@
|
||||||
using System;
|
using Microsoft.Data.Entity;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
namespace MvcMusicStore.Models
|
||||||
{
|
{
|
||||||
//public class MusicStoreEntities : DbContext
|
public class MusicStoreEntities : EntityContext
|
||||||
//{
|
|
||||||
// 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 List<Album> Albums { get; set; }
|
public MusicStoreEntities()
|
||||||
public List<Genre> Genres { get; set; }
|
: base(null) // TODO: Fix after discussion of which patterns to use here
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public EntitySet<Album> Albums { get; set; }
|
||||||
{
|
public EntitySet<Genre> Genres { get; set; }
|
||||||
throw new NotImplementedException();
|
public EntitySet<Artist> Artists { get; set; }
|
||||||
}
|
public EntitySet<Cart> Carts { get; set; }
|
||||||
|
public EntitySet<Order> Orders { get; set; }
|
||||||
internal object Entry(Album album)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
|
||||||
//using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
namespace MvcMusicStore.Models
|
||||||
{
|
{
|
||||||
//[Bind(Include = "FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email")]
|
//[Bind(Include = "FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email")]
|
||||||
public class Order
|
public class Order
|
||||||
{
|
{
|
||||||
|
public Order()
|
||||||
|
{
|
||||||
|
OrderDetails = new List<OrderDetail>();
|
||||||
|
}
|
||||||
|
|
||||||
//[ScaffoldColumn(false)]
|
//[ScaffoldColumn(false)]
|
||||||
public int OrderId { get; set; }
|
public int OrderId { get; set; }
|
||||||
|
|
||||||
|
|
@ -59,7 +62,7 @@ namespace MvcMusicStore.Models
|
||||||
//[DataType(DataType.EmailAddress)]
|
//[DataType(DataType.EmailAddress)]
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
|
|
||||||
// [ScaffoldColumn(false)]
|
//[ScaffoldColumn(false)]
|
||||||
public decimal Total { get; set; }
|
public decimal Total { get; set; }
|
||||||
|
|
||||||
public List<OrderDetail> OrderDetails { get; set; }
|
public List<OrderDetail> OrderDetails { get; set; }
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,196 +1,159 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Abstractions;
|
using Microsoft.AspNet.Abstractions;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
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 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);
|
_storeContext = storeContext;
|
||||||
cart.ShoppingCartId = cart.GetCartId(context);
|
_cartId = cartId;
|
||||||
return cart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to simplify shopping cart calls
|
public static ShoppingCart GetCart(MusicStoreEntities storeContext, Controller controller)
|
||||||
public static ShoppingCart GetCart(MusicStoreEntities db, 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
|
throw new NotImplementedException();
|
||||||
var cartItem = _db.Carts.SingleOrDefault(
|
//if (context.Session[CartSessionKey] == null)
|
||||||
c => c.CartId == ShoppingCartId
|
//{
|
||||||
&& c.AlbumId == album.AlbumId);
|
// 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)
|
if (cartItem == null)
|
||||||
{
|
{
|
||||||
// Create a new cart item if no cart item exists
|
|
||||||
cartItem = new Cart
|
cartItem = new Cart
|
||||||
{
|
{
|
||||||
AlbumId = album.AlbumId,
|
AlbumId = album.AlbumId,
|
||||||
CartId = ShoppingCartId,
|
CartId = _cartId,
|
||||||
Count = 1,
|
Count = 1,
|
||||||
DateCreated = DateTime.Now
|
DateCreated = DateTime.Now
|
||||||
};
|
};
|
||||||
|
|
||||||
_db.Carts.Add(cartItem);
|
_storeContext.Carts.Add(cartItem);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// If the item does exist in the cart, then add one to the quantity
|
|
||||||
cartItem.Count++;
|
cartItem.Count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int RemoveFromCart(int id)
|
public async Task<int> RemoveFromCart(int id)
|
||||||
{
|
{
|
||||||
// Get the cart
|
var cartItem = await GetCartItem(id);
|
||||||
var cartItem = _db.Carts.Single(
|
|
||||||
cart => cart.CartId == ShoppingCartId
|
|
||||||
&& cart.RecordId == id);
|
|
||||||
|
|
||||||
int itemCount = 0;
|
|
||||||
|
|
||||||
if (cartItem != null)
|
if (cartItem != null)
|
||||||
{
|
{
|
||||||
if (cartItem.Count > 1)
|
if (cartItem.Count > 1)
|
||||||
{
|
{
|
||||||
cartItem.Count--;
|
return --cartItem.Count;
|
||||||
itemCount = cartItem.Count;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_db.Carts.Remove(cartItem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_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);
|
return _storeContext.Carts.SingleOrDefaultAsync(
|
||||||
|
c => c.CartId == _cartId && c.AlbumId == albumId);
|
||||||
foreach (var cartItem in cartItems)
|
|
||||||
{
|
|
||||||
_db.Carts.Remove(cartItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
return _storeContext.Carts
|
||||||
int? count = (from cartItems in _db.Carts
|
.Where(c => c.CartId == _cartId)
|
||||||
where cartItems.CartId == ShoppingCartId
|
.Select(c => c.Count)
|
||||||
select (int?)cartItems.Count).Sum();
|
.SumAsync();
|
||||||
|
|
||||||
// Return 0 if all entries are null
|
|
||||||
return count ?? 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public decimal GetTotal()
|
public Task<decimal> GetTotal()
|
||||||
{
|
{
|
||||||
// Multiply album price by count of that album to get
|
return _storeContext.Carts
|
||||||
// the current price for each of those albums in the cart
|
.Where(c => c.CartId == _cartId)
|
||||||
// sum all album price totals to get the cart total
|
.Select(c => c.Count * c.Album.Price)
|
||||||
decimal? total = (from cartItems in _db.Carts
|
.SumAsync();
|
||||||
where cartItems.CartId == ShoppingCartId
|
|
||||||
select (int?)cartItems.Count * cartItems.Album.Price).Sum();
|
|
||||||
return total ?? decimal.Zero;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CreateOrder(Order order)
|
public async Task<int> CreateOrder(Order order)
|
||||||
{
|
{
|
||||||
decimal orderTotal = 0;
|
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)
|
foreach (var item in cartItems)
|
||||||
{
|
{
|
||||||
var album = _db.Albums.Find(item.AlbumId);
|
order.OrderDetails.Add(new OrderDetail
|
||||||
|
|
||||||
var orderDetail = new OrderDetail
|
|
||||||
{
|
{
|
||||||
AlbumId = item.AlbumId,
|
AlbumId = item.AlbumId,
|
||||||
OrderId = order.OrderId,
|
OrderId = order.OrderId,
|
||||||
UnitPrice = album.Price,
|
UnitPrice = item.Album.Price,
|
||||||
Quantity = item.Count,
|
Quantity = item.Count,
|
||||||
};
|
});
|
||||||
|
|
||||||
// Set the order total of the shopping cart
|
orderTotal += item.Count * item.Album.Price;
|
||||||
orderTotal += (item.Count * item.Album.Price);
|
|
||||||
|
|
||||||
_db.OrderDetails.Add(orderDetail);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the order's total to the orderTotal count
|
|
||||||
order.Total = orderTotal;
|
order.Total = orderTotal;
|
||||||
|
|
||||||
// Empty the shopping cart
|
await EmptyCart();
|
||||||
EmptyCart();
|
|
||||||
|
|
||||||
// Return the OrderId as the confirmation number
|
|
||||||
return order.OrderId;
|
return order.OrderId;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We're using HttpContextBase to allow access to cookies.
|
private async Task EmptyCart()
|
||||||
public string GetCartId(HttpContext context)
|
|
||||||
{
|
{
|
||||||
//if (context.Session[CartSessionKey] == null)
|
foreach (var cartItem in await _storeContext.Carts.Where(
|
||||||
//{
|
c => c.CartId == _cartId).ToListAsync())
|
||||||
// if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
|
{
|
||||||
// {
|
_storeContext.Carts.Remove(cartItem);
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a user has logged in, migrate their shopping cart to
|
public async Task MigrateCart(string userName)
|
||||||
// be associated with their username
|
|
||||||
public void 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;
|
item.CartId = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await _storeContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MvcMusicStore
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 class ShoppingCartRemoveViewModel
|
||||||
{
|
{
|
||||||
public string Message { get; set; }
|
public string Message { get; set; }
|
||||||
public decimal CartTotal { get; set; }
|
public decimal CartTotal { get; set; }
|
||||||
public int CartCount { get; set; }
|
public int CartCount { get; set; }
|
||||||
public int ItemCount { get; set; }
|
public int ItemCount { get; set; }
|
||||||
public int DeleteId { get; set; }
|
public int DeleteId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
using MvcMusicStore.Models;
|
||||||
|
|
||||||
namespace MvcMusicStore.ViewModels
|
namespace MvcMusicStore.ViewModels
|
||||||
|
|
@ -6,6 +8,6 @@ namespace MvcMusicStore.ViewModels
|
||||||
public class ShoppingCartViewModel
|
public class ShoppingCartViewModel
|
||||||
{
|
{
|
||||||
public List<Cart> CartItems { get; set; }
|
public List<Cart> CartItems { get; set; }
|
||||||
public decimal CartTotal { get; set; }
|
public decimal CartTotal { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,16 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.DependencyInjection" : "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": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,11 +1,8 @@
|
||||||
using System.Collections.Generic;
|
using System.Threading.Tasks;
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
using Microsoft.AspNet.Identity.EntityFramework;
|
using Microsoft.AspNet.Identity.EntityFramework;
|
||||||
using Microsoft.AspNet.Identity.Owin;
|
|
||||||
using Microsoft.Owin.Security;
|
using Microsoft.Owin.Security;
|
||||||
using MvcMusicStore.Models;
|
using MvcMusicStore.Models;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
using System;
|
namespace MvcMusicStore.Models
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Web;
|
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
|
||||||
{
|
{
|
||||||
public class Artist
|
public class Artist
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
namespace MvcMusicStore.Models
|
namespace MvcMusicStore.Models
|
||||||
{
|
{
|
||||||
public class Genre
|
public class Genre
|
||||||
{
|
{
|
||||||
public int GenreId { get; set; }
|
public int GenreId { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
public List<Album> Albums { get; set; }
|
public List<Album> Albums { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data.Entity;
|
using System.Data.Entity;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue