Пример #1
0
        public async Task <Shared.PagingResult <GameModel> > PagingAsync(Shared.PagingByIdModel model)
        {
            Shared.PagingResult <GameModel> result = new Shared.PagingResult <GameModel>();
            var pagingResult = await Repository.PagingAsync(Mapper.Map <DataAccess.Models.PagingByIdModel>(model));

            if (pagingResult?.Items == null)
            {
                return(result);
            }
            result.CanLoadMore = pagingResult.CanLoadMore;
            result.Total       = pagingResult.Total;
            result.Items       = new List <GameModel>();
            List <Task> tasks = new List <Task>();

            foreach (Game item in pagingResult.Items)
            {
                GameModel game = Mapper.Map <GameModel>(item);
                tasks.Add(Task.WhenAll(
                              _userRepository.GetByIdAsync(game.CreatedBy).ContinueWith(r =>
                {
                    if (r.Result == null)
                    {
                        return;
                    }
                    game.User          = Mapper.Map <UserModel>(r.Result);
                    List <Hero> heroes = r.Result.Heroes;
                    if (heroes == null || heroes.All(e => e.Id != game.UserHeroId))
                    {
                        return;
                    }
                    game.UserHero = Mapper.Map <HeroModel>(r.Result.Heroes.First(e => e.Id == game.UserHeroId));
                }),
                              string.IsNullOrEmpty(game.OpponentId) ? Task.FromResult(true) : _userRepository.GetByIdAsync(game.OpponentId).ContinueWith(r =>
                {
                    if (r.Result == null)
                    {
                        return;
                    }
                    game.Opponent      = Mapper.Map <UserModel>(r.Result);
                    List <Hero> heroes = r.Result.Heroes;
                    if (heroes == null || heroes.All(e => e.Id != game.OpponentHeroId))
                    {
                        return;
                    }
                    game.OpponentHero = Mapper.Map <HeroModel>(r.Result.Heroes.First(e => e.Id == game.OpponentHeroId));
                })).ContinueWith(r => result.Items.Add(game)));
            }
            await Task.WhenAll(tasks);

            return(result);
        }
Пример #2
0
        public async Task <Shared.PagingResult <FriendModel> > PagingFriendsAsync(Shared.PagingByIdModel pagingByIdModel)
        {
            var paging = await Repository.PagingFriendsAsync(Mapper.Map <DataAccess.Models.PagingByIdModel>(pagingByIdModel));

            var result = new Shared.PagingResult <FriendModel>();

            if (paging?.Items == null)
            {
                return(result);
            }
            result.Items       = Mapper.Map <List <FriendModel> >(paging.Items);
            result.Total       = paging.Total;
            result.CanLoadMore = paging.CanLoadMore;
            return(result);
        }