Ejemplo n.º 1
0
        public Auction CreateAuction(DateTime date, string description, List <Art> arts)
        {
            Auction auction = new Auction()
            {
                Date        = date,
                Description = description,
                Arts        = arts
            };

            using (AuctionDBContext db = new AuctionDBContext())
            {
                //db.Arts.Load();

                foreach (var art in arts)
                {
                    db.Arts.Attach(art);
                    db.Entry(art).Entity.Auction = auction;
                    db.Entry(art).State          = EntityState.Modified;
                    //
                    //db.Entry(art).State = EntityState.Modified;;
                }

                db.Auktions.Add(auction);
                db.DebugDetectChanges();
                db.SaveChanges();
            }
            return(auction);
        }
Ejemplo n.º 2
0
        public CtrMember.MemberBidState InsertBid(int mid, decimal bidAmount, int aid)
        {
            CtrArt    ctrArt = new CtrArt();
            CtrMember ctrMem = new CtrMember();
            Art       a      = ctrArt.RetrieveById(aid);
            Member    m      = ctrMem.RetrieveSingleByID(mid);

            CtrMember.MemberBidState bidState = CtrMember.MemberBidState.BidError;

            if (m == null)
            {
                return(CtrMember.MemberBidState.BidError);
            }
            if (a == null)
            {
                return(CtrMember.MemberBidState.BidError);
            }

            if (ctrMem.CheckEligibleBid(m, bidAmount) == CtrMember.MemberBidState.Verified)
            {
                using (AuctionDBContext db = new AuctionDBContext())
                {
                    Bid b = new Bid();

                    b.DateTime  = DateTime.Now;
                    b.BidAmount = bidAmount;
                    b.Art       = a;
                    b.Member    = m;

                    try
                    {
                        db.Members.Attach(m);
                        db.Arts.Attach(a);
                        db.Bids.Add(b);
                        db.DebugDetectChanges();
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    bidState = CtrMember.MemberBidState.BidConfirmed;
                }
            }
            else
            {
                bidState = CtrMember.MemberBidState.NotEnough;
            }

            return(bidState);
        }
Ejemplo n.º 3
0
        public Auction UpdateAuction(Auction auction)
        {
            if (auction == null)
            {
                throw new NullReferenceException("auction");
            }
            using (AuctionDBContext db = new AuctionDBContext())
            {
                try
                {
                    var artsInDb = db.Arts.Where(x => x.AuctionId == auction.Id).ToList();
                    foreach (var art in artsInDb)
                    {
                        art.AuctionId       = null;
                        db.Entry(art).State = EntityState.Modified;
                    }

                    var artsInDbIds = artsInDb.Select(x => x.Id);

                    foreach (var art in auction.Arts)
                    {
                        art.AuctionId = auction.Id;
                        if (!artsInDbIds.Contains(art.Id))
                        {
                            db.Entry(art).State = EntityState.Modified;
                        }
                        else
                        {
                            var tempObj = db.ChangeTracker.Entries <Art>().Single(x => x.Entity.Id == art.Id);
                            tempObj.Entity.AuctionId = auction.Id;
                        }
                    }

                    var artsList = auction.Arts;
                    auction.Arts = null;

                    db.Entry(auction).State = EntityState.Modified;

                    db.DebugDetectChanges();
                    db.SaveChanges();
                    auction.Arts = artsList;
                }
                catch (UpdateException)
                {
                }
            }
            return(auction);
        }
Ejemplo n.º 4
0
        public void DeleteArt(Art art)
        {
            if (art == null)
            {
                throw new ArgumentNullException("art");
            }
            using (AuctionDBContext db = new AuctionDBContext())
            {
                var dbArt = db.Arts.Single(x => x.Id == art.Id);

                //db.Arts.Attach(art);
                //db.Entry(art).State = EntityState.Deleted;
                db.Arts.Remove(dbArt);
                db.DebugDetectChanges();
                db.SaveChanges();
            }
        }