Ejemplo n.º 1
0
        public async Task <JsonResult> GetMessagesForUser(string type, int pageSize, int pageIndex, string cats, bool isReloadCategory)
        {
            var categories = cats.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            Message[]  messages;
            Category[] newCategories = null;
            int        total;

            if (!string.IsNullOrEmpty(type) && type.Equals("starred", StringComparison.OrdinalIgnoreCase))
            {
                var starred = new[] { ((int)MessageStatus.Starred).ToString() };
                messages = await _messageRepository.GetMessagesForUser(SiteUser.UserId, pageSize, pageIndex, "all", categories, starred);

                total = await _messageRepository.CountMessages(SiteUser.UserId, "all", categories, starred);

                if (isReloadCategory)
                {
                    newCategories = await _messageRepository.GetCategoriesByStatus(SiteUser.UserId, starred);
                }
            }
            else if (!string.IsNullOrEmpty(type) && type.Equals("trash", StringComparison.OrdinalIgnoreCase))
            {
                var trash = new[] { ((int)MessageStatus.InTrash).ToString() };
                messages = await _messageRepository.GetMessagesForUser(SiteUser.UserId, pageSize, pageIndex, "all", categories, trash);

                total = await _messageRepository.CountMessages(SiteUser.UserId, "all", categories, trash);

                if (isReloadCategory)
                {
                    newCategories = await _messageRepository.GetCategoriesByStatus(SiteUser.UserId, trash);
                }
            }
            else
            {
                messages = await _messageRepository.GetMessagesForUser(SiteUser.UserId, pageSize, pageIndex, "all", categories,
                                                                       new[] { ((int)MessageStatus.Default).ToString(), ((int)MessageStatus.Starred).ToString(), ((int)MessageStatus.Archived).ToString() });

                total = await _messageRepository.CountMessages(SiteUser.UserId, "all", categories,
                                                               new[] { ((int)MessageStatus.Default).ToString(), ((int)MessageStatus.Starred).ToString(), ((int)MessageStatus.Archived).ToString() });

                if (isReloadCategory)
                {
                    //var allCategories = await _messageRepository.GetAllCategories(SiteUser.UserId);
                    var allCategories = await _messageRepository.GetCategoriesForFiltering(SiteUser.UserId);

                    newCategories = allCategories?.Select(c => new Category
                    {
                        Categoryid   = c.EnvTypesId,
                        CategoryName = c.EnvTypeDescription
                    }).ToArray();
                }
            }

            if (messages == null)
            {
                return(Json(new { }));
            }

            var jsmessages = messages.Select(msg => new
            {
                id           = msg.MessageId,
                header       = msg.HeadLine,
                content      = msg.MsgText,
                categoryId   = msg.MessageArea,
                categoryName = msg.AreaDescription,
                isUnRead     = !msg.MessageRead,
                isStarred    = msg.Status == (int)MessageStatus.Starred,
                isTrash      = msg.Status == (int)MessageStatus.InTrash,
                isDelete     = msg.Status == (int)MessageStatus.Deleted,
                receivedDate = $"{msg.SendDate:yyyy-MM-dd}"
            });

            var jsCategories = newCategories?
                               .OrderBy(cat => cat.CategoryName)
                               .Select(cat => new
            {
                id         = cat.Categoryid,
                name       = cat.CategoryName,
                isSelected = false
            });


            return(Json(new
            {
                messages = jsmessages,
                categories = jsCategories,
                total = total
            }));
        }