示例#1
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"));
        }
示例#2
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());
        }