Permalink
Browse files

Fixing Bid model

  • Loading branch information...
1 parent 2fbdc1d commit 87dfbc932fe5df7c75b8604a9bfc79572f1ad2ba @jchadwick jchadwick committed Dec 22, 2011
Showing with 47 additions and 11 deletions.
  1. +27 −0 Core/DataAccess/DataContext.cs
  2. +17 −8 Core/Model/Auction.cs
  3. +0 −2 Core/Model/Bid.cs
  4. +3 −1 Core/Model/User.cs
@@ -18,5 +18,32 @@ public DataContext()
Configuration.ProxyCreationEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
+
+ protected override void OnModelCreating(DbModelBuilder modelBuilder)
+ {
+/*
+ modelBuilder.Entity<Auction>()
+ .HasMany(x => x.Bids)
+ .WithOptional()
+ .Map(x => x.MapKey("Auction_ID"));
+*/
+
+ modelBuilder.Entity<Bid>()
+ .HasRequired(x => x.Auction)
+ .WithMany()
+ .WillCascadeOnDelete(false);
+
+ modelBuilder.Entity<Bid>()
+ .HasRequired(x => x.User)
+ .WithMany()
+ .WillCascadeOnDelete(false);
+
+/*
+ modelBuilder.Entity<User>()
+ .HasMany(x => x.Bids)
+ .WithOptional()
+ .Map(x => x.MapKey("User_ID"));
+*/
+ }
}
}
View
@@ -36,7 +36,6 @@ public bool IsCompleted
public virtual User Owner { get; set; }
-
public Auction()
{
Bids = new Collection<Bid>();
@@ -50,15 +49,13 @@ public void PostBid(User user, Currency bidAmount)
Contract.Requires(user != null);
Contract.Requires(bidAmount != null);
- var bid = new Bid(user, this, bidAmount);
+ // TODO: Support multiple currencies
+ if (WinningBid != null && bidAmount.Value <= WinningBid.Amount.Value)
+ throw new InvalidBidAmountException(bidAmount, WinningBid);
+ var bid = new Bid(user, this, bidAmount);
+ WinningBid = bid;
Bids.Add(bid);
-
- // TODO: Support multiple currencies
- if (WinningBid == null || bidAmount.Value > WinningBid.Amount.Value)
- {
- WinningBid = bid;
- }
}
@@ -89,4 +86,16 @@ public class Metadata
public object Owner;
}
}
+
+ public class InvalidBidAmountException : Exception
+ {
+ public Currency BidAmount { get; set; }
+ public Bid WinningBid { get; set; }
+
+ public InvalidBidAmountException(Currency bidAmount, Bid winningBid)
+ {
+ BidAmount = bidAmount;
+ WinningBid = winningBid;
+ }
+ }
}
View
@@ -87,7 +87,6 @@ public override int GetHashCode()
public class Metadata
{
[Required]
- [InverseProperty("Bids")]
public object Auction;
[Required]
@@ -97,7 +96,6 @@ public class Metadata
public object Timestamp;
[Required]
- [InverseProperty("Bids")]
public object User;
}
}
View
@@ -1,4 +1,3 @@
-using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
@@ -61,6 +60,9 @@ public void Bid(Auction auction, Currency bidAmount)
public class Metadata
{
+ [InverseProperty("User")]
+ public object Bids;
+
[StringLength(50)]
public object DisplayName;

0 comments on commit 87dfbc9

Please sign in to comment.