public async Task <Response> CreateResponseAsync() { var userFavorites = await favoritesService.ChangeUserFavoritesAsync( UserFavoritesConverter.GetUserFavoritesDtos(request.Favorites, clientConnection.UserId.Value), clientConnection.UserId.Value).ConfigureAwait(false); return(new FavoritesResponse(request.RequestId, UserFavoritesConverter.GetUserFavoritesVms(userFavorites, clientConnection.UserId.Value))); }
public async Task <List <UserFavoritesDto> > ChangeUserFavoritesAsync(List <UserFavoritesDto> newFavorites, long userId) { using (MessengerDbContext context = contextFactory.Create()) { using (var transaction = await context.Database.BeginTransactionAsync().ConfigureAwait(false)) { try { var userFavorites = await context.UsersFavorites .Where(opt => opt.UserId == userId) .ToListAsync() .ConfigureAwait(false); context.UsersFavorites.RemoveRange(userFavorites); var favorites = UserFavoritesConverter.GetUserFavorites(newFavorites); short counter = 1; foreach (var favorite in favorites) { favorite.SerialNumber = counter; counter++; } await context.UsersFavorites.AddRangeAsync(favorites).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); transaction.Commit(); return(UserFavoritesConverter.GetUserFavoritesDtos(favorites)); } catch (Exception ex) { throw new InternalErrorException("Error adding favorites.", ex); } } } }
public async Task <List <UserFavoritesDto> > GetUserFavoritesAsync(long userId) { using (MessengerDbContext context = contextFactory.Create()) { var userFavorites = await context.UsersFavorites .Include(opt => opt.Channel) .Include(opt => opt.Contact) .ThenInclude(opt => opt.ContactUser) .Include(opt => opt.Chat) .Where(opt => opt.UserId == userId) .OrderBy(opt => opt.SerialNumber) .ToListAsync() .ConfigureAwait(false); return(UserFavoritesConverter.GetUserFavoritesDtos(userFavorites)); } }