public ActionResult CreateBet(decimal amount, int?couponEventId) { using (var db = new RosBetsContext()) { if (!User.Identity.IsAuthenticated) { return(Json(new { result = "NotLoggedIn" })); } var coupon = Coupon.GetCoupon(HttpContext); var user = db.Users.FirstOrDefault(x => x.Mail == User.Identity.Name); if (user.Money < amount) { return(Json(new { result = "NoMoney" })); } var bet = new Bet { UserId = user.Id, BetAmount = amount, Date = DateTime.Now }; db.Bets.Add(bet); user.Money -= amount; db.SaveChanges(); coupon.CreateBet(bet, couponEventId); var viewmodel = new CouponViewModel { CouponEvents = coupon.GetCouponEvents(), TotalCoefficient = coupon.GetCoefficient() }; return(PartialView("right_column", viewmodel)); } }
public void AddToCoupon(MatchEvent matchEvent) { using (var db = new RosBetsContext()) { var couponEvent = db.CouponEvents .SingleOrDefault(c => c.Coupon == CouponId && c.MatchId == matchEvent.MatchId && c.EventId == matchEvent.EventId); float?totalValue = null; switch (matchEvent.EventId) { case 2: case 3: totalValue = db.MatchEvents .SingleOrDefault(x => x.MatchId == matchEvent.MatchId && x.EventId == 1).EventValue; break; case 11: case 12: totalValue = db.MatchEvents .SingleOrDefault(x => x.MatchId == matchEvent.MatchId && x.EventId == 10).EventValue; break; case 14: case 15: totalValue = db.MatchEvents .SingleOrDefault(x => x.MatchId == matchEvent.MatchId && x.EventId == 13).EventValue; break; } if (couponEvent == null) { var sameMatchEvents = db.CouponEvents.Where(x => x.MatchId == matchEvent.MatchId); if (sameMatchEvents.Any()) { db.CouponEvents.RemoveRange(sameMatchEvents); } couponEvent = new CouponEvent { EventId = matchEvent.EventId, MatchId = matchEvent.MatchId, Coefficient = matchEvent.EventValue, Coupon = CouponId, Total = totalValue }; db.CouponEvents.Add(couponEvent); } else { db.CouponEvents.Remove(couponEvent); } db.SaveChanges(); } }
public List <CouponEvent> GetCouponEvents() { using (var db = new RosBetsContext()) { return(db.CouponEvents .Where(c => c.Coupon == CouponId) .Include(c => c.Match) .Include(c => c.Match.Championship) .Include(c => c.Event) .ToList()); } }
public void RemoveFromCoupon(int id) { using (var db = new RosBetsContext()) { var couponEvent = db.CouponEvents.Single(c => c.Coupon == CouponId && c.Id == id); if (couponEvent != null) { db.CouponEvents.Remove(couponEvent); } db.SaveChanges(); } }
public void AssignUsername(string userName) { using (var db = new RosBetsContext()) { var couponEvents = db.CouponEvents.Where(c => c.Coupon == CouponId); foreach (var couponEvent in couponEvents) { couponEvent.Coupon = userName; } db.SaveChanges(); } }
public void ClearCoupon() { using (var db = new RosBetsContext()) { var couponEvents = db.CouponEvents.Where(c => c.Coupon == CouponId); foreach (var couponEvent in couponEvents) { db.CouponEvents.Remove(couponEvent); } db.SaveChanges(); } }
public float?GetCoefficient() { float?totalCoefficient; using (var db = new RosBetsContext()) { var coefficients = db.CouponEvents .Where(c => c.Coupon == CouponId) .Select(c => c.Coefficient).ToArray(); totalCoefficient = coefficients.Aggregate(1f, (a, b) => a * b); } return(totalCoefficient); }
protected void Application_Start() { using (var context = new RosBetsContext()) SqlConnectionString = context.Database.Connection.ConnectionString; AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); if (!String.IsNullOrEmpty(SqlConnectionString)) { SqlDependency.Start(SqlConnectionString); } }
public ActionResult AllOrdinary(params BetTO[] couponEvents) { if (!User.Identity.IsAuthenticated) { return(Json(new { result = "NotLoggedIn" })); } var user = new User(); using (var db = new RosBetsContext()) { user = db.Users.FirstOrDefault(x => x.Mail == User.Identity.Name); } var betAmount = couponEvents.Sum(x => x.Value); if (betAmount > user.Money) { return(Json(new { result = "NoMoney" })); } var coupon = Coupon.GetCoupon(HttpContext); foreach (var e in couponEvents) { var bet = new Bet { UserId = user.Id, BetAmount = e.Value, Date = DateTime.Now }; using (var db = new RosBetsContext()) { db.Bets.Add(bet); user.Money -= e.Value; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); coupon.CreateBet(bet, e.EventId); } } var viewmodel = new CouponViewModel { CouponEvents = coupon.GetCouponEvents(), TotalCoefficient = coupon.GetCoefficient() }; return(PartialView("right_column", viewmodel)); }
public void CreateBet(Bet bet, int?couponEventId) { using (var db = new RosBetsContext()) { float totalCoefficient = 1f; List <CouponEvent> couponEvents = new List <CouponEvent>(); if (couponEventId != null) { couponEvents.Add(db.CouponEvents.Where(x => x.Id == couponEventId).FirstOrDefault()); } else { couponEvents = GetCouponEvents(); } foreach (var couponEvent in couponEvents) { var betEvent = new BetEvent { BetId = bet.Id, EventId = (int)couponEvent.EventId, MatchId = (int)couponEvent.MatchId, Coefficient = couponEvent.Coefficient, BetEventStatusId = 4, Total = couponEvent.Total }; totalCoefficient *= couponEvent.Coefficient; db.BetEvents.Add(betEvent); if (!db.CouponEvents.Local.Contains(couponEvent)) { db.CouponEvents.Attach(couponEvent); } db.CouponEvents.Remove(couponEvent); db.SaveChanges(); } bet.TotalCoefficient = totalCoefficient; bet.Payout = bet.BetAmount * (decimal)totalCoefficient; db.Entry(bet).State = EntityState.Modified; db.SaveChanges(); } }
public ActionResult AddToCoupon(string id) { using (var db = new RosBetsContext()) { var ids = id.Split('_'); var eventId = int.Parse(ids[0]); var matchId = int.Parse(ids[1]); var matchEvent = db.MatchEvents.SingleOrDefault(m => m.EventId == eventId && m.MatchId == matchId); var coupon = Coupon.GetCoupon(HttpContext); if (matchEvent != null) { coupon.AddToCoupon(matchEvent); } var viewmodel = new CouponViewModel { CouponEvents = coupon.GetCouponEvents(), TotalCoefficient = coupon.GetCoefficient() }; return(PartialView("right_column", viewmodel)); } }