Example #1
0
        public IActionResult SearchAll(string searchText)
        {
            var loggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole");

            if (searchText == null)
            {
                searchText = "";
            }
            IEnumerable <Video>   videos   = new List <Video>();
            IEnumerable <Comment> comments = new List <Comment>();
            IEnumerable <User>    users    = new List <User>();

            users    = _userData.Search(searchText, null, "desc");
            comments = _commentData.Search(searchText, "desc");
            if (loggedInUserRole == null)
            {
                videos = _videoData.GetAllNoUser().Where(v => v.Name.Contains(searchText) ||
                                                         v.Description.Contains(searchText));
            }
            else
            {
                if (loggedInUserRole.Equals("0"))
                {
                    videos = _videoData.GetAllForAdmin().Where(v => v.Name.Contains(searchText) ||
                                                               v.Description.Contains(searchText));
                }
                else
                {
                    long loggedInUserId = long.Parse(HttpContext.Session.GetString("LoggedInUserId"));
                    videos = _videoData.GetAllForUser(loggedInUserId).
                             Where(v => v.Name.Contains(searchText) ||
                                   v.Description.Contains(searchText));
                }
            }
            if (videos.Count() > 0)
            {
                foreach (var v in videos)
                {
                    v.Owner = _userData.GetById(v.OwnerId);
                }
            }
            if (comments.Count() > 0)
            {
                foreach (var c in comments)
                {
                    c.User  = _userData.GetById(c.UserId);
                    c.Video = _videoData.GetById(c.VideoId);
                }
            }

            SearchViewModel searchViewModel = new SearchViewModel();

            searchViewModel.Users    = UserForAdminPage.ConvertDTO(users);
            searchViewModel.Comments = CommentDTO.ConvertCommenstToDTO(comments);
            searchViewModel.Videos   = VideoDTO.ConvertVideosToDTO(videos);
            var contentType = Request.ContentType;

            if (contentType != null)
            {
                if (contentType.Equals("application/json"))
                {
                    return(Json(searchViewModel));
                }
            }
            return(View("SearchPage", searchViewModel));
        }