Ejemplo n.º 1
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var post = _db.Posts.FirstOrDefault(p => p.Id == id);

            var viewModel = new CreateBlogPostViewModel()
            {
                Id                 = post.Id,
                Description        = post.Description,
                ShortDescription   = post.ShortDescription,
                Category           = post.Category,
                Modified           = post.Modified,
                PostedOn           = post.PostedOn,
                Published          = post.Published,
                Title              = post.Title,
                SelectedCategoryId = post.Category.Id,
                FrontPageFeature   = post.FrontPageFeature
            };

            return(View(viewModel));
        }
Ejemplo n.º 2
0
        public IActionResult CreateBlogPost(CreateBlogPostViewModel model)
        {
            if (ModelState.IsValid)
            {
                var blogPost = _mapper.Map <BlogPost>(model);

                _blogPostRepository.Add(blogPost);

                foreach (var tag in model.ContentTags)
                {
                    var contentTag = _contentTagRepository.GetByName(tag);
                    var blogtag    = new BlogPostContentTag
                    {
                        BlogPost     = blogPost,
                        ContentTag   = contentTag,
                        BlogPostId   = blogPost.Id,
                        ContentTagId = contentTag.Id
                    };

                    blogPost.BlogPostsContentTags.Add(blogtag);
                }

                _blogPostRepository.Update(blogPost);

                return(RedirectToAction("blogposts"));
            }
            return(View(model));
        }
Ejemplo n.º 3
0
        public IActionResult CreateBlogPost()
        {
            var model = new CreateBlogPostViewModel
            {
                ContentTags = _contentTagRepository.List().Select(s => s.Name).ToList()
            };

            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult Create(CreateBlogPostViewModel viewModel, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                var category = _db.Categories.FirstOrDefault(c => c.Id == viewModel.SelectedCategoryId);
                var post     = new Post();

                //Incase the selected category isn't in the database yet..
                if (category != null)
                {
                    post.Category = category;
                }
                else
                {
                    var selectedCategoryName = viewModel.CategoryList.ToList().FirstOrDefault(item => int.Parse(item.Value) == viewModel.SelectedCategoryId).Text;
                    _db.Categories.Add(new Category()
                    {
                        Name = selectedCategoryName
                    });
                    _db.SaveChanges();

                    var categoryRecord = _db.Categories.FirstOrDefault(c => c.Id == viewModel.SelectedCategoryId);
                    post.Category = categoryRecord;
                }

                // if user uploaded image, process it
                if (image != null)
                {
                    post.MimeType     = image.ContentType;
                    post.PhotoContent = new byte[image.ContentLength];

                    image.InputStream.Read(post.PhotoContent, 0, image.ContentLength);

                    var imageToResize = Image.FromStream(image.InputStream);
                    post.ThumbnailPhotoContent = GetImageThumbnail(imageToResize);
                }


                post.Description      = viewModel.Description;
                post.ShortDescription = viewModel.ShortDescription;
                post.Modified         = viewModel.Modified;
                post.Published        = viewModel.Published;
                post.PostedOn         = viewModel.PostedOn;
                post.Tags             = viewModel.Tags;
                post.Title            = viewModel.Title;
                post.FrontPageFeature = viewModel.FrontPageFeature;

                _db.Posts.Add(post);
                _db.SaveChanges();
                return(RedirectToAction("Admin"));
            }
            else
            {
                return(View(viewModel));
            }
        }
Ejemplo n.º 5
0
        public IActionResult Create(CreateBlogPostViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrors()));
            }

            var blog = _blogPostService.Create(vm.ToModel());

            return(Ok(BlogPostViewModel.From(blog)));
        }
Ejemplo n.º 6
0
        public async Task <BlogPostViewModel> CreateAsync(CreateBlogPostViewModel vm)
        {
            var response = await _httpClient.PostAsJsonAsync($"{_urlBase}/Create", vm);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            return(await response.Content.ReadFromJsonAsync <BlogPostViewModel>());
        }
Ejemplo n.º 7
0
        public ActionResult Edit(CreateBlogPostViewModel editedPost, FormCollection collection, HttpPostedFileBase image = null)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var postToUpdate = _db.Posts.FirstOrDefault(x => x.Id == editedPost.Id);
                    if (postToUpdate != null)
                    {
                        postToUpdate.Title            = editedPost.Title;
                        postToUpdate.ShortDescription = editedPost.ShortDescription;
                        postToUpdate.Description      = editedPost.Description;
                        postToUpdate.Published        = editedPost.Published;
                        postToUpdate.Modified         = DateTime.Today.Date;
                        postToUpdate.FrontPageFeature = editedPost.FrontPageFeature;

                        var newCategory = _db.Categories.FirstOrDefault(c => c.Id == editedPost.SelectedCategoryId);

                        if (postToUpdate.Category != newCategory)
                        {
                            postToUpdate.Category = newCategory;
                        }

                        if (image != null)
                        {
                            postToUpdate.PhotoContent = new byte[image.ContentLength];
                            image.InputStream.Read(postToUpdate.PhotoContent, 0, image.ContentLength);

                            postToUpdate.MimeType = image.ContentType;


                            var imageToResize = Image.FromStream(image.InputStream);
                            postToUpdate.ThumbnailPhotoContent = GetImageThumbnail(imageToResize);
                        }
                    }


                    _db.Entry(postToUpdate).State = System.Data.Entity.EntityState.Modified;
                    _db.SaveChanges();
                    TempData["message"] = string.Format($"Article with title: {editedPost.Title} has been updated!");
                    return(RedirectToAction("Admin"));
                }

                return(RedirectToAction("Admin"));
            }
            catch
            {
                return(View());
            }
        }
Ejemplo n.º 8
0
        public IActionResult Create(int blogId)
        {
            var blogs = _blogService.GetBlogs();

            return(Ok(CreateBlogPostViewModel.From(blogId, blogs)));
        }
 protected override async Task OnInitializedAsync()
 {
     BlogPost = await _blogPostService.GetCreateAsync(int.Parse(BlogId));
 }