Esempio n. 1
0
        private User GetUserByUsername(string username)
        {
            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            var queryRis = factoryContext.Users.SqlQuery("select * from dbo.Users where UserSiteName = '" + Name + "'").ToList <User>();

            factoryContext.Database.Connection.Close();

            if (!queryRis.Any())
            {
                return(null);
            }

            foreach (var item in queryRis)
            {
                if (item.Username == username)
                {
                    return(item);
                }
            }

            return(null);
        }
Esempio n. 2
0
        public void Setup(string connectionString)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException();
            }
            if (!isValidConnection(connectionString))
            {
                throw new UnavailableDbException();
            }

            SiteFactoryContext ctx = new SiteFactoryContext(connectionString);

            ctx.Database.Initialize(true);

            /*Dato che non funziona il drop faccio le delete in modo che gli unit test vadano in blocco*/
            /*La cancellazione del DB usando il Database Inizializer DropCreateDatabaseAlways non va a buon fine*/
            /*Il drop causa errore di database in uso (Sql Server Express)*/

            ctx.Database.ExecuteSqlCommand("delete from dbo.Auctions");
            ctx.Database.ExecuteSqlCommand("delete from dbo.Sessions");
            ctx.Database.ExecuteSqlCommand("delete from dbo.Users");
            ctx.Database.ExecuteSqlCommand("delete from dbo.Sites");

            ctx.Database.Connection.Close();
        }
Esempio n. 3
0
        public IEnumerable <IUser> GetUsers()
        {
            if (!this.IsSiteExist(this.Name))
            {
                throw new InvalidOperationException();
            }

            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            var queryRis = factoryContext.Users.SqlQuery("select * from dbo.Users where UserSiteName = '" + Name + "'").ToList <User>();

            factoryContext.Database.Connection.Close();

            if (!queryRis.Any())
            {
                return(new List <IUser>());
            }

            List <IUser> allUser = new List <IUser>();

            foreach (var item in queryRis)
            {
                allUser.Add(item);
            }

            return(allUser);
        }
Esempio n. 4
0
        public IEnumerable <string> GetSiteNames(string connectionString)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException();
            }
            if (!isValidConnection(connectionString))
            {
                throw new UnavailableDbException();
            }

            List <string> allSites = new List <string>();

            SiteFactoryContext ctx = new SiteFactoryContext(connectionString);

            var queryRis = ctx.Sites.SqlQuery("select * from dbo.Sites").ToList <Site>();

            foreach (var item in queryRis)
            {
                allSites.Add(item.Name);
            }

            ctx.Database.Connection.Close();

            return(allSites);
        }
Esempio n. 5
0
        public IEnumerable <IAuction> GetAuctions(bool onlyNotEnded)
        {
            if (!this.IsSiteExist(this.Name))
            {
                throw new InvalidOperationException();
            }

            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            var queryRis = factoryContext.Auctions.SqlQuery("select * from dbo.Auctions where AuctionSiteName = '" + Name + "'").ToList <Auction>();

            factoryContext.Database.Connection.Close();

            if (!queryRis.Any())
            {
                return(new List <IAuction>());
            }

            List <IAuction> allAuctions = new List <IAuction>();

            foreach (var item in queryRis)
            {
                if (!onlyNotEnded || item.EndsOn < Now)
                {
                    allAuctions.Add(item);
                }
            }


            return(allAuctions);
        }
Esempio n. 6
0
        public int GetTheTimezoneOf(string connectionString, string name)
        {
            if (connectionString == null || name == null)
            {
                throw new ArgumentNullException();
            }
            if (name.Length < DomainConstraints.MinSiteName || name.Length > DomainConstraints.MaxSiteName)
            {
                throw new ArgumentException();
            }

            if (!isValidConnection(connectionString))
            {
                throw new UnavailableDbException();
            }

            SiteFactoryContext ctx = new SiteFactoryContext(connectionString);

            var queryRis = ctx.Sites.SqlQuery("select * from dbo.Sites where Name = '" + name + "'").ToList <Site>();

            ctx.Database.Connection.Close();

            //if (queryRis.Count<Site>() == 0)
            if (!queryRis.Any())
            {
                throw new InexistentNameException(name);
            }

            return(queryRis.ToArray <Site>()[0].Timezone);
        }
Esempio n. 7
0
        public void CreateSiteOnDb(string connectionString, string name, int timezone, int sessionExpirationTimeInSeconds, double minimumBidIncrement)
        {
            if (connectionString == null || name == null)
            {
                throw new ArgumentNullException();
            }
            if (name.Length < DomainConstraints.MinSiteName || name.Length > DomainConstraints.MaxSiteName)
            {
                throw new ArgumentException();
            }
            if (timezone < DomainConstraints.MinTimeZone || timezone > DomainConstraints.MaxTimeZone || sessionExpirationTimeInSeconds < 0 || minimumBidIncrement < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (!isValidConnection(connectionString))
            {
                throw new UnavailableDbException();
            }

            SiteFactoryContext ctx = new SiteFactoryContext(connectionString);

            if (this.isSiteAlreadyInDB(name, ctx))
            {
                throw new NameAlreadyInUseException(name);
            }

            Site newSite = new Site(name, minimumBidIncrement, sessionExpirationTimeInSeconds, timezone, connectionString, ctx, null);

            ctx.Sites.Add(newSite);

            ctx.SaveChanges();

            ctx.Database.Connection.Close();
        }
Esempio n. 8
0
        private bool isValidConnection(string connStr)
        {
            if (connStr == null)
            {
                throw new ArgumentNullException();
            }

            using (var db = new SiteFactoryContext(connStr))
            {
                try
                {
                    db.Database.Connection.Open();

                    if (db.Database.Connection.State == ConnectionState.Open)
                    {
                        Console.WriteLine(@"INFO: ConnectionString: " + db.Database.Connection.ConnectionString
                                          + "\n DataBase: " + db.Database.Connection.Database
                                          + "\n DataSource: " + db.Database.Connection.DataSource
                                          + "\n ServerVersion: " + db.Database.Connection.ServerVersion
                                          + "\n TimeOut: " + db.Database.Connection.ConnectionTimeout);

                        db.Database.Connection.Close();

                        return(true);
                    }
                    return(false);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                    return(false);
                }
            }
        }
Esempio n. 9
0
        public User(Site currSite, string user, string pwd, SiteFactoryContext fc)
        {
            UserSiteName = currSite.Name;
            Username     = user;
            Pwd          = pwd;

            this.factoryContext = fc;
        }
Esempio n. 10
0
        private void SaveValidUntil()
        {
            if (factoryContext == null)
            {
                this.factoryContext = new SiteFactoryContext(SessionSite.ConnectionString);
            }
            factoryContext.Database.ExecuteSqlCommand("update dbo.Sessions set ValidUntil = '" + ValidUntil + "' where Id = '" + this.Id + "'");

            factoryContext.Database.Connection.Close();
        }
Esempio n. 11
0
        private bool IsAuctionInDB(int auctionId)
        {
            this.factoryContext = new SiteFactoryContext(AuctionSite.ConnectionString);
            var queryResult = factoryContext.Auctions.SqlQuery("select * from dbo.Auctions where Id = " + auctionId).ToList <Auction>();

            factoryContext.Database.Connection.Close();

            //return queryResult.Count<Auction>() > 0;
            return(queryResult.Any());
        }
Esempio n. 12
0
        private void SaveLogin(ISession session)
        {
            if (factoryContext == null)
            {
                this.factoryContext = new SiteFactoryContext(this.ConnectionString);
            }
            factoryContext.Database.ExecuteSqlCommand("update dbo.Sessions set ValidUntil = '" + session.ValidUntil + "' where Id = '" + session.Id + "'");
            factoryContext.Database.ExecuteSqlCommand("update dbo.Sessions set IsSessionActive = 'true' where Id = '" + session.Id + "'");

            factoryContext.Database.Connection.Close();
        }
Esempio n. 13
0
        public void Delete()
        {
            if (!this.IsAuctionInDB(this.Id))
            {
                throw new InvalidOperationException();
            }

            /*Delete on DB*/
            this.factoryContext = new SiteFactoryContext(AuctionSite.ConnectionString);
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Auctions where Id = " + this.Id);

            factoryContext.Database.Connection.Close();
        }
Esempio n. 14
0
        private bool isSiteAlreadyInDB(string siteName, SiteFactoryContext db)
        {
            if (siteName == null)
            {
                throw new ArgumentNullException();
            }
            var queryResult = db.Sites.SqlQuery("select * from dbo.Sites where Name = '" + siteName + "'").ToList <Site>();

            db.Database.Connection.Close();

            //return queryResult.Count<Site>() > 0;
            return(queryResult.Any());
        }
Esempio n. 15
0
        public void Delete()
        {
            if (!this.IsSessionExist())
            {
                throw new InvalidOperationException();
            }

            this.factoryContext = new SiteFactoryContext(SessionSite.ConnectionString);
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Sessions where Id = '" + this.Id + "'");
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Auctions where CurrSellerUserName = '******'");

            factoryContext.Database.Connection.Close();
        }
Esempio n. 16
0
        public bool IsSiteExist(string userName)
        {
            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            var queryResult = factoryContext.Sites.SqlQuery("select * from dbo.Sites where Name = '" + this.Name + "'").ToList <Site>();

            factoryContext.Database.Connection.Close();

            //return queryResult.Count<Site>() > 0;
            return(queryResult.Any());
        }
Esempio n. 17
0
        private bool IsSessionExist()
        {
            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(SessionSite.ConnectionString);
            }

            var queryResult = factoryContext.Session.SqlQuery("select * from dbo.Sessions where Id = '" + this.Id + "'").ToList <Session>();

            factoryContext.Database.Connection.Close();

            //return queryResult.Count<Session>() > 0;
            return(queryResult.Any());
        }
Esempio n. 18
0
        public void Logout()
        {
            if (!this.IsValid())
            {
                throw new InvalidOperationException();
            }

            IsSessionActive = false;

            this.factoryContext = new SiteFactoryContext(SessionSite.ConnectionString);
            factoryContext.Database.ExecuteSqlCommand("update dbo.Sessions set IsSessionActive = 'false' where Id = '" + this.Id + "'");

            factoryContext.Database.Connection.Close();
        }
Esempio n. 19
0
        public bool IsUserInSite(string username)
        {
            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            var queryRis =
                factoryContext.Users.SqlQuery("select * from dbo.Users where UserSiteName = '" + Name +
                                              "' and UserName='******'").ToList <User>();

            factoryContext.Database.Connection.Close();

            return(queryRis.Any());
        }
Esempio n. 20
0
        public Auction(int id, User seller, string descr, DateTime endTime, double startPrice, Site site, SiteFactoryContext fc)
        {
            this.Id = id;

            this.Description = descr;
            this.EndsOn      = endTime;

            this.StartPrice          = startPrice;
            this.AuctionCurrentPrice = startPrice;
            this.Cmo = 0;

            CurrSellerUserName = seller.Username;
            AuctionSiteName    = site.Name;

            this.factoryContext = fc;
        }
Esempio n. 21
0
        public Session(string id, User owner, DateTime validUntil, bool isThisSessionActive, Site site, SiteFactoryContext fc)
        {
            this.Id = id;

            this.SessionOwnerUsername = owner.Username;

            this.ValidUntil = validUntil;

            this.IsSessionActive = isThisSessionActive;

            this.SessionSiteName = site.Name;

            //this.SessionSite = site;

            this.factoryContext = fc;

            this.IsSessionActive = true;
        }
Esempio n. 22
0
        public void Delete()
        {
            if (!this.IsSiteExist(this.Name))
            {
                throw new InvalidOperationException();
            }

            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Auctions where AuctionSiteName = '" + this.Name + "'");
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Sessions where SessionSiteName = '" + this.Name + "'");
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Users where UserSiteName = '" + this.Name + "'");
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Sites where Name = '" + this.Name + "'");

            factoryContext.Database.Connection.Close();
        }
Esempio n. 23
0
        public Site(string name, double minBidIncr, int sessionExpiration, int timezone, string connStr, SiteFactoryContext fc, IAlarmClock alarmClock)
        {
            this.Name = name;
            this.MinimumBidIncrement        = minBidIncr;
            this.SessionExpirationInSeconds = sessionExpiration;
            this.Timezone = timezone;

            this.ConnectionString = connStr;

            this.alarmClock = alarmClock;

            if (this.alarmClock != null)
            {
                siteAlarm = this.alarmClock.InstantiateAlarm(300000);
                siteAlarm.RingingEvent += SiteAlarm_RingingEvent;
            }

            this.factoryContext = fc;
        }
Esempio n. 24
0
        public ISite LoadSite(string connectionString, string name, IAlarmClock alarmClock)
        {
            if (connectionString == null || name == null || alarmClock == null)
            {
                throw new ArgumentNullException();
            }
            if (!isValidConnection(connectionString))
            {
                throw new UnavailableDbException();
            }
            if (name.Length < DomainConstraints.MinSiteName || name.Length > DomainConstraints.MaxSiteName)
            {
                throw new ArgumentException();
            }

            SiteFactoryContext ctx = new SiteFactoryContext(connectionString);

            var queryRis = ctx.Sites.SqlQuery("select * from dbo.Sites where Name = '" + name + "'").ToList <Site>();

            ctx.Database.Connection.Close();

            //if (queryRis.Count<Site>() == 0)
            if (!queryRis.Any())
            {
                throw new InexistentNameException(name);
            }

            Site theSite = queryRis.ToArray <Site>()[0];

            if (theSite.Timezone != alarmClock.Timezone)
            {
                throw new ArgumentException();
            }

            //return theSite;
            return(new Site(theSite.Name, theSite.MinimumBidIncrement, theSite.SessionExpirationInSeconds,
                            theSite.Timezone, connectionString, ctx, alarmClock));
        }
Esempio n. 25
0
        public ISession GetSession(string sessionId)
        {
            //if (sessionId == null || sessionId == string.Empty)
            if (string.IsNullOrEmpty(sessionId))
            {
                throw new ArgumentNullException();
            }

            if (!this.IsSiteExist(this.Name))
            {
                throw new InvalidOperationException();
            }

            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            var queryRis = factoryContext.Session.SqlQuery(
                "select * from dbo.Sessions where SessionSiteName = '" + Name + "' and Id = '" + sessionId + "'").ToList <Session>();

            factoryContext.Database.Connection.Close();

            if (!queryRis.Any())
            {
                return(null);
            }

            Session s = queryRis.ToArray <Session>()[0];

            if (!s.IsValid())
            {
                return(null);
            }

            return(s);
        }
Esempio n. 26
0
        public bool BidOnAuction(ISession theSession, double offer)
        {
            Session session = theSession as Session;

            if (AuctionSite.Now > EndsOn || !this.IsAuctionInDB(this.Id))
            {
                throw new InvalidOperationException();
            }
            if (offer < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (session == null)
            {
                throw new ArgumentNullException();
            }
            //if (!session.IsValid() || session.User == Seller || (session.User as User).UserSite != (Seller as User).UserSite)
            if (!session.IsValid() || session.User.Equals(Seller) || (session.User as User).UserSite.Name != (Seller as User).UserSite.Name)
            {
                throw new ArgumentException();
            }

            bool isValid = true;

            //if (session.User as User == AuctionCurrentWinner as User && offer < (Cmo + AuctionSite.MinimumBidIncrement))
            if ((session.User as User).Equals(AuctionCurrentWinner) && offer < (Cmo + AuctionSite.MinimumBidIncrement))
            {
                isValid = false;
            }
            //else if (session.User as User != AuctionCurrentWinner && offer < AuctionCurrentPrice)
            else if (!(session.User as User).Equals(AuctionCurrentWinner) && offer < AuctionCurrentPrice)
            {
                isValid = false;
            }
            //else if (session.User as User != AuctionCurrentWinner && offer < (AuctionCurrentPrice + AuctionSite.MinimumBidIncrement) && Cmo != 0)
            else if (!(session.User as User).Equals(AuctionCurrentWinner) && offer < (AuctionCurrentPrice + AuctionSite.MinimumBidIncrement) && Cmo != 0)
            {
                isValid = false;
            }
            else if (Cmo == 0)
            {
                Cmo = offer;
                AuctionCurrentWinner = session.User as User;
            }
            //else if (session.User as User == AuctionCurrentWinner)
            else if ((session.User as User).Equals(AuctionCurrentWinner))
            {
                Cmo = offer;
            }
            //else if (Cmo != 0 && session.User as User != AuctionCurrentWinner && offer > Cmo)
            else if (Cmo != 0 && !(session.User as User).Equals(AuctionCurrentWinner) && offer > Cmo)
            {
                AuctionCurrentWinner = session.User as User;
                AuctionCurrentPrice  = min(offer, (Cmo + AuctionSite.MinimumBidIncrement));
                Cmo = offer;
            }
            //else if (Cmo != 0 && session.User as User != AuctionCurrentWinner && offer <= Cmo)
            else if (Cmo != 0 && !(session.User as User).Equals(AuctionCurrentWinner) && offer <= Cmo)
            {
                AuctionCurrentPrice = min((offer + AuctionSite.MinimumBidIncrement), Cmo);
            }

            //In ogni caso faccio il refresh della validità della Session dopo una offerta

            session.ValidUntil = session.ValidUntil.AddSeconds(AuctionSite.SessionExpirationInSeconds);

            if (factoryContext == null)
            {
                this.factoryContext = new SiteFactoryContext(AuctionSite.ConnectionString);
            }

            factoryContext.Database.ExecuteSqlCommand("update dbo.Sessions set ValidUntil = '" + session.ValidUntil + "' where Id = '" + session.Id + "'");

            if (isValid)
            {
                factoryContext.Database.ExecuteSqlCommand("update dbo.Auctions set AuctionCurrentWinnerUsername = '******' where Id = " + this.Id);

                string query = "update dbo.Auctions set AuctionCurrentPrice = " + AuctionCurrentPrice.ToString().Replace(',', '.') + " where Id = " +
                               this.Id;
                factoryContext.Database.ExecuteSqlCommand(query);
                factoryContext.Database.ExecuteSqlCommand("update dbo.Auctions set Cmo = " +
                                                          Cmo.ToString().Replace(',', '.') + " where Id = " + this.Id);
            }

            factoryContext.Database.Connection.Close();

            return(isValid);
        }
Esempio n. 27
0
        public ISession Login(string username, string password)
        {
            if (username == null || password == null)
            {
                throw new ArgumentNullException();
            }
            if (username.Length < DomainConstraints.MinUserName || username.Length > DomainConstraints.MaxUserName || password.Length < DomainConstraints.MinUserPassword)
            {
                throw new ArgumentException();
            }

            if (!this.IsSiteExist(this.Name))
            {
                throw new InvalidOperationException();
            }

            if (!IsUserInSite(username))
            {
                return(null);
            }

            User theUser = this.GetUserByUsername(username);

            if (theUser.Pwd != password)
            {
                return(null);
            }


            theUser = new User(this, username, password, factoryContext);

            Session currSession       = null;
            IEnumerator <ISession> en = this.GetSessions().GetEnumerator();

            while (en.MoveNext())
            {
                currSession = en.Current as Session;

                if (currSession.User.Username == username && (currSession.User as User).Pwd == password &&
                    !currSession.IsSessionExpired())
                {
                    currSession.IsSessionActive = true;
                    //currSession.ValidUntil = currSession.ValidUntil.AddSeconds(this.SessionExpirationInSeconds);
                    currSession.ValidUntil = Now.AddSeconds(this.SessionExpirationInSeconds);
                    SaveLogin(currSession);
                    return(currSession);
                }
            }

            en.Dispose();

            if (factoryContext == null)
            {
                factoryContext = new SiteFactoryContext(this.ConnectionString);
            }

            int     sessionId  = factoryContext.Session.Count <Session>();
            Session newSession = new Session(sessionId.ToString(), theUser, Now.AddSeconds(this.SessionExpirationInSeconds), true, this, factoryContext);

            //factoryContext.Sites.Attach(this);

            //factoryContext.Entry(theUser).State = EntityState.Added;
            //factoryContext.Users.Attach(theUser);

            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //factoryContext.Configuration.ProxyCreationEnabled = false;
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            factoryContext.Session.Add(newSession);
            factoryContext.SaveChanges();

            factoryContext.Database.Connection.Close();

            return(newSession);
        }
Esempio n. 28
0
        public void Delete()
        {
            if (!UserSite.IsUserInSite(this.Username))
            {
                throw new InvalidOperationException();
            }

            IAuction currAuction = null;
            IEnumerable <IAuction> allOpenAuctions = UserSite.GetAuctions(true); //Solo aste aperte
            IEnumerator <IAuction> auctionIterator = allOpenAuctions.GetEnumerator();

            while (auctionIterator.MoveNext())
            {
                currAuction = auctionIterator.Current;

                if ((currAuction.Seller as User).Equals(this) || (currAuction.CurrentWinner() as User).Equals(this))
                {
                    throw new InvalidOperationException();
                }
            }

            auctionIterator.Dispose();

            /*
             * Se arrivo qui vuol dire che l'utente può essere tranquillamente cancellato in quanto
             * non fa parte di aste in corso
             */

            this.factoryContext = new SiteFactoryContext(UserSite.ConnectionString);

            IEnumerable <IAuction> allAuctions = UserSite.GetAuctions(false);

            auctionIterator = allAuctions.GetEnumerator();

            while (auctionIterator.MoveNext())
            {
                currAuction = auctionIterator.Current;
                if ((currAuction.Seller as User).Equals(this))
                {
                    /*
                     * Se sono arrivato a poter fare questo if vuol dire che sto valutando
                     * per forza un'asta chiusa
                     * (se fosse aperta sarei caduto nell'exception precedente!)
                     */
                    currAuction.Delete();
                }
                else
                {
                    if ((currAuction.CurrentWinner() as User).Equals(this))
                    {
                        /*
                         * Stesso discorso di sopra;
                         * se sono arrivato qui sono sicuro che quest'asta è chiusa altrimenti
                         * sarei caduto nell'exception precedente!
                         */

                        /*Update Auction on DB*/
                        factoryContext.Database.ExecuteSqlCommand("update dbo.Auctions set AuctionCurrentWinner_Username = '' where Id = " + currAuction.Id);
                    }
                }
            }

            auctionIterator.Dispose();

            /*In ogni caso cancello l'utente dal db*/
            factoryContext.Database.ExecuteSqlCommand("delete from dbo.Users where Username = '******'");

            factoryContext.Database.Connection.Close();
        }