public async Task <IActionResult> PostPronostic([FromBody] Pronostic pronostic)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var identity = HttpContext.User.Identity as ClaimsIdentity;

            if (!CheckClaims.CheckUser(identity, pronostic.User.Id))
            {
                return(BadRequest("It's not you :)"));
            }


            var isAlreadyBet = _context.Pronostic.Any(p => p.Match.Id == pronostic.Match.Id && p.User.Id == pronostic.User.Id);

            if (isAlreadyBet)
            {
                return(BadRequest("A bet already exists for this match/user"));
            }
            if (pronostic.Match.Date <= DateTime.Now)
            {
                return(BadRequest("Cannot bet on already played match"));
            }
            _context.Entry(pronostic.User).State  = EntityState.Unchanged;
            _context.Entry(pronostic.Match).State = EntityState.Unchanged;
            _context.Pronostic.Add(pronostic);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetPronostic", new { id = pronostic.Id }, pronostic));
        }
Exemple #2
0
 private bool IsPronosticRight(Pronostic pronostic, Match match)
 {
     if (StateTeam1(pronostic) == StateTeam1(match))
     {
         return(true);
     }
     return(false);
 }
        public Pronostic Create(Pronostic pronostic)
        {
            if (context.Pronostic.Any(x => x.IdMatch == pronostic.IdMatch && x.IdUser == pronostic.IdUser))
            {
                throw new AppException("Pronostic already created on match id " + pronostic.IdMatch + " for user id " + pronostic.IdUser);
            }

            context.Pronostic.Add(pronostic);
            context.SaveChanges();

            return(pronostic);
        }
        public async Task <IActionResult> PutPronostic([FromRoute] long id, [FromBody] Pronostic pronostic)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != pronostic.Id)
            {
                return(BadRequest());
            }

            var identity = HttpContext.User.Identity as ClaimsIdentity;

            if (!CheckClaims.CheckUser(identity, pronostic.User.Id))
            {
                return(BadRequest("It's not you :)"));
            }


            if (pronostic.Match.Date <= DateTime.Now)
            {
                return(BadRequest("Cannot bet on already played match"));
            }

            _context.Entry(pronostic).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PronosticExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #5
0
 private MatchStatus StateTeam1(Pronostic pronostic)
 {
     if (pronostic.ScoreTeam1 > pronostic.ScoreTeam2)
     {
         return(MatchStatus.Win);
     }
     else
     {
         if (pronostic.ScoreTeam1 == pronostic.ScoreTeam2)
         {
             return(MatchStatus.Equal);
         }
         else
         {
             return(MatchStatus.Loose);
         }
     }
 }
        public void Update(Pronostic pronosticParam)
        {
            var pronostic = context.Pronostic.Find(pronosticParam.Id);

            if (pronostic == null)
            {
                throw new AppException("Pronostic not found");
            }

            // Check is match has alredy start to avoid update of a pronostic
            var match = context.Match.Find(pronostic.IdMatch);

            if (match.Date <= DateTime.Now)
            {
                throw new AppException("Match has already started, you can't change pronostic");
            }

            pronostic.ScoreTeam1 = pronosticParam.ScoreTeam1;
            pronostic.ScoreTeam2 = pronosticParam.ScoreTeam2;

            context.Pronostic.Update(pronostic);
            context.SaveChanges();
        }
Exemple #7
0
        public void PostTips(PostTipsBindingModel ptbm, string userId)
        {
            ApplicationUser user       = this.data.Users.Find(x => x.Id == userId).First();
            Guid            g          = Guid.NewGuid();
            string          GuidString = Convert.ToBase64String(g.ToByteArray());

            GuidString = GuidString.Replace("=", "");
            Pronostic pronostic = new Pronostic()
            {
                Id             = g.ToString(),
                User           = user,
                Chance         = ptbm.Chance,
                AwayTeam       = ptbm.SelectAwayTeam,
                LocalTeam      = ptbm.SelectHomeTeam,
                Koeficent      = ptbm.Koeficent,
                StartMatch     = ptbm.StartMatch,
                TimeElpased    = DateTime.Now,
                TypeOfPrognise = ptbm.SelectTips
            };

            data.Pronostics.InsertOrUpdate(pronostic);
            data.SaveChanges();
        }