private void CloseAuctions() { logger.InfoFormat("CloseAuctions: {0}", JsonConvert.SerializeObject(new { })); try { using (AuctionDB db = new AuctionDB()) { var now = DateTime.UtcNow; var auctions = db.Auctions.Where(a => a.ClosingTime <= now && a.Status != AuctionStatus.COMPLETED).Include(a => a.Bids.Select(b => b.User)).ToList(); foreach (var auction in auctions) { logger.InfoFormat("CloseAuctions: Auction {0} is closed", JsonConvert.SerializeObject(new { auction.Id, auction.Name, auction.CurrentPrice, auction.Currency, })); auction.Status = AuctionStatus.COMPLETED; db.Entry(auction).State = EntityState.Modified; // za svakog korisnika vraca najveci bid var results = auction.Bids.GroupBy(b => b.User).Select(g => new { tokens = g.Max(b => b.NumberOfTokens), user = g.Key }); // vratiti tokene svima koji nisu pobedili foreach (var result in results) { // nije pobednik if (auction.User != result.user) { logger.InfoFormat("CloseAuctions: user is getting tokens back {0}", JsonConvert.SerializeObject(new { result.tokens, result.user.UserName })); result.user.NumberOfTokens += result.tokens; db.Entry(result.user).State = EntityState.Modified; } } } db.SaveChanges(); } } catch (Exception e) { logger.Error("CloseAuctions", e); } }
public bool SetDefaultValues(EditDefaultValuesViewModel model) { logger.InfoFormat("SetDefaultValues: {0}", JsonConvert.SerializeObject(new { model })); if (model == null) { logger.ErrorFormat("GetEditDefaultValues: {0} model is null", JsonConvert.SerializeObject(new { })); return(false); } try { using (AuctionDB db = new AuctionDB()) { DefaultValues defaultValues = db.DefaultValues.SingleOrDefault(); if (defaultValues == null) { logger.InfoFormat("SetDefaultValues: {0} defaultValues is null", JsonConvert.SerializeObject(new { })); defaultValues = new DefaultValues(); db.Entry(defaultValues).State = EntityState.Added; } else { db.Entry(defaultValues).State = EntityState.Modified; } defaultValues.AuctionDuration = model.AuctionDuration; defaultValues.Currency = model.Currency; defaultValues.NumberOfAuctionsPerPage = model.NumberOfAuctionsPerPage; defaultValues.SilverTokenNumber = model.SilverTokenNumber; defaultValues.GoldTokenNumber = model.GoldTokenNumber; defaultValues.PlatinuTokenNumber = model.PlatinuTokenNumber; defaultValues.TokenValue = model.TokenValue; db.SaveChanges(); logger.InfoFormat("SetDefaultValues: {0} new defaultValues", JsonConvert.SerializeObject(new { })); return(true); } } catch (Exception e) { logger.Error("SetDefaultValues", e); } //something went wrong return(false); }
public bool SetOrderStatus(string orderID, OrderStatus status) { logger.InfoFormat("SetOrderStatus: {0} ", JsonConvert.SerializeObject(new { orderID, status })); try { using (AuctionDB db = new AuctionDB()) { var order = db.Orders.Include(a => a.User).Where(o => o.Id.ToString() == orderID).SingleOrDefault(); if (order == null) { logger.ErrorFormat("SetOrderStatus: order {0} is null ", JsonConvert.SerializeObject(new { order })); return(false); } order.Status = status; if (status == OrderStatus.COMPLETED) { // add tokens to the user order.User.NumberOfTokens += order.NumberOfTokens; db.Entry(order.User).State = EntityState.Modified; } db.Entry(order).State = EntityState.Modified; db.SaveChanges(); logger.InfoFormat("SetOrderStatus: updated order {0} ", JsonConvert.SerializeObject(new { order.Id, order.NumberOfTokens, order.Status, order.User.UserName })); return(true); } } catch (Exception e) { logger.Error("SetOrderStatus", e); } return(false); }
public bool OpenAuction(string id) { logger.InfoFormat("OpenAuction: {0}", JsonConvert.SerializeObject(new { id })); try { if (String.IsNullOrEmpty(id)) { logger.ErrorFormat("OpenAuction: {0} id is null", JsonConvert.SerializeObject(new { id })); return(false); } var guidId = new Guid(id); using (AuctionDB db = new AuctionDB()) { var auction = db.Auctions.Where(a => a.Id.Equals(guidId)).SingleOrDefault(); if (auction == null) { logger.ErrorFormat("OpenAuction: Auction {0} doesn't exist", JsonConvert.SerializeObject(new { auction })); return(false); } auction.OpeningTime = DateTime.UtcNow; auction.ClosingTime = DateTime.UtcNow.Add(TimeSpan.FromSeconds(auction.Duration)); auction.Status = AuctionStatus.OPENED; db.Entry(auction).State = EntityState.Modified; db.SaveChanges(); logger.InfoFormat("OpenAuction: Auction is {0} open", JsonConvert.SerializeObject(new { auction.Id, auction.Name, auction.OpeningTime, auction.Duration, auction.ClosingTime, auction.CurrentPrice, auction.Currency })); return(true); } } catch (Exception e) { logger.Error("OpenAuction", e); } // something went wrong return(false); }
public bool MakeBid(string auctionId, long?offerTokens, string userId) { logger.InfoFormat("MakeBid: {0} ", JsonConvert.SerializeObject(new { auctionId, offerTokens, userId })); if (auctionId == null || offerTokens == null) { logger.ErrorFormat("MakeBid: something is null {0} ", JsonConvert.SerializeObject(new { auctionId, offerTokens, })); return(false); } try { var guidId = new Guid(auctionId); using (AuctionDB db = new AuctionDB()) { CloseAuctions(); var auction = db.Auctions.Where(a => a.Id.Equals(guidId)).Include(a => a.Bids).SingleOrDefault(); var user = db.Users.Where(u => u.Id.Equals(userId)).SingleOrDefault(); var dv = db.DefaultValues.SingleOrDefault(); if (auction == null || user == null || dv == null || auction.Status != AuctionStatus.OPENED) { logger.ErrorFormat("MakeBid: something is null or auction is not open {0} ", JsonConvert.SerializeObject(new { auction, user, dv })); return(false); } var maxBid = auction.Bids.Max(b => (long?)b.NumberOfTokens); if (maxBid == null) { maxBid = (long?)(Math.Ceiling(auction.CurrentPrice / dv.TokenValue)); } else { // offer should be one token more than the current max bid maxBid++; } // offer is too low if (offerTokens < maxBid) { return(false); } // max offer of this user for this auction var userMaxBid = auction.Bids.Where(b => b.User.Id.Equals(userId)).Max(b => (long?)b.NumberOfTokens); long tokensToPay = 0; if (userMaxBid == null) { // this is users first bid tokensToPay = (long)offerTokens; } else { // this is not users first bid, should pay only the diffeerence tokensToPay = (long)offerTokens - (long)userMaxBid; } if (user.NumberOfTokens < tokensToPay) { logger.ErrorFormat("MakeBid: user doesn't have enough tokens {0} ", JsonConvert.SerializeObject(new { tokensToPay, user.UserName, })); // there is not enough tokens return(false); } // update auction last bidder auction.User = user; auction.CurrentPrice = (long)offerTokens * dv.TokenValue; //db.Entry(auction).State = EntityState.Modified; try { db.SaveChanges(); } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { // catch possible concurrency exception logger.Error("Make Bid", e); return(false); } // update user token user.NumberOfTokens -= tokensToPay; db.Entry(user).State = EntityState.Modified; // create and add new bid Bid bid = new Bid { Id = Guid.NewGuid(), Auction = auction, NumberOfTokens = (long)offerTokens, PlacingTime = DateTime.UtcNow, User = user }; db.Bids.Add(bid); db.SaveChanges(); logger.InfoFormat("MakeBid: new bid and updated auction {0} ", JsonConvert.SerializeObject(new { bid.Auction.Name, bid.PlacingTime, bid.NumberOfTokens, bid.User.UserName, auctionUser = auction.User.UserName, auction.CurrentPrice })); return(true); } } catch (Exception e) { logger.Error("MakeBid", e); } return(false); }