Exemplo n.º 1
0
        public IActionResult GetById(long id)
        {
            var loggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole");

            var  contentType = Request.ContentType;
            User user        = _userData.GetById(id);

            if (user == null)
            {
                return(NotFound());
            }
            if (contentType == null)
            {
                if (loggedInUserRole != null)
                {
                    if (loggedInUserRole.Equals("1"))
                    {
                        long loggedInUserId = long.Parse(HttpContext.Session.GetString("LoggedInUserId"));
                        if (user.Blocked == true && user.Id != loggedInUserId)
                        {
                            return(StatusCode(401));
                        }
                    }
                }
                else
                {
                    if (user.Blocked == true)
                    {
                        return(StatusCode(401));
                    }
                }
            }

            user.Followers    = _followData.GetFollowers(user.Id);
            user.Following    = _followData.GetFollowings(user.Id);
            user.UserVideos   = _videoData.GetByOwnerId(user.Id);
            user.LikedVideos  = _videoData.GetLikedVideos(user);
            user.UserComments = _commentData.GetCommentsByUser(user.Id);
            foreach (var u in user.LikedVideos)
            {
                u.Owner = _userData.GetById(u.OwnerId);
            }

            SingleUserDTO singleUserDTO = SingleUserDTO.ConvertUserToDTO(user);

            if (contentType != null)
            {
                if (contentType.Equals("application/json"))
                {
                    return(Json(singleUserDTO));
                }
                else if (contentType.Equals("text/html"))
                {
                    SingleUserDTO newDTO = SingleUserDTO.ConvertUserToDTO(user);
                    return(View("SingleUserInfo", newDTO));
                }
                return(StatusCode(415));
            }
            return(View("UserPage", singleUserDTO));
        }
Exemplo n.º 2
0
        public IActionResult Promote(int id)
        {
            var  contentType = Request.ContentType;
            User user        = _userData.GetById(id);

            if (user == null)
            {
                return(NotFound());
            }
            if (user.Role == RoleType.Admin)
            {
                return(BadRequest());
            }
            user.Role = RoleType.Admin;
            user      = _userData.Update(user);
            if (contentType != null)
            {
                if (contentType.Equals("application/json"))
                {
                    UserForVideoComment userDto = UserForVideoComment.ConvertUserForVideoComment(user);
                    return(Json(userDto));
                }
                else if (contentType.Equals("text/html"))
                {
                    SingleUserDTO newDTO = SingleUserDTO.ConvertUserToDTO(user);
                    return(View("SingleUserInfo", newDTO));
                }
                else if (contentType.Equals("application/x-www-form-urlencoded"))
                {
                    return(RedirectToAction(nameof(GetById), id));
                }
                return(StatusCode(415));
            }
            return(Json("Success"));
        }
Exemplo n.º 3
0
        public IActionResult EditUser(AddUserDTO user, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                if (_userData.ExistUsername(user.Username, user.Id))
                {
                    ViewBag.Message = "Username already exist";
                    return(Json("Username aleady exist"));
                }

                var editUser = _userData.GetById(user.Id);
                if (editUser == null)
                {
                    return(NotFound());
                }
                editUser.FirstName = user.FirstName;
                editUser.LastName  = user.LastName;
                if (user.Password != null)
                {
                    editUser.Password = user.Password;
                }
                editUser.Email       = user.Email;
                editUser.Description = user.Description;
                if (user.Image != null)
                {
                    editUser.ProfilePictureUrl = AddImage(editUser.Username, user.Image);
                }
                editUser = _userData.Update(editUser);

                var contentType = Request.ContentType;
                if (contentType != null)
                {
                    if (contentType.Equals("application/json"))
                    {
                        UserForVideoComment userDto = UserForVideoComment.ConvertUserForVideoComment(editUser);
                        return(Json(userDto));
                    }
                    else if (contentType.Equals("text/html"))
                    {
                        SingleUserDTO newDTO = SingleUserDTO.ConvertUserToDTO(editUser);
                        return(View("SingleUserInfo", newDTO));
                    }
                    else if (contentType.Equals("application/x-www-form-urlencoded"))
                    {
                        return(RedirectToAction(nameof(GetById), user.Id));
                    }
                    return(RedirectToAction(nameof(GetById), user.Id));;
                }
                return(Json("Success"));
            }
            else
            {
                return(BadRequest());
            }
        }
Exemplo n.º 4
0
        public IActionResult GetLoggedInUser()
        {
            var loggedInUserId = HttpContext.Session.GetString("LoggedInUserId");

            var contentType = Request.ContentType;

            if (loggedInUserId == null)
            {
                if (contentType != null)
                {
                    if (contentType.Equals("application/json"))
                    {
                        return(NoContent());
                    }
                }
                return(Json(null));
            }
            User user = _userData.GetById(long.Parse(loggedInUserId));

            if (user == null)
            {
                if (contentType != null)
                {
                    if (contentType.Equals("application/json"))
                    {
                        return(NoContent());
                    }
                }
                return(Json(null));
            }
            if (contentType != null)
            {
                if (contentType.Equals("application/json"))
                {
                    UserForVideoComment userDto = UserForVideoComment.ConvertUserForVideoComment(user);
                    return(Json(userDto));
                }
                else if (contentType.Equals("text/html"))
                {
                    SingleUserDTO newDTO = SingleUserDTO.ConvertUserToDTO(user);
                    return(View("SingleUserInfo", newDTO));
                }
                return(StatusCode(415));
            }
            return(Json(user));
        }
Exemplo n.º 5
0
        public IActionResult AddVideo(AddVideoDTO videoDTO, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                var loggedInUserId = HttpContext.Session.GetString("LoggedInUserId");

                if (loggedInUserId == null)
                {
                    return(StatusCode(401));
                }
                User user = _userData.GetById(long.Parse(loggedInUserId));
                if (user == null)
                {
                    return(StatusCode(401));
                }
                if (user.Blocked == true)
                {
                    return(StatusCode(401));
                }

                Video newVideo = new Video();
                newVideo.AllowComments    = videoDTO.AllowComments;
                newVideo.AllowRaiting     = videoDTO.AllowRaiting;
                newVideo.Blocked          = false;
                newVideo.Deleted          = false;
                newVideo.Description      = videoDTO.Description;
                newVideo.CreationDate     = DateTime.Today;
                newVideo.Name             = videoDTO.Name;
                newVideo.NumberOfDislikes = 0;
                newVideo.NumberOfLikes    = 0;
                newVideo.NumberOfViews    = 0;
                newVideo.OwnerId          = user.Id;
                newVideo.VideoUrl         = videoDTO.VideoUrl;
                newVideo.Visibility       = videoDTO.Visibility;

                if (image != null)
                {
                    newVideo.PhotoUrl = AddImage(newVideo.Name, user.Username, image);
                }
                else
                {
                    newVideo.PhotoUrl = "defaultVideoImage.jpg";
                }


                newVideo = _videoData.Create(newVideo);
                VideoDTO video       = VideoDTO.ConvertVideoToDTO(newVideo);
                var      contentType = Request.ContentType;

                user.Followers   = _followData.GetFollowers(user.Id);
                user.Following   = _followData.GetFollowings(user.Id);
                user.UserVideos  = _videoData.GetByOwnerId(user.Id);
                user.LikedVideos = _videoData.GetLikedVideos(user);
                foreach (var u in user.LikedVideos)
                {
                    u.Owner = _userData.GetById(u.OwnerId);
                }

                SingleUserDTO singleUserDTO = SingleUserDTO.ConvertUserToDTO(user);

                if (contentType != null)
                {
                    if (contentType.Equals("application/json"))
                    {
                        return(Json(video));
                    }
                    else if (contentType.Equals("text/html"))
                    {
                        return(View("UserPage", singleUserDTO));
                    }
                    return(View("UserPage", singleUserDTO));
                }
                return(Json(video));
            }
            else
            {
                return(Json(new VideoDTO()));
            }
        }