public async Task <IActionResult> PutPostVideo(Guid id, PostVideo postVideo)
        {
            if (id != postVideo.Id)
            {
                return(BadRequest());
            }

            _context.Entry(postVideo).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostVideoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> Edit(Guid id, [Bind("Id,PostId,VideoId")] PostVideo postVideo)
        {
            if (id != postVideo.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(postVideo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostVideoExists(postVideo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PostId"]  = new SelectList(_context.Post, "Id", "Id", postVideo.PostId);
            ViewData["VideoId"] = new SelectList(_context.Videos, "Id", "Id", postVideo.VideoId);
            return(View(postVideo));
        }
        public async Task <IHttpActionResult> PutPostVideo(Guid id, PostVideo postVideo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != postVideo.id)
            {
                return(BadRequest());
            }

            db.Entry(postVideo).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostVideoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> PostPostVideo(PostVideo postVideo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.PostVideos.Add(postVideo);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (PostVideoExists(postVideo.id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = postVideo.id }, postVideo));
        }
예제 #5
0
        public async Task <PostVideo> AddPostVideos(IFormFile file, int postId)
        {
            var post = await postRepository.Get(postId);

            if (file == null)
            {
                return(null);
            }

            if (file.Length == 0)
            {
                return(null);
            }

            var videoPath = Path.Combine(webHostEnvironment.WebRootPath, "videos", file.FileName);

            var songReader = file.OpenReadStream();

            using (var fileStream = new FileStream(videoPath, FileMode.Create))
            {
                songReader.CopyTo(fileStream);

                var postVideo = new PostVideo {
                    VideoSource = fileStream.Name, Post = post
                };

                await ctx.PostVideos.AddAsync(postVideo);

                return(postVideo);
            }
        }
예제 #6
0
        public void AddVideoToPost(string postid, string videoUrl)
        {
            List <int> numlist  = new List <int>();
            int        num      = 1;
            string     siteName = null;
            string     thumbUrl = null;
            var        check    = _context.PostVideos.Where(x => x.PostId == postid && x.VideoUrl == videoUrl).Any();

            if (!check)
            {
                while (_context.PostVideos.Where(x => x.Id == num).Any())
                {
                    num++;
                }
                if (videoUrl.Contains("youtube.com") || videoUrl.Contains("youtu.be"))
                {
                    int pos    = videoUrl.LastIndexOf("/") + 1;
                    var result = videoUrl.Substring(pos, videoUrl.Length - pos);
                    thumbUrl = "https://img.youtube.com/vi/" + result + "/0.jpg";
                    siteName = "YouTube";
                }
                var video = new PostVideo {
                    Id = num, PostId = postid, VideoUrl = videoUrl, VideoSiteName = siteName, VideoThumbnail = thumbUrl
                };
                _context.PostVideos.Add(video);
                Save();
            }
        }
        public async Task <IHttpActionResult> GetPostVideo(Guid id)
        {
            PostVideo postVideo = await db.PostVideos.FindAsync(id);

            if (postVideo == null)
            {
                return(NotFound());
            }

            return(Ok(postVideo));
        }
예제 #8
0
        public async Task <ActionResult> Create(PostViewModel model, HttpPostedFileBase file)
        {
            if (model != null && ModelState.IsValid)
            {
                var post = new Post
                {
                    Body        = model.Body,
                    Title       = model.Title,
                    Description = model.Description,
                    IsPublic    = true,
                    PostedOn    = DateTime.Now,
                    UserId      = this.User.Identity.GetUserId(),
                };

                db.Posts.Add(post);
                await db.SaveChangesAsync();

                Success("Successfully create post.", true);
                if (model.VideoUrl != null)
                {
                    string s = YouTubeUrlHandler.GetVideoId(model.VideoUrl);
                    if (s != "Error")
                    {
                        var postVideo = new PostVideo
                        {
                            PostId   = post.PostId,
                            VideoUrl = s,
                        };
                        db.Entry(postVideo).State = EntityState.Added;
                        await db.SaveChangesAsync();

                        Success("Video to post was successfully added.", true);
                    }
                }
                if (file?.FileName != null)
                {
                    var postImage = new PostImage
                    {
                        ImageUrl = UploadPhoto(file),
                        PostId   = post.PostId
                    };
                    db.Entry(postImage).State = EntityState.Added;
                    await db.SaveChangesAsync();

                    Success("Successfully upload picture.", true);
                }
            }

            ViewBag.UserId = new SelectList(db.Users, "Id", "FullName");
            return(RedirectToAction("Index", "Home"));
        }
예제 #9
0
        public async Task <IActionResult> Create([Bind("Id,PostId,VideoId")] PostVideo postVideo)
        {
            if (ModelState.IsValid)
            {
                postVideo.Id = Guid.NewGuid();
                _context.Add(postVideo);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PostId"]  = new SelectList(_context.Post, "Id", "Id", postVideo.PostId);
            ViewData["VideoId"] = new SelectList(_context.Videos, "Id", "Id", postVideo.VideoId);
            return(View(postVideo));
        }
        public async Task <IHttpActionResult> DeletePostVideo(Guid id)
        {
            PostVideo postVideo = await db.PostVideos.FindAsync(id);

            if (postVideo == null)
            {
                return(NotFound());
            }

            db.PostVideos.Remove(postVideo);
            await db.SaveChangesAsync();

            return(Ok(postVideo));
        }
예제 #11
0
 public ResultModel Create(PostVideo createVideoForPost)
 {
     try
     {
         _postVideoRepository.Create(createVideoForPost);
         _unitOfWork.Commit();
         return(new ResultModel {
             Status = true, Message = "Create Process Success ! "
         });
     }
     catch (Exception ex)
     {
         return(new ResultModel {
             Status = false, Message = ex.ToString()
         });
     }
 }
예제 #12
0
        public PostDTO AddPost(IFormFile file, Post post)
        {
            CompletePostDTO completePost = new CompletePostDTO()
            {
                FileInPost = file,
                Post       = post
            };

            IPostHandler textPost  = new PostText(postRepository, unitOfWork);
            IPostHandler imagePost = new PostImage(postRepository, unitOfWork, cloudinary);
            IPostHandler videoPost = new PostVideo(postRepository, unitOfWork, cloudinary);

            textPost.SetNextPostType(imagePost);
            imagePost.SetNextPostType(videoPost);

            textPost.EvaluatePost(completePost);

            return(mapper.Map <PostDTO>(post));
        }
        public async Task <ActionResult <PostVideo> > PostPostVideo(PostVideo postVideo)
        {
            _context.PostVideo.Add(postVideo);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (PostVideoExists(postVideo.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetPostVideo", new { id = postVideo.Id }, postVideo));
        }
예제 #14
0
        public JsonResult CreatePost([FromForm] PostCreateApi model)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    if (string.IsNullOrEmpty(model.Text))
                    {
                        Result.Status  = false;
                        Result.Message = "You can not add a post without writing text ! ";
                        return(BadResponse(Result));
                    }
                    var appUser = _userService.FindByUserName(User.Identity.Name);
                    if (appUser == null)
                    {
                        return(BadResponse(new ResultModel
                        {
                            Status = false,
                            Message = "User not found !"
                        }));
                    }

                    bool hasImage          = CheckItemType.HasItemImage(model);
                    bool hasVideo          = CheckItemType.HasItemVideo(model);
                    var  imageUploadResult = new ImageUploadResult();
                    var  videoUploadResult = new VideoUploadResult();

                    #region CloudinaryProcess

                    #region ImageUploadingProcess
                    if (hasImage)
                    {
                        using (var stream = model.Photo.OpenReadStream())
                        {
                            var uploadParams = new ImageUploadParams
                            {
                                File = new FileDescription(model.Photo.Name, stream)
                            };

                            imageUploadResult = _cloudinary.Upload(uploadParams);
                            if (imageUploadResult.Error != null)
                            {
                                scope.Dispose();
                                return(BadResponse(ResultModel.Error("The upload process can not be done !")));
                            }
                        }
                    }
                    #endregion

                    #region VideoUploadingProcess

                    if (hasVideo)
                    {
                        using (var stream = model.Video.OpenReadStream())
                        {
                            var uploadParams = new VideoUploadParams
                            {
                                File = new FileDescription(model.Video.Name, stream)
                            };

                            videoUploadResult = _cloudinary.Upload(uploadParams);
                            if (videoUploadResult.Error != null)
                            {
                                scope.Dispose();
                                return(BadResponse(ResultModel.Error("The upload process can not be done !")));
                            }
                        }
                    }

                    #endregion

                    #endregion

                    #region CRUD

                    var newPost = new Post
                    {
                        Text     = model.Text,
                        PostType = hasImage == true ? (int)PostTypeEnum.PostImage
                        : hasVideo == true ? (int)PostTypeEnum.PostVideo
                        : (int)PostTypeEnum.PostText,
                        CreatedBy = appUser.Id
                    };
                    ResultModel postModel = _postService.Create(newPost);

                    if (!postModel.Status)
                    {
                        scope.Dispose();
                        return(BadResponse(ResultModel.Error("The upload process can not be done !")));
                    }

                    #region PostImages
                    if (imageUploadResult != null && imageUploadResult.StatusCode == HttpStatusCode.OK)
                    {
                        var postImages = new PostImage
                        {
                            PostId   = newPost.Id,
                            ImageUrl = imageUploadResult.Url.ToString()
                        };
                        ResultModel postImageModel = _postImageService.Create(postImages);

                        if (!postImageModel.Status)
                        {
                            scope.Dispose();
                            return(BadResponse(ResultModel.Error("The upload process can not be done !")));
                        }
                    }

                    #endregion

                    #region PostVideos

                    if (videoUploadResult != null && videoUploadResult.StatusCode == HttpStatusCode.OK)
                    {
                        var postVideos = new PostVideo
                        {
                            PostId   = newPost.Id,
                            VideoUrl = videoUploadResult.Url.ToString()
                        };
                        ResultModel postVideoModel = _postVideoService.Create(postVideos);

                        if (!postVideoModel.Status)
                        {
                            scope.Dispose();
                            return(BadResponse(ResultModel.Error("The upload process can not be done !")));
                        }
                    }

                    #endregion

                    #endregion

                    scope.Complete();
                    //TODO
                    //There must be an integration that returns the last post that has just been createad.

                    return(OkResponse(new PostListDto
                    {
                        Id = newPost.Id,
                        Text = newPost.Text,
                        ImageUrl = imageUploadResult.Url?.ToString(),
                        CreatedByUserName = appUser.UserName,
                        CreatedByUserPhoto = appUser.UserDetail.ProfilePhotoPath,
                        CreatedDate = newPost.CreatedDate,
                        VideoUrl = videoUploadResult.Url?.ToString(),
                        PostType = newPost.PostType,
                        Comments = null
                    }));
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    Result.Status  = false;
                    Result.Message = ex.ToString();
                    return(BadResponse(Result));
                }
            }
        }
예제 #15
0
        public async Task <ActionResult> Edit(int id, PostViewModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var currentPost  = db.Posts.FirstOrDefault(p => p.PostId == id);
                var currentVideo = db.PostVideos.FirstOrDefault(p => p.PostId == id);
                var currentImage = db.PostImages.FirstOrDefault(p => p.PostId == id);

                if (currentPost != null)
                {
                    currentPost.Body        = model.Body;
                    currentPost.Description = model.Description;
                    currentPost.IsPublic    = model.IsPublic;
                    currentPost.Title       = model.Title;
                    currentPost.Modified    = DateTime.Now;

                    db.Entry(currentPost).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    Information("You have successfully edit post.", true);


                    if (model.VideoUrl != null && currentVideo != null)
                    {
                        currentVideo.VideoUrl        = YouTubeUrlHandler.GetVideoId(model.VideoUrl);
                        currentVideo.PostId          = id;
                        db.Entry(currentVideo).State = EntityState.Modified;
                        await db.SaveChangesAsync();

                        Information("You have successfully edit video url.", true);
                    }
                    else if (model.VideoUrl != null && currentVideo == null)
                    {
                        PostVideo postVideo = new PostVideo()
                        {
                            VideoUrl = YouTubeUrlHandler.GetVideoId(model.VideoUrl),
                            PostId   = id,
                        };
                        db.Entry(postVideo).State = EntityState.Added;
                        await db.SaveChangesAsync();

                        Success("Successfully add video to post.", true);
                    }

                    //Add Edit Image

                    if (file?.FileName != null && currentImage != null)
                    {
                        if (file.FileName != currentImage.ImageUrl)
                        {
                            currentImage.ImageUrl        = UploadPhoto(file);
                            currentImage.PostId          = id;
                            db.Entry(currentImage).State = EntityState.Modified;
                            await db.SaveChangesAsync();

                            Information("Successfully change picture to post.", true);
                        }
                    }
                    else if (file?.FileName != null)
                    {
                        PostImage postImage = new PostImage()
                        {
                            ImageUrl = UploadPhoto(file),
                            PostId   = id,
                        };
                        db.PostImages.Add(postImage);
                        await db.SaveChangesAsync();

                        Information("Successfully add picture to post.", true);
                    }
                    else if (currentImage != null && file?.FileName == null)
                    {
                        db.Entry(currentImage).State = EntityState.Deleted;
                        await db.SaveChangesAsync();

                        Information("Remove picture from post.");
                    }
                }

                return(RedirectToAction("Index", "Home"));
            }
            Error("Something went wrong.", true);
            return(View());
        }