public async Task <IActionResult> AddTournament(AddTournamentDto newTournament) // Add Tournament { return(Ok(await _tournamentService.AddTournamants(newTournament))); }
int GetUserId() => int.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); // Authenticated user. public async Task <ServiceResponse <List <GetTournamentDto> > > AddTournamants(AddTournamentDto newTournament) // Add user to the database. { ServiceResponse <List <GetTournamentDto> > serviceResponse = new ServiceResponse <List <GetTournamentDto> >(); webApi.Models.Tournament tournament = _mapper.Map <webApi.Models.Tournament>(newTournament); //Map Tournament to the tournament type. tournament.user = await _context.Users.FirstOrDefaultAsync(u => u.Id == GetUserId()); // grab Id of the authenticated user await _context.Tournaments.AddAsync(tournament); await _context.SaveChangesAsync(); serviceResponse.Data = (_context.Tournaments.Where(c => c.user.Id == GetUserId()).Select(c => _mapper.Map <GetTournamentDto>(c))).ToList(); // get results related to authorized user. return(serviceResponse); }