Ejemplo n.º 1
0
        public IActionResult GetVideoById(long id)
        {
            var   contentType = Request.ContentType;
            Video video       = new Video();

            if (contentType != null)
            {
                video = _videoData.GetById(id);
                if (video.Name == null)
                {
                    return(NotFound());
                }
            }
            else
            {
                var loggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole");
                if (loggedInUserRole == null)
                {
                    video = _videoData.GetById(id);
                    if (video == null)
                    {
                        return(NotFound());
                    }
                    if (video.Blocked == true || video.Visibility == Visibility.Private)
                    {
                        return(StatusCode(401));
                    }
                }
                else
                {
                    if (loggedInUserRole.Equals("0"))
                    {
                        video = _videoData.GetById(id);
                        if (video == null)
                        {
                            return(NotFound());
                        }
                    }
                    else
                    {
                        long loggedInUserId = long.Parse(HttpContext.Session.GetString("LoggedInUserId"));
                        video = _videoData.GetById(id);
                        if (video == null)
                        {
                            return(NotFound());
                        }
                        if (video.OwnerId != loggedInUserId && (video.Blocked == true || video.Visibility == Visibility.Private))
                        {
                            return(StatusCode(401));
                        }
                    }
                }
            }
            video.Owner    = _userData.GetById(video.OwnerId);
            video.Comments = _commentData.GetCommentsByVideo(video.Id);
            foreach (var comment in video.Comments)
            {
                comment.User = _userData.GetById(comment.UserId);
            }
            video.NumberOfViews++;
            video = _videoData.Update(video);
            SingleVideoDTO singleVideoDTO = SingleVideoDTO.ConvertVideoToDTO(video);

            if (contentType != null)
            {
                if (contentType.Equals("application/json"))
                {
                    return(Json(singleVideoDTO));
                }
                else if (contentType.Equals("text/html"))
                {
                    VideoDTO newDTO = VideoDTO.ConvertVideoToDTO(video);
                    return(View("VideoInfo", newDTO));
                }
                return(StatusCode(415));
            }
            return(View("VideoPage", singleVideoDTO));
        }