Ejemplo n.º 1
0
        /// <summary>
        /// Does the check.
        /// </summary>
        /// <param name="personOfferor">The person offer.</param>
        /// <param name="auction">The auction.</param>
        /// <param name="offerorAuctions">The offer person auctions.</param>
        /// <param name="allProducts">All products.</param>
        /// <returns>
        /// true if the auction can be posted.
        /// </returns>
        public static bool DoCheck(PersonOfferor personOfferor, Auction auction, List <Auction> offerorAuctions, List <Product> allProducts)
        {
            PersonOfferorService offerorService = new PersonOfferorService(personOfferor);

            //// if it's banned.
            if (offerorService.IsBanned)
            {
                return(false);
            }

            //// can't have more than this.
            bool hasMaxAuctions = offerorService.DidPersonHitMaxListLimit(personOfferor, offerorAuctions);

            if (hasMaxAuctions)
            {
                return(false);
            }

            //// can't have more than this in specified category.
            List <Auction> offerorCategoryAuctions = new List <Auction>();

            foreach (Auction listed_auction in offerorAuctions)
            {
                if (listed_auction.Product.Category.Name.Equals(auction.Product.Category.Name))
                {
                    offerorCategoryAuctions.Add(listed_auction);
                }
            }

            bool hasMaxInCategory = offerorService.DidPersonHitMaxCategoryListLimit(personOfferor, auction.Product.Category, offerorCategoryAuctions);

            if (hasMaxInCategory)
            {
                return(false);
            }

            AuctionService auctionService = new AuctionService(auction);

            //// if it's older
            if (DateTime.Now.CompareTo(auction.StartDate) > 0)
            {
                //// it should not be older than 5 min.
                bool olderThanMinutes = (DateTime.Now - auction.StartDate).TotalMinutes > 5;
                if (olderThanMinutes)
                {
                    ////return false;
                }
            }

            bool anySimilarExists = ExistsAnySimilarProductCheck.DoCheck(auction.Product, allProducts);

            //// should not be similar.
            if (anySimilarExists)
            {
                Log.Info(auction.Product.Name + " already exists due to LevensteinDistance!");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void DoCheckOfferor_ShouldThrow()
        {
            PersonOfferor offeror        = null;
            var           auctionServcie = InstanceHelper.GetAuctionService(null);

            Assert.ThrowsAny <Exception>(() => CanOfferorEndAuctionCheck.DoCheck(offeror, auctionServcie));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PersonOfferorService"/> class.
        /// </summary>
        /// <param name="offeror">The offer person.</param>
        public PersonOfferorService(PersonOfferor offeror)
        {
            offeror.ValidateObject();

            this.Offeror = offeror;

            this.UpdateIsBanned();
        }
Ejemplo n.º 4
0
        public void CreatePersonOfferor_ShouldThrow()
        {
            var personOfferor = new PersonOfferor();

            personOfferor.Person = null;

            Assert.ThrowsAny <Exception>(() => personOfferor.ValidateObject());
        }
Ejemplo n.º 5
0
 public static PersonOfferorService GetPersonOfferorService(PersonOfferor personOfferor)
 {
     if (personOfferor != null)
     {
         return(new PersonOfferorService(personOfferor));
     }
     return(new PersonOfferorService(GetPersonOfferor()));
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Did the person hit maximum list limit.
        /// </summary>
        /// <param name="offeror">The offer person.</param>
        /// <param name="auctions">The auctions.</param>
        /// <returns>
        /// true if he did, false either
        /// </returns>
        public bool DidPersonHitMaxListLimit(PersonOfferor offeror, List <Auction> auctions)
        {
            PersonOfferorService personOfferorService = new PersonOfferorService(offeror);

            int counted = personOfferorService.CountAllActiveAuctions(auctions);

            int max_in_progress = maxInProgress;

            return(counted >= max_in_progress);
        }
Ejemplo n.º 7
0
        public void CreatePersonOfferor_ShouldInstantiatePersonOfferor()
        {
            Person person        = new Person();
            var    personOfferor = new PersonOfferor();

            personOfferor.Person = person;

            personOfferor.ValidateObject();
            Assert.NotNull(personOfferor);
        }
Ejemplo n.º 8
0
        public void CreatePersonOfferor_ShouldThrowBadId(int id)
        {
            var personOfferor = new PersonOfferor {
                IdOfferor      = id,
                LastBannedDate = DateTime.Now,
                Person         = new Person()
            };

            Assert.ThrowsAny <Exception>(() => personOfferor.ValidateObject());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Did the person hit maximum category list limit.
        /// </summary>
        /// <param name="offeror">The offer person.</param>
        /// <param name="category">The category.</param>
        /// <param name="auctions">The auctions.</param>
        /// <returns>
        /// true if he did, false either
        /// </returns>
        public bool DidPersonHitMaxCategoryListLimit(PersonOfferor offeror, Category category, List <Auction> auctions)
        {
            PersonOfferorService personOfferorService = new PersonOfferorService(offeror);

            int counted = personOfferorService.CountActiveAuctionsInCategory(category, auctions);

            int max_in_category = maxInProgressByCategory;

            return(counted >= max_in_category);
        }
Ejemplo n.º 10
0
        //[InlineData(-1, null, null, null)]
        public void PersonOfferorMark_ShouldThrow(int mark, Person sender, PersonOfferor receiver, DateTime dateOccur)
        {
            PersonOfferorMark pom = new PersonOfferorMark();

            pom.DateOccur = dateOccur;
            pom.Mark      = mark;
            pom.Sender    = sender;
            pom.Receiver  = receiver;

            Assert.ThrowsAny <Exception>(() => pom.ValidateObject());
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Registers the person.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <returns>
        /// true if it succeeds
        /// </returns>
        /// <exception cref="System.Exception">Person is already registered!</exception>
        public Person RegisterPerson(Person person)
        {
            PersonOfferor offeror;
            PersonBidder  bidder;

            IPersonTable        personTable        = this.tablesProvider.GetPersonTable();
            IPersonBidderTable  personBidderTable  = this.tablesProvider.GetPersonBidderTable();
            IPersonOfferorTable personOfferorTable = this.tablesProvider.GetPersonOfferorTable();

            try
            {
                if (person.IdPerson != 0)
                {
                    throw new Exception("Person is already registered!");
                }

                person.ValidateObject();

                Person exists = personTable.FetchPersonByPhone(person.Phone);

                if (exists != null)
                {
                    person = exists; //// to update the id.
                    return(person);
                }
            }
            catch (Exception e)
            {
                Log.Info("RegisterPerson: " + e.Message);
                throw e;
            }

            //// at this point person doesn't exist into db.
            //// the actual insert
            personTable.InsertPerson(person);
            person = personTable.FetchPersonByPhone(person.Phone); //// in order to update Id

            offeror = new PersonOfferor()
            {
                Person = person
            };
            bidder = new PersonBidder()
            {
                Person = person
            };

            personOfferorTable.InsertPersonOfferor(person.IdPerson, offeror);
            personBidderTable.InsertPersonBidder(person.IdPerson, bidder);

            Log.Info("RegisterPerson: " + person.Name + " person id =" + person.IdPerson + " inserted with success.");

            return(person);
        }
        public void InsertPersonOfferor(int idPerson, PersonOfferor personOfferor)
        {
            PersonOfferor offeror = FetchPersonOfferorByPerson(personOfferor.Person);

            if (offeror != null)
            {
                return;
            }

            personOfferor.IdOfferor = index++;
            list.Add(personOfferor);
        }
Ejemplo n.º 13
0
        public List <Auction> FetchOfferorAuctions(PersonOfferor offeror)
        {
            List <Auction> auctions = new List <Auction>();

            foreach (Auction auction in list)
            {
                if (auction.PersonOfferor.IdOfferor == offeror.IdOfferor)
                {
                    auctions.Add(auction);
                }
            }
            return(auctions);
        }
Ejemplo n.º 14
0
        public List <PersonOfferorMark> FetchPersonOfferorMarks(PersonOfferor offeror)
        {
            List <PersonOfferorMark> result = new List <PersonOfferorMark>();

            foreach (PersonOfferorMark mark in list)
            {
                if (mark.Receiver.IdOfferor == offeror.IdOfferor)
                {
                    result.Add(mark);
                }
            }

            return(result);
        }
Ejemplo n.º 15
0
        public List <Auction> FetchOfferorAuctionsByCategory(PersonOfferor offerer, Category category)
        {
            List <Auction> auctions = new List <Auction>();

            foreach (Auction auction in list)
            {
                if (auction.PersonOfferor.IdOfferor == offerer.IdOfferor && auction.Product.Category.Name.Equals(category.Name))
                {
                    auctions.Add(auction);
                }
            }

            return(auctions);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Ends the auction.
        /// </summary>
        /// <param name="offeror">The offer person.</param>
        public void EndAuction(PersonOfferor offeror)
        {
            try
            {
                this.Auction.ValidateObject();
                CanOfferorEndAuctionCheck.DoCheck(offeror, this);
            }
            catch (Exception e)
            {
                throw e;
            }

            this.IsActive        = false;
            this.Auction.EndDate = DateTime.Now;
            this.HadEnded        = true;
        }
        /// <summary>
        /// Does the check.
        /// </summary>
        /// <param name="offerPerson">The offer person.</param>
        /// <param name="auction">The auction.</param>
        /// <exception cref="System.Exception">
        /// offer person is null!
        /// or
        /// offer person does not have enough privileges for this!
        /// or
        /// Auction is already ended!
        /// </exception>
        public static void DoCheck(PersonOfferor offerPerson, AuctionService auction)
        {
            if (offerPerson == null)
            {
                throw new Exception("offer person is null!");
            }

            if (offerPerson.IdOfferor != auction.Auction.PersonOfferor.IdOfferor)
            {
                // does not belongs to.
                throw new Exception("offer person does not have enough privileges for this!");
            }

            if (auction.HadEnded)
            {
                throw new Exception("Auction is already ended!");
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Posts the mark.
        /// </summary>
        /// <param name="fromPerson">From person.</param>
        /// <param name="toPerson">To person.</param>
        /// <param name="mark">The mark.</param>
        /// <exception cref="System.Exception">A PERSON IS NOT REGISTERED!</exception>
        public void PostMark(Person fromPerson, Person toPerson, int mark)
        {
            IPersonOfferorTable personOfferorTable = this.tablesProvider.GetPersonOfferorTable();
            IPersonMarkTable    personMarkTable    = this.tablesProvider.GetPersonMarkTable();

            fromPerson.ValidateObject();
            toPerson.ValidateObject();

            if (fromPerson.IdPerson == 0 || toPerson.IdPerson == 0)
            {
                throw new Exception("A PERSON IS NOT REGISTERED!");
            }

            PersonOfferor offeror = personOfferorTable.FetchPersonOfferorByPerson(toPerson);

            if (mark > MaxMarkRating || mark < MinMarkRating)
            {
                throw new Exception("BadRating");
            }

            PersonOfferorMark markObj = new PersonOfferorMark()
            {
                DateOccur = DateTime.Now, Mark = mark, Receiver = offeror, Sender = fromPerson
            };

            personMarkTable.InsertPersonMark(markObj);

            Log.Info("mark registered!");

            PersonOfferorService offerorService = new PersonOfferorService(offeror);

            List <PersonOfferorMark> marks = personMarkTable.FetchPersonOfferorMarks(offeror);

            offerorService.UpdateRatingBasedOnMarks(marks);

            if (offerorService.Rating < minRatingAllowedForBidding)
            {
                offeror.LastBannedDate = DateTime.Now;
                offerorService.UpdateIsBanned();
                personOfferorTable.UpdatePersonOfferor(offeror);
            }
        }
        public void UpdatePersonOfferor(PersonOfferor personOfferor)
        {
            PersonOfferor found = null;

            foreach (PersonOfferor offeror in list)
            {
                if (offeror.IdOfferor == personOfferor.IdOfferor)
                {
                    found = offeror;
                    break;
                }
            }

            if (found == null)
            {
                return;
            }

            found.LastBannedDate = personOfferor.LastBannedDate;
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Ends the auction.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="auction">The auction.</param>
        public void EndAuction(Person person, Auction auction)
        {
            IAuctionTable       auctionTable       = this.tablesProvider.GetAuctionTable();
            IPersonOfferorTable personOfferorTable = this.tablesProvider.GetPersonOfferorTable();

            try
            {
                PersonOfferor  offeror        = personOfferorTable.FetchPersonOfferorByPerson(person);
                AuctionService auctionService = new AuctionService(auction);

                auctionService.EndAuction(offeror);

                auctionTable.UpdateAuction(auctionService.Auction);
                Log.Info("auction ended!");
            }
            catch (Exception e)
            {
                Log.Info("EndAuction :" + e.Message);
                throw e;
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Registers the auction.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="auction">The auction.</param>
        /// <returns>
        /// False if the register auction has failed.
        /// </returns>
        /// <exception cref="System.Exception">Person is not registered!</exception>
        public Auction RegisterAuction(Person person, Auction auction)
        {
            IPersonOfferorTable personOfferorTable = this.tablesProvider.GetPersonOfferorTable();
            IProductTable       productTable       = this.tablesProvider.GetProductTable();
            IAuctionTable       auctionTable       = this.tablesProvider.GetAuctionTable();

            PersonOfferor offeror = personOfferorTable.FetchPersonOfferorByPerson(person);

            offeror.Person = person;
            Currency currency = auction.Currency;
            Category category = auction.Product.Category;

            try
            {
                auction.PersonOfferor = offeror ?? throw new Exception("BAD OFFEROR");

                auction.ValidateObject();

                auction.Currency.ValidateObject();
                auction.Product.ValidateObject();

                if (auction.Currency.IdCurrency == 0)
                {
                    throw new Exception("Invalid currency.");
                }

                Product product = productTable.FetchProductByAllAttributes(auction.Product.Category.IdCategory, auction.Product);
                if (product != null)
                {
                    product.Category = category;

                    Auction fetched = auctionTable.FetchAuctionByIds(offeror.IdOfferor, product.IdProduct);

                    if (fetched.IdAuction != 0)
                    {
                        fetched.PersonOfferor = offeror;
                        fetched.Product       = product;
                        fetched.Currency      = currency;

                        return(fetched);
                        ////throw new Exception("Auction is already registered");
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            List <Auction> offerorAuctions  = auctionTable.FetchOfferorAuctions(offeror);
            List <Product> existingProducts = productTable.FetchAllProducts();

            bool canPost = CanAuctionBePostedCheck.DoCheck(offeror, auction, offerorAuctions, existingProducts);

            if (!canPost)
            {
                throw new Exception("CanAuctionBePostedCheck failed!");
            }

            productTable.InsertProduct(category.IdCategory, auction.Product);

            Product selectedProduct = productTable.FetchProductByAllAttributes(category.IdCategory, auction.Product);

            selectedProduct.Category = category;

            auctionTable.InsertAuction(offeror.IdOfferor, selectedProduct.IdProduct, currency.IdCurrency, auction);

            auction = auctionTable.FetchAuctionByIds(offeror.IdOfferor, selectedProduct.IdProduct);
            auction.PersonOfferor = offeror;
            auction.Product       = selectedProduct;
            auction.Currency      = currency;

            this.auctions.Add(auction);

            AuctionService auctionService = new AuctionService(auction);

            auctionService.StartTimers();

            return(auction);
        }