コード例 #1
0
 public void CleanupSessions()
 {
     if (Utils.IsSiteDisposed(this))
     {
         throw new InvalidOperationException("Invalid operation: this site is disposed.");
     }
     using (var context = new AuctionSiteContext(ConnectionString))
     {
         foreach (var iUser in GetUsers())
         {
             var user = iUser as UserBLL;
             if (user?.Session != null)
             {
                 if (!user.Session.IsValid())
                 {
                     var dbUser = context.Users.Find(user.UserID);
                     if (null == dbUser)
                     {
                         throw new InvalidOperationException("Invalid operation: user not found.");
                     }
                     dbUser.Session              = null;
                     dbUser.SessionId            = null;
                     context.Entry(dbUser).State = EntityState.Modified;
                     context.Entry(context.Sessions.Find(user.Session.Id)).State = EntityState.Deleted;
                 }
             }
         }
         context.SaveChanges();
     }
 }
コード例 #2
0
 private SessionBLL GetUserSession(UserBLL user)
 {
     using (var context = new AuctionSiteContext(ConnectionString))
     {
         var session = context.Sessions.Find(Utils.CreateSessionId(this, user));
         if (null == session)
         {
             user.Session = Utils.CreateNewSession(this, user);
             return(user.Session);
         }
         var sessionBLL = new SessionBLL(session, user);
         if (!sessionBLL.IsValid())
         {
             sessionBLL.Logout();
             user.Session = Utils.CreateNewSession(this, user);
         }
         else
         {
             var validUntil = AlarmClock.Now.AddSeconds(SessionExpirationInSeconds);
             session.ValidUntil           = validUntil;
             user.Session.ValidUntil      = validUntil;
             context.Entry(session).State = EntityState.Modified;
             context.SaveChanges();
         }
         return(user.Session);
     }
 }
コード例 #3
0
        public ISession Login(string username, string password)
        {
            if (Utils.IsSiteDisposed(this))
            {
                throw new InvalidOperationException("Invalid operation: this site is disposed.");
            }
            if (null == username)
            {
                throw new ArgumentNullException($"{nameof(username)} cannot be null");
            }
            if (null == password)
            {
                throw new ArgumentNullException($"{nameof(password)} cannot be null");
            }
            if (!Utils.IsValidUsername(username))
            {
                throw new ArgumentException($"{nameof(username)} is not valid.");
            }
            if (!Utils.IsValidPassword(password))
            {
                throw new ArgumentException($"{nameof(password)} is not valid.");
            }

            var user = GetUserByUsername(username);

            if (null == user)
            {
                return(null);
            }

            if (!Utils.ArePasswordsEquals(user.Password, password, Convert.FromBase64String(user.Salt)))
            {
                return(null);
            }

            var userSession = GetUserSession(user);

            //user.Session = userSession;
            using (var context = new AuctionSiteContext(ConnectionString))
            {
                var dbUser = context.Users.Find(user.UserID);
                if (null == dbUser)
                {
                    throw new InvalidOperationException("Invalid operation: user not found.");
                }
                dbUser.SessionId            = userSession.Id;
                context.Entry(dbUser).State = EntityState.Modified;
                context.SaveChanges();
            }

            return(userSession);
        }
コード例 #4
0
 private static void UpdateAuction(AuctionSiteContext context, Auction auction, ISession session, double offer, double?currentPrice)
 {
     auction.LastBid = offer;
     if (null != session)
     {
         auction.CurrentWinnerId = ((UserBLL)session.User).UserID;
     }
     if (null != currentPrice)
     {
         auction.CurrentPrice = (double)currentPrice;
     }
     context.Entry(auction).State = EntityState.Modified;
     context.SaveChanges();
 }
コード例 #5
0
        public void Delete()
        {
            if (Utils.IsAuctionDisposed(this))
            {
                throw new InvalidOperationException("Invalid operation: user is disposed.");
            }
            var sellerBLL = (UserBLL)Seller;

            using (var context = new AuctionSiteContext(sellerBLL.Site.ConnectionString))
            {
                var auction = context.Auctions.Find(Id);
                context.Entry(auction).State = EntityState.Deleted;
                context.SaveChanges();
            }
            IsDeleted = true;
        }
コード例 #6
0
 public void Delete()
 {
     if (Utils.IsSiteDisposed(this))
     {
         throw new InvalidOperationException("Invalid operation: this site is disposed.");
     }
     DeleteUsers();
     DeleteAuctions();
     using (var context = new AuctionSiteContext(ConnectionString))
     {
         var site = context.Sites.Find(this.Name);
         context.Entry(site).State = EntityState.Deleted;
         context.SaveChanges();
     }
     IsDeleted = true;
 }
コード例 #7
0
        public void UpdateDeletedUser()
        {
            var sellerBLL = (UserBLL)Seller;

            using (var context = new AuctionSiteContext(sellerBLL.Site.ConnectionString))
            {
                var auction = context.Auctions.Find(Id);
                if (null == auction)
                {
                    throw new InvalidOperationException("Invalid operation: auction not found.");
                }
                auction.CurrentWinner        = null;
                auction.CurrentWinnerId      = null;
                context.Entry(auction).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
コード例 #8
0
        public void Delete()
        {
            if (Utils.IsUserDisposed(this))
            {
                throw new InvalidOperationException("User disposed.");
            }
            foreach (var auction in Site.GetAuctions(true))
            {
                if (Equals(auction.CurrentWinner()))
                {
                    throw new InvalidOperationException("User cannot be deleted because is a current winner in an auction.");
                }
                if (Equals(auction.Seller))
                {
                    throw new InvalidOperationException("User cannot be deleted because is a seller.");
                }
            }
            foreach (var auction in Site.GetEndedAuctions())
            {
                var auctionBLL = (AuctionBLL)auction;
                if (Equals(auctionBLL.CurrentWinner()))
                {
                    auctionBLL.UpdateDeletedUser();
                }
                else if (Equals(auctionBLL.Seller))
                {
                    auctionBLL.Delete();
                }
            }
            Session?.Logout();
            using (var context = new AuctionSiteContext(Site.ConnectionString))
            {
                var user = context.Users.Find(UserID);
                context.Entry(user).State = EntityState.Deleted;
                context.SaveChanges();
            }

            IsDeleted = true;
        }
コード例 #9
0
        public bool BidOnAuction(ISession session, double offer)
        {
            if (Utils.IsAuctionDisposed(this))
            {
                throw new InvalidOperationException("Invalid operation: user is disposed.");
            }
            if (null == session)
            {
                throw new ArgumentNullException($"{nameof(session)} cannot be null.");
            }
            if (offer < 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(offer)} cannot be a negative number.");
            }
            if (!session.IsValid())
            {
                throw new ArgumentException($"{nameof(session)} not valid.");
            }
            if (Seller.Equals(session.User))
            {
                throw new ArgumentException("Bidder and seller cannot be the same user.");
            }
            if (!((UserBLL)Seller).Site.Equals(((UserBLL)session.User).Site))
            {
                throw new ArgumentException($"Seller site must be the same of bidder site.");
            }

            var sellerBLL = (UserBLL)Seller;

            using (var context = new AuctionSiteContext(sellerBLL.Site.ConnectionString))
            {
                var auction = context.Auctions.Find(Id);
                if (null == auction)
                {
                    throw new InvalidOperationException("Invalid operation: auction not found.");
                }
                var lastBid             = auction.LastBid;
                var currentWinnerId     = auction.CurrentWinnerId;
                var minimumBidIncrement = sellerBLL.Site.MinimumBidIncrement;
                var startingPrice       = auction.StartingPrice;
                if (offer < startingPrice)
                {
                    return(false);
                }
                var isFirstBid           = null == lastBid && null == currentWinnerId;
                var bidderAlreadyWinning = currentWinnerId == ((UserBLL)session.User).UserID;
                if (bidderAlreadyWinning)
                {
                    if (lastBid + minimumBidIncrement >= offer)
                    {
                        return(false);
                    }
                }
                if (offer < CurrentPrice())
                {
                    return(false);
                }
                if (!isFirstBid && offer < CurrentPrice() + minimumBidIncrement)
                {
                    return(false);
                }
                var sessionDb = context.Sessions.Find(session.Id);
                if (null == sessionDb)
                {
                    throw new InvalidOperationException("Invalid operation: session not found");
                }
                sessionDb.ValidUntil             = sellerBLL.Site.AlarmClock.Now.AddSeconds(sellerBLL.Site.SessionExpirationInSeconds);
                context.Entry(sessionDb).State   = EntityState.Modified;
                ((SessionBLL)session).ValidUntil = sellerBLL.Site.AlarmClock.Now.AddSeconds(sellerBLL.Site.SessionExpirationInSeconds);
                if (isFirstBid)
                {
                    UpdateAuction(context, auction, session, offer, null);
                    return(true);
                }
                if (bidderAlreadyWinning)
                {
                    UpdateAuction(context, auction, null, offer, null);
                    return(true);
                }
                if (offer > lastBid)
                {
                    UpdateAuction(context, auction, session, offer, Math.Min(offer, (double)lastBid + minimumBidIncrement));
                    return(true);
                }

                UpdateAuction(context, auction, null, offer, Math.Min(offer + minimumBidIncrement, (double)lastBid));

                return(true);
            }
        }
コード例 #10
0
        public IAuction CreateAuction(string description, DateTime endsOn, double startingPrice)
        {
            if (!IsValid())
            {
                throw new InvalidOperationException("Session not valid.");
            }
            if (Utils.IsSessionDisposed(this))
            {
                throw new InvalidOperationException("Session disposed.");
            }
            if (null == description)
            {
                throw new ArgumentNullException($"{nameof(description)} cannot be null.");
            }
            if (string.Empty == description)
            {
                throw new ArgumentException($"{nameof(description)} cannot be an empty string.");
            }
            if (IsStartingPriceNegative())
            {
                throw new ArgumentOutOfRangeException($"{nameof(startingPrice)} cannot be a negative number.");
            }
            if (endsOn < AlarmClock.Now)
            {
                throw new UnavailableTimeMachineException($"{endsOn} cannot be in the past.");
            }
            var     userBLL = User as UserBLL;
            Auction auction;
            var     validUntil = AlarmClock.Now.AddSeconds(userBLL.Site.SessionExpirationInSeconds);

            using (var context = new AuctionSiteContext(userBLL.Site.ConnectionString))
            {
                auction = new Auction()
                {
                    Description     = description,
                    EndsOn          = endsOn,
                    SiteName        = userBLL.Site.Name,
                    SellerId        = userBLL.UserID,
                    CurrentWinnerId = null,
                    LastBid         = null,
                    StartingPrice   = startingPrice,
                    CurrentPrice    = startingPrice
                };
                context.Auctions.Add(auction);

                var session = context.Sessions.Find(Id);
                if (null == session)
                {
                    throw new InvalidOperationException("Invalid operation: session not found.");
                }
                session.ValidUntil           = validUntil;
                context.Entry(session).State = EntityState.Modified;
                context.SaveChanges();
            }
            ValidUntil = validUntil;
            return(new AuctionBLL(auction, User));

            bool IsStartingPriceNegative()
            {
                return(startingPrice < 0);
            }
        }