Exemple #1
0
        // GET: Match/Create
        public ActionResult Create()
        {
            ViewBag.Players = _matchService.GetPlayers();
            var matchCreateModel = new MatchCreateModel()
            {
                MatchDate = DateTime.Now
            };

            return(View(matchCreateModel));
        }
        public int AddNewMatch(MatchCreateModel createModel)
        {
            try
            {
                var trans = _context.Database.BeginTransaction();

                if (!createModel.GameId.HasValue)
                {
                    return(-1);
                }

                var newMatch = new Match
                {
                    GameId      = createModel.GameId.Value,
                    StartTime   = DateTime.Now,
                    EntranceFee = createModel.EntranceFee
                };
                _context.Matches.Add(newMatch);
                _context.SaveChanges();
                foreach (var party in createModel.StartingPlayers)
                {
                    var partObj = new Participant
                    {
                        IsResolved = false,
                        MatchId    = newMatch.MatchId,
                        PlayerId   = party
                    };
                    _context.Participants.Add(partObj);
                }

                newMatch.NumberOfPlayers = createModel.StartingPlayers.Length;
                _context.SaveChanges();

                var userId = _httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
                var audit  = new MatchAudit
                {
                    AuditDate = DateTime.Now,
                    MatchId   = newMatch.MatchId,
                    UserId    = userId,
                    Type      = AuditType.CreateMatch
                };
                _context.MatchAudits.Add(audit);
                _context.SaveChanges();
                trans.Commit();
                trans.Dispose();
                return(newMatch.MatchId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Log.Error(e, "Error occured while saving a new match");
                return(-1);
            }
        }
        public IActionResult StartMatch([FromBody] MatchCreateModel model)
        {
            if (model == null)
            {
                return(BadRequest("Model is missing"));
            }
            if (model.GameId is null or 0)
            {
                return(BadRequest("Invalid Game"));
            }
            var matchId = _service.AddNewMatch(model);
            var match   = _service.GetMatch(matchId);

            return(Ok(match));
        }
Exemple #4
0
        public ActionResult Create(MatchCreateModel match)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var matchID = _matchService.CreateMatch(match.MatchDate, match.Winner, match.Loser, MatchWinner.Player1);
                    return(RedirectToAction("Details", new { Id = matchID }));
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                }
            }

            ViewBag.Players = _matchService.GetPlayers();
            return(View(match));
        }
Exemple #5
0
 public ActionResult <MatchDomainModel> Post([FromBody] MatchCreateModel model)
 {
     return(CommandAsync(async() => await _matchService.Save <MatchDomainModel>(model)));
 }