Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="newPost"></param>
        /// <returns></returns>
        public async Task <bool> CreatePost(BlogPostInputModel newPost)
        {
            var client  = _clientFactory.CreateClient();
            var request = new HttpRequestMessage(HttpMethod.Post, $"{apiRootUrl}BlogPosts/");

            var blogPostDTO = new BlogPostDTO()
            {
                Author        = newPost.Author,
                Title         = newPost.Title,
                Text          = newPost.Text,
                PublishedDate = DateTime.Now
            };

            if (newPost.ImageFile != null)
            {
                newPost.ImageUrl = await _imageFileService.SaveImage(newPost.ImageFile);
            }

            // Serialize the model and set it as the content of our request
            var postJson = JsonSerializer.Serialize(newPost);

            request.Headers.Add("User-Agent", "IthWeb");
            request.Content = new StringContent(postJson, Encoding.UTF8, "application/json");

            var response = await client.SendAsync(request);

            return(response.IsSuccessStatusCode);
        }
Example #2
0
        public async Task <IActionResult> EditPost([Bind("Id", "Title", "ImageFile", "ImageUrl", "Text", "PublishedDate", "Author")] BlogPostInputModel inputModel)
        {
            var isUpdateSuccessful = await _blogService.UpdatePost(inputModel);

            if (isUpdateSuccessful)
            {
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Error"));
        }
Example #3
0
        public IActionResult Post(BlogPostInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            this.blogPostsService.CreatePost(model.Title, model.Content, model.ProductId);

            return(this.RedirectToAction(nameof(All)));
        }
        public async Task AddAsync(BlogPostInputModel blogPostInputModel)
        {
            await this.blogPostsRepository.AddAsync(new BlogPost
            {
                Title    = blogPostInputModel.Title,
                Content  = blogPostInputModel.Content,
                Author   = blogPostInputModel.Author,
                ImageUrl = blogPostInputModel.Image.ToString(),
            });

            await this.blogPostsRepository.SaveChangesAsync();
        }
Example #5
0
        public async Task <IActionResult> Index([Bind("Title", "Text", "ImageFile")] BlogPostInputModel inputModel)
        {
            inputModel.Author = User.Identity.Name;

            var isCreateSuccessful = await _blogService.CreatePost(inputModel);

            if (!isCreateSuccessful)
            {
                TempData["PostError"] = "Something went wrong, try again or contact support!";
            }

            return(RedirectToAction());
        }
Example #6
0
        public IActionResult Post()
        {
            var products = this.productService.GetAllProducts();

            var selectListItemProducts = products.Select(p => new SelectListItem
            {
                Value = p.Id.ToString(),
                Text  = p.Name
            })
                                         .ToList();

            var blogInputModel = new BlogPostInputModel
            {
                Products = selectListItemProducts
            };

            return(this.View(blogInputModel));
        }
        public async Task <IActionResult> AddBlogPost(BlogPostInputModel blogPostInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(blogPostInputModel));
            }

            try
            {
                await this.blogPostsService.AddAsync(blogPostInputModel);
            }
            catch (Exception)
            {
                return(this.View("DuplicateValue", blogPostInputModel));
            }

            return(this.RedirectToAction("Index"));
        }
Example #8
0
        public async Task <IActionResult> EditPost(int id)
        {
            var post = await _blogService.GetPostById(id);

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

            var vm = new BlogPostInputModel()
            {
                Id            = post.Id,
                Author        = post.Author,
                PublishedDate = post.PublishedDate,
                Title         = post.Title,
                Text          = post.Text,
                ImageUrl      = post.ImageUrl
            };

            return(View(vm));
        }
        public async Task <IActionResult> AddBlogPost(BlogPostInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            string imageUrl;

            try
            {
                imageUrl = await this.cloudinaryService.UploadPictureAsync(input.Image, input.Title);
            }
            catch (System.Exception)
            {
                // In case of missing Cloudinary configuration from appsettings.json
                imageUrl = GlobalConstants.Images.CloudinaryMissing;
            }

            await this.blogPostsService.AddAsync(input.Title, input.Content, input.Author, imageUrl);

            return(this.RedirectToAction("Index"));
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="updatedPost"></param>
        /// <returns></returns>
        public async Task <bool> UpdatePost(BlogPostInputModel updatedPost)
        {
            var client  = _clientFactory.CreateClient();
            var request = new HttpRequestMessage(HttpMethod.Put, $"{apiRootUrl}BlogPosts/{updatedPost.Id}");

            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("User-Agent", "IthWeb");

            var editedPost = new BlogPostDTO()
            {
                Id            = updatedPost.Id,
                Author        = updatedPost.Author,
                PublishedDate = updatedPost.PublishedDate,
                Title         = updatedPost.Title,
                Text          = updatedPost.Text,
                ImageUrl      = updatedPost.ImageUrl
            };

            if (updatedPost.ImageFile != null)
            {
                if (!string.IsNullOrEmpty(updatedPost.ImageUrl))
                {
                    var imageFileName = updatedPost.ImageUrl.Split('/').LastOrDefault();
                    await _imageFileService.DeleteImage(imageFileName);
                }

                editedPost.ImageUrl = await _imageFileService.SaveImage(updatedPost.ImageFile);
            }

            var postJson = JsonSerializer.Serialize(editedPost);

            request.Content = new StringContent(postJson, Encoding.UTF8, "application/json");

            var response = await client.SendAsync(request);

            return(response.IsSuccessStatusCode);
        }
        public void InsertBlogPost(BlogPostInputModel input)
        {
            var model = _mapper.Map <BlogPost>(input);

            _blogPostRepository.Insert(model);
        }