public IActionResult Delete(int id) { BlogImageViewModel blogImageVm = _blogImageService.GetById(id); _blogImageService.Delete(id); _blogImageService.SaveChanges(); string pathImage = blogImageVm.Path; if (!string.IsNullOrEmpty(pathImage)) { pathImage.DeletementByString(_env); } return(new OkObjectResult(id)); }
public async Task <IActionResult> Update(PostViewModel model, string returnUrl = null) { var isBloger = HttpContext.User.IsInRole(Constants.BlogerRole); if (!isBloger) { return(RedirectToAction("Index", "Home")); } byte[] bytes = new byte[] { }; var fileName = ""; if (Request.Form.Files[0] != null) { var length = Request.Form.Files[0].Length; using (BinaryReader reader = new BinaryReader(Request.Form.Files[0].OpenReadStream())) { bytes = reader.ReadBytes((int)length); } fileName = Request.Form.Files[0].FileName; } if (ModelState.IsValid) { var imageId = Guid.NewGuid().ToString(); var post = await _postService.GetById(model.Id); if (post != null) { post.CategoryId = model.CategoryId; post.Title = model.Title; post.ShortDescription = model.ShortDescription; post.Content = model.Content; post.ThumbnailImage = fileName + "__" + imageId; post.UpdatedDate = DateTime.UtcNow; } await _postService.Update(post); await _blogImageService.Delete(post.ImageId); await _blogImageService.Insert(imageId, fileName, bytes); } return(RedirectToAction("GetPosts")); }