Ejemplo n.º 1
0
        public async Task <FilterMessagesDTO> FilterMessages(FilterMessagesDTO filter)
        {
            var messagesQuery = messageRepository.GetEntitiesQuery().Where(w => w.IsDelete == false).AsQueryable();

            if (!string.IsNullOrEmpty(filter.Title))
            {
                messagesQuery = messagesQuery.Where(s => s.text.Contains(filter.Title));
            }

            if (filter.Categories != null && filter.Categories.Any())
            {
                messagesQuery = messagesQuery.SelectMany(s =>
                                                         s.ProductSelectedCategories.Where(f => filter.Categories.Contains(f.MessageCategoryId)).Select(t => t.Message));
            }

            var count = (int)Math.Ceiling(messagesQuery.Count() / (double)filter.TakeEntity);

            var pager = Pager.Build(count, filter.PageId, filter.TakeEntity);

            var messages = await messagesQuery.Paging(pager).Select(s => new MessagesDTO {
                text                   = s.text,
                Like                   = s.Like,
                IsImportant            = s.IsImportant,
                DisLike                = s.DisLike,
                AuthorAvatar           = s.User.Avatar,
                AuthorFirstName        = s.User.FirstName,
                AuthorLastName         = s.User.LastName,
                AuthorGender           = s.User.Gender,
                AuthorMembershipNumber = s.User.MembershipNumber
            }).ToListAsync();

            return(filter.SetMessages(messages).SetPaging(pager));
        }