public async Task <CurrentUserViewModel> Handle(GetCurrentUserQuery request, CancellationToken cancellationToken) { // need to use current user's email address to look up these details since b2c's id is not being stored string currentUserEmail = _currentUserService.GetUserEmail(); try { CurrentUserViewModel user = await _context.Users .Include(u => u.UserAchievements).ThenInclude(ua => ua.Achievement) .Include(u => u.UserRewards).ThenInclude(ur => ur.Reward) .Where(u => u.Email == currentUserEmail) .ProjectTo <CurrentUserViewModel>(_mapper.ConfigurationProvider) .SingleOrDefaultAsync(cancellationToken); if (user == null) { throw new NotFoundException(nameof(User), currentUserEmail); } return(user); } catch (NotFoundException nfex) { // nothing to do here - just rethrow the exception from above _logger.LogError(nfex, "Unable to find current user with email {currentUserEmail}", currentUserEmail); throw; } catch (Exception ex) { _logger.LogError(ex, "An error occurred while trying to find current user with email {currentUserEmail}", currentUserEmail); throw new NotFoundException(nameof(User), currentUserEmail); } }
public async Task <CurrentUserViewModel> Handle(GetCurrentUserQuery request, CancellationToken cancellationToken) { // need to use current user's email address to look up these details since b2c's id is not being stored var currentUserEmail = _currentUserService.GetUserEmail(); var user = await _context.Users .Include(u => u.UserAchievements).ThenInclude(ua => ua.Achievement) .Include(u => u.UserRewards).ThenInclude(ur => ur.Reward) .Where(u => u.Email == currentUserEmail) .ProjectTo <CurrentUserViewModel>(_mapper.ConfigurationProvider) .SingleOrDefaultAsync(cancellationToken); if (user == null) { throw new NotFoundException(nameof(User), currentUserEmail); } return(user); }
public async Task <Unit> Handle(UpsertCurrentUserCommand request, CancellationToken cancellationToken) { var user = await _context .Users .Where(u => u.Email == _currentUserService.GetUserEmail()) .FirstOrDefaultAsync(cancellationToken); if (user == null) { user = new Domain.Entities.User(); await _context.Users.AddAsync(user, cancellationToken); } _mapper.Map(_currentUserService, user); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }