コード例 #1
0
ファイル: HomeController.cs プロジェクト: BulatL/Youtube2
        public IActionResult Home()
        {
            var loggedInUserUsername = HttpContext.Session.GetString("LoggedInUserUsername");
            var loggedInUserRole     = HttpContext.Session.GetString("LoggedInUserRole");
            var loggedInUserId       = HttpContext.Session.GetString("LoggedInUserId");


            IEnumerable <Video> top5Videos    = new List <Video>();
            IEnumerable <Video> allVideos     = new List <Video>();
            List <VideoDTO>     top5VideosDTO = new List <VideoDTO>();
            List <VideoDTO>     allVideosDTO  = new List <VideoDTO>();

            if (loggedInUserRole == null)
            {
                top5Videos = _videoData.GetTop5("NoUser", 0);
                allVideos  = _videoData.GetAllNoUser();
            }
            else
            {
                if (loggedInUserRole.Equals("Admin"))
                {
                    top5Videos = _videoData.GetTop5("Admin", long.Parse(loggedInUserId));
                    allVideos  = _videoData.GetAllForAdmin();
                }
                else
                {
                    top5Videos = _videoData.GetTop5("User", long.Parse(loggedInUserId));
                    allVideos  = _videoData.GetAllForUser(long.Parse(loggedInUserId));
                }
            }
            foreach (var v in top5Videos)
            {
                v.Owner            = _userData.GetById(v.OwnerId);
                v.Owner.UserVideos = null;
                VideoDTO videoDTO = VideoDTO.ConvertVideoToDTO(v);
                top5VideosDTO.Add(videoDTO);
            }

            foreach (var v in allVideos)
            {
                v.Owner            = _userData.GetById(v.OwnerId);
                v.Owner.UserVideos = null;
                VideoDTO videoDTO = VideoDTO.ConvertVideoToDTO(v);
                allVideosDTO.Add(videoDTO);
            }

            HomeVideoViewModel homeVideoViewModel = new HomeVideoViewModel();

            homeVideoViewModel.AllVideos  = allVideosDTO;
            homeVideoViewModel.Top5Videos = top5VideosDTO;
            var contentType = Request.ContentType;

            if (contentType != null)
            {
                if (contentType.Equals("application/json"))
                {
                    return(Json(homeVideoViewModel));
                }
                else if (contentType.Equals("text/html"))
                {
                    return(View("~/Views/Shared/HomePage.cshtml", homeVideoViewModel));
                }
                return(StatusCode(415));
            }
            return(View("~/Views/Shared/HomePage.cshtml", homeVideoViewModel));
        }
コード例 #2
0
ファイル: SearchController.cs プロジェクト: BulatL/Youtube2
        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));
        }