public async Task <IActionResult> Create(MatchViewModel matchViewModel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "Data is not valid");
                matchViewModel.ClubsItems    = clubService.GetAllAsKeyValuePairs();
                matchViewModel.SeasonsItems  = seasonService.GetAllAsKeyValuePairs();
                matchViewModel.StadiumsItems = stadiumService.GetAllAsKeyValuePairs();
                matchViewModel.RefereesItems = refereeService.GetAllAsKeyValuePairs();

                return(View(matchViewModel));
            }
            try
            {
                await matchService.CreateAsync(matchViewModel);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.InnerException?.Message ?? ex.Message);
                matchViewModel.ClubsItems    = clubService.GetAllAsKeyValuePairs();
                matchViewModel.SeasonsItems  = seasonService.GetAllAsKeyValuePairs();
                matchViewModel.StadiumsItems = stadiumService.GetAllAsKeyValuePairs();
                matchViewModel.RefereesItems = refereeService.GetAllAsKeyValuePairs();

                return(View(matchViewModel));
            }

            TempData["SuccessMessage"] = "Match added successfully.";

            return(RedirectToAction("Index"));
        }
Example #2
0
        public async Task <IActionResult> CreateMatch([FromBody] MatchCreateDto matchCreateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var matchToCreate = _mapper.Map <Match>(matchCreateDto);
            await _matchService.CreateAsync(matchToCreate);

            var match = await _matchService.GetDetailByIdAsync(matchToCreate.Id);

            var returnMatch = _mapper.Map <MatchDetailDto>(match);

            return(Ok(returnMatch));
        }