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 <Response> CreateResponseAsync() { UserFavoritesDto favorites = await favoritesService.AddUserFavoritesAsync( request.ChannelId, request.ChatId, request.ContactId, clientConnection.UserId.Value).ConfigureAwait(false); return(new FavoritesResponse(request.RequestId, new List <UserFavoritesVm> { UserFavoritesConverter.GetUserFavoriteVm(favorites, clientConnection.UserId.GetValueOrDefault()) })); }
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)); } }
private async Task <UserFavoritesDto> AddChatToUserFavoritesAsync(long chatId, long userId) { using (MessengerDbContext context = contextFactory.Create()) { var chat = await context.Chats .AsNoTracking() .FirstOrDefaultAsync(chatOpt => chatOpt.Id == chatId && chatOpt.ChatUsers.Any(chatUser => chatUser.UserId == userId && !chatUser.Banned && !chatUser.Deleted && !chatOpt.Deleted)) .ConfigureAwait(false); UserFavorite lastUserFavorites = await context.UsersFavorites .AsNoTracking() .OrderByDescending(opt => opt.SerialNumber) .FirstOrDefaultAsync(opt => opt.UserId == userId) .ConfigureAwait(false); short newSerialNumber = lastUserFavorites != null ? (short)(lastUserFavorites.SerialNumber + 1) : (short)1; if (chat != null) { UserFavorite userFavorites = new UserFavorite { ChatId = chatId, UserId = userId, SerialNumber = newSerialNumber }; await context.UsersFavorites.AddAsync(userFavorites).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); return(UserFavoritesConverter.GetUserFavoriteDto(userFavorites)); } else { throw new ObjectDoesNotExistsException(); } } }
public async Task <Response> CreateResponseAsync() { List <UserFavoritesDto> usersFavorites = await favoritesService.GetUserFavoritesAsync(clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); return(new FavoritesResponse(request.RequestId, UserFavoritesConverter.GetUserFavoritesVms(usersFavorites, clientConnection.UserId.GetValueOrDefault()))); }