public ActionResult Bid(Guid?auctionId, int?bidOffer) { using (var transaction = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { try { if (auctionId != null && bidOffer != null) { Auction auction = db.auctions.Where(a => a.id == auctionId).FirstOrDefault(); Bid bidToCompare = auction.bids.OrderByDescending(b => b.timeSent).FirstOrDefault(); int oldOffer = 0; if (bidToCompare != null) { oldOffer = bidToCompare.numOfTokens; } else { oldOffer = auction.tokenPrice; } User user = Session["User"] as User; User user1 = db.users.Where(u => u.id == user.id).FirstOrDefault(); if (bidOffer > oldOffer && bidOffer <= user.numOfTokens && user.id != auction.userIdCreate) { Bid bid = new Bid { userId = user.id, timeSent = System.DateTime.Now, auctionId = auction.id, numOfTokens = (int)bidOffer }; db.bids.Add(bid); user1.numOfTokens = user1.numOfTokens - (int)bidOffer; db.Entry(user1).State = EntityState.Modified; AdminSettings admin = db.adminSettings.FirstOrDefault(); auction.currPrice = (decimal)bidOffer * admin.T; auction.tokenPrice = (int)bidOffer; var hubContext = GlobalHost.ConnectionManager.GetHubContext <HubProjekat>(); hubContext.Clients.All.updatePage(auction.id, auction.currPrice, auction.currency, user1.mail, auction.tokenPrice); hubContext.Clients.All.bidUp(user1.name, user1.lastname, bid.timeSent, bid.numOfTokens, auction.currPrice, auction.id); hubContext.Clients.All.userUp(user1.numOfTokens, user1.id); db.Entry(auction).State = EntityState.Modified; db.SaveChanges(); transaction.Commit(); } } } catch (Exception e) { transaction.Rollback(); ViewBag.Message = "There is an error in transaction"; log.Error($"Error-not finished bid on {DateTime.Now}"); } } // var auctions = from a in db.auctions select a; return(RedirectToAction("AllAuctions", "Home")); //, auctions.ToList()); }
public ActionResult ChangeStatus(Guid id) { Auction auction = db.auctions.Where(a => a.id == id).FirstOrDefault(); auction.status = "OPENED"; auction.timeOpened = System.DateTime.Now; db.Entry(auction).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("AllAuctions", "Home")); }
public ActionResult Edit(EditViewModel model) { if (ModelState.IsValid) { User userAddit = Session["User"] as User; User userAddit1 = db.users.Where(u => u.id == userAddit.id).FirstOrDefault(); var passwordAddit = ""; using (MD5 md5Hash = MD5.Create()) { byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(model.Password); byte[] hashBytes = md5Hash.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); } passwordAddit = sb.ToString(); } userAddit.password = userAddit1.password = passwordAddit; userAddit.mail = userAddit1.mail = model.Email; userAddit.lastname = userAddit1.lastname = model.Lastname; userAddit.name = userAddit1.name = model.Firstname; db.Entry(userAddit1).State = EntityState.Modified; db.SaveChanges(); return(View("AboutUser", userAddit)); } return(View(model)); }
public ActionResult EndOfAuction(Guid?id) { using (var transaction = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { try { Auction auction = db.auctions.Where(a => a.id == id).FirstOrDefault(); auction.status = "COMPLETED"; auction.timeClosed = System.DateTime.Now; db.Entry(auction).State = EntityState.Modified; db.SaveChanges(); transaction.Commit(); if (auction.bids.Count != 0) { User sendTo = auction.bids.LastOrDefault().user; string subject = "Get ordered product"; string body = "You have won on the auction. Soon you will your ordered product will be sent to you. "; string mailTo = sendTo.mail; EmailClass.Email(mailTo, subject, body); var hubContext = GlobalHost.ConnectionManager.GetHubContext <HubProjekat>(); hubContext.Clients.All.updateWinner(auction.id, sendTo.name, sendTo.lastname); } } catch (Exception e) { transaction.Rollback(); log.Error($"Error-not sent mail to winner on {DateTime.Now}"); } } return(RedirectToAction("AllAuctions", "Home")); }
public ActionResult Completed(Guid?reference, string status) { using (var transaction = db.Database.BeginTransaction(IsolationLevel.Serializable)) { try { TokenOrder order = db.tokenOrders.Where(t => t.id == reference).FirstOrDefault(); if (order == null) { RedirectToAction("TokenOrders", "TokenOrder"); } if (order.status == "SUBMITTED") { if (status == "success") { order.status = "COMPLETED"; User user1 = Session["User"] as User; User user = db.users.Where(u => u.id == user1.id).FirstOrDefault(); user.numOfTokens += order.numOfTokens; var number = user.numOfTokens; var hubContext = GlobalHost.ConnectionManager.GetHubContext <HubProjekat>(); hubContext.Clients.All.userUp1(user.numOfTokens, user.id); db.Entry(user).State = EntityState.Modified; db.Entry(order).State = EntityState.Modified; db.SaveChanges(); EmailClass.Email(user.mail, "Token order", "Token order completed"); transaction.Commit(); } else { order.status = "CANCELED"; db.Entry(order).State = EntityState.Modified; db.SaveChanges(); ViewBag.Mesage = "The order is canceled"; transaction.Commit(); } } } catch (Exception e) { transaction.Rollback(); ViewBag.Message = "There is an error in transaction"; log.Error($"Error-not completed token order on {DateTime.Now}"); } } return(RedirectToAction("TokenOrders", "TokenOrder")); }
public void DefaultParameters() { AuctionDb db = new AuctionDb(); AdminSettings admin = db.adminSettings.FirstOrDefault(); admin.N = 10; admin.D = 18000; admin.S = 30; admin.G = 50; admin.P = 100; admin.C = "RSD"; admin.T = 100; db.Entry(admin).State = EntityState.Modified; db.SaveChanges(); }