/// <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); }
/// <summary> /// Prevents a default instance of the <see cref="DomainDataStorage"/> class from being created. /// </summary> private DomainDataStorage() { string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString; this.databaseConneection = new MySqlConnection(connectionString); this.auctionTable = this.databaseConneection.As <IAuctionTable>(); this.bidTable = this.databaseConneection.As <IBidTable>(); this.categoryTable = this.databaseConneection.As <ICategoryTable>(); this.currencyTable = this.databaseConneection.As <ICurrencyTable>(); this.personBidderTable = this.databaseConneection.As <IPersonBidderTable>(); this.personMarkTable = this.databaseConneection.As <IPersonMarkTable>(); this.personOfferorTable = this.databaseConneection.As <IPersonOfferorTable>(); this.personTable = this.databaseConneection.As <IPersonTable>(); this.productTable = this.databaseConneection.As <IProductTable>(); }
/// <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); } }
/// <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; } }
/// <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); }