public async Task <IActionResult> Update(int id, [FromForm] BlogUpdateModel model) { var blog = await _blogService.FindByIdAsync(id); if (id != model.Id) { return(BadRequest("Geçersiz id")); } var uploadModel = await baseController.UploadFile(model.Image, "image/jpeg"); if (uploadModel.UploadState == UploadState.Success) { model.ImagePath = uploadModel.NewName; await _blogService.UpdateAsync(_mapper.Map <Blog>(model)); return(NoContent()); } else if (uploadModel.UploadState == UploadState.NotExist) { model.ImagePath = blog.ImagePath; await _blogService.UpdateAsync(_mapper.Map <Blog>(model)); return(NoContent()); } else { return(BadRequest(uploadModel.ErrorMessage)); } }
public async Task UpdateAsync(BlogUpdateModel model) { MultipartFormDataContent formData = new MultipartFormDataContent(); if (model.Image != null) { var stream = new MemoryStream(); await model.Image.CopyToAsync(stream); var bytes = stream.ToArray(); ByteArrayContent byteContent = new ByteArrayContent(bytes); byteContent.Headers.ContentType = new MediaTypeHeaderValue(model.Image.ContentType); formData.Add(byteContent, nameof(BlogAddModel.Image), model.Image.FileName); } var user = _accessor.HttpContext.Session.GetObject <AppUserViewModel>("activeUser"); model.AppUserId = user.Id; formData.Add(new StringContent(model.Id.ToString()), nameof(BlogUpdateModel.Id)); formData.Add(new StringContent(model.AppUserId.ToString()), nameof(BlogUpdateModel.AppUserId)); formData.Add(new StringContent(model.ShortDescription), nameof(BlogUpdateModel.ShortDescription)); formData.Add(new StringContent(model.Description), nameof(BlogUpdateModel.Description)); formData.Add(new StringContent(model.Title), nameof(BlogUpdateModel.Title)); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessor.HttpContext.Session.GetString("token")); await _httpClient.PutAsync($"{model.Id}", formData); }
public async Task <IActionResult> Update(int id, [FromForm] BlogUpdateModel blogUpdateModel) { if (id != blogUpdateModel.Id) { return(BadRequest()); } var uploadModel = await UploadFormFileAsync(blogUpdateModel.FormFile, "image/jpeg", "blog"); if (uploadModel.UploadState == UploadState.Success) { blogUpdateModel.ImagePath = uploadModel.ImageName; await _blogService.UpdateAsync(_mapper.Map <Blog>(blogUpdateModel)); return(NoContent()); } else if (uploadModel.UploadState == UploadState.NotExists) { var updatedBlog = await _blogService.GetByIdAsync(blogUpdateModel.Id); blogUpdateModel.ImagePath = updatedBlog.ImagePath; await _blogService.UpdateAsync(_mapper.Map <Blog>(blogUpdateModel)); return(NoContent()); } else { return(BadRequest(uploadModel.ErrorMessage)); } }
public async Task UpdateAsync(BlogUpdateModel blogUpdateModel) { MultipartFormDataContent formData = new MultipartFormDataContent(); if (blogUpdateModel.Image != null) { var stream = new MemoryStream(); await blogUpdateModel.Image.CopyToAsync(stream); var bytes = stream.ToArray(); ByteArrayContent byteArrayContent = new ByteArrayContent(bytes); byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(blogUpdateModel.Image.ContentType); formData.Add(byteArrayContent, nameof(BlogUpdateModel.Image), blogUpdateModel.Image.FileName); } var user = _httpContextAccessor.HttpContext.Session.GetObject <AppUserViewModel>("activeUser"); blogUpdateModel.AppUserId = user.Id; formData.Add(new StringContent(blogUpdateModel.Id.ToString()), nameof(BlogUpdateModel.Id)); formData.Add(new StringContent(blogUpdateModel.AppUserId.ToString()), nameof(BlogUpdateModel.AppUserId)); formData.Add(new StringContent(blogUpdateModel.Title.ToString()), nameof(BlogUpdateModel.Title)); formData.Add(new StringContent(blogUpdateModel.ShortDescription.ToString()), nameof(BlogUpdateModel.ShortDescription)); formData.Add(new StringContent(blogUpdateModel.Description.ToString()), nameof(BlogUpdateModel.Description)); await _httpClient.PutAsync($"{blogUpdateModel.Id}", formData); }
public async Task UpdateAsync(BlogUpdateModel blogUpdateModel) { MultipartFormDataContent formDataContent = new MultipartFormDataContent(); if (blogUpdateModel.FormFile != null) { var stream = new MemoryStream(); await blogUpdateModel.FormFile.CopyToAsync(stream); var bytes = stream.ToArray(); // var bytes = await System.IO.File.ReadAllBytesAsync(blogAddModel.FormFile.FileName); ByteArrayContent byteArrayContent = new ByteArrayContent(bytes); byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(blogUpdateModel.FormFile.ContentType); formDataContent.Add(byteArrayContent, nameof(blogUpdateModel.FormFile), blogUpdateModel.FormFile.FileName); } var user = _httpContextAccesor.HttpContext.Session.GetObject <AppUserViewModel>("ActiveUser"); blogUpdateModel.AppUserId = user.Id; formDataContent.Add(new StringContent(blogUpdateModel.Id.ToString()), nameof(blogUpdateModel.Id)); formDataContent.Add(new StringContent(blogUpdateModel.AppUserId.ToString()), nameof(blogUpdateModel.AppUserId)); formDataContent.Add(new StringContent(blogUpdateModel.ShortDescription), nameof(blogUpdateModel.ShortDescription)); formDataContent.Add(new StringContent(blogUpdateModel.Description), nameof(blogUpdateModel.Description)); formDataContent.Add(new StringContent(blogUpdateModel.Title), nameof(blogUpdateModel.Title)); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _httpContextAccesor.HttpContext.Session.GetString("Token")); await _httpClient.PutAsync($"{blogUpdateModel.Id}", formDataContent); }
public async Task <IActionResult> Edit(BlogUpdateModel blogUpdateModel) { if (ModelState.IsValid) { await _blogApiService.UpdateAsync(blogUpdateModel); return(RedirectToAction("Index", "Blog")); } return(View(blogUpdateModel)); }
public async Task <IActionResult> Update(BlogUpdateModel model) { TempData["active"] = "blog"; if (ModelState.IsValid) { await _blogApiService.UpdateAsync(model); return(RedirectToAction("Index")); } return(View(model)); }
public async Task <IActionResult> Update(int id) { var blog = await _blogApiService.GetByIdAsync(id); var model = new BlogUpdateModel(); model.Description = blog.Description; model.Id = blog.Id; model.ShortDescription = blog.ShortDescription; model.Title = blog.Description; model.Image = blog.Image; return(View(model)); }
public async Task <IActionResult> Edit(int id, BlogUpdateModel blogUpdateModel) { TempData["active"] = "blog"; if (id != blogUpdateModel.Id) { return(NotFound()); } if (ModelState.IsValid) { await _blogApiService.UpdateAsync(blogUpdateModel); return(RedirectToAction(nameof(Index))); } return(View(blogUpdateModel)); }
public async Task <IActionResult> Edit(int id) { var updatedBlog = await _blogApiService.GetByIdAsync(id); if (updatedBlog != null) { BlogUpdateModel blogModel = new BlogUpdateModel { ShortDescription = updatedBlog.ShortDescription, Title = updatedBlog.Title, Description = updatedBlog.Description, Id = updatedBlog.Id }; return(View(blogModel)); } return(NotFound()); }
public async Task <ActionResult <Blog> > UpdateBlog(string id, BlogUpdateModel blog) { var blogToUpdate = _mapper.Map <Blog>(blog); var result = await _blogService.UpdateBlogAsync(id, blogToUpdate).ConfigureAwait(false); if (!result) { return(StatusCode(StatusCodes.Status500InternalServerError)); } var updatedBlog = await _blogService.GetBlogAsync(id).ConfigureAwait(false); if (updatedBlog == null) { return(NotFound()); } return(Ok(_mapper.Map <BlogDto>(updatedBlog))); }
public async Task <IActionResult> Update(int id, [FromForm] BlogUpdateModel blogUpdateModel) { if (id != blogUpdateModel.Id) { return(BadRequest("geçersiz id")); } var uploadModel = await UploadFileAsync(blogUpdateModel.Image, "image/jpeg"); if (uploadModel.UploadState == UploadState.Success) { var updatedBlog = await _blogService.FindByIdAsync(blogUpdateModel.Id); updatedBlog.ShortDescription = blogUpdateModel.ShortDescription; updatedBlog.Title = blogUpdateModel.Title; updatedBlog.Description = blogUpdateModel.Description; updatedBlog.ImagePath = uploadModel.NewName; await _blogService.UpdateAsync(updatedBlog); return(NoContent()); } else if (uploadModel.UploadState == UploadState.NotExist) { var updatedBlog = await _blogService.FindByIdAsync(blogUpdateModel.Id); updatedBlog.ShortDescription = blogUpdateModel.ShortDescription; updatedBlog.Title = blogUpdateModel.Title; updatedBlog.Description = blogUpdateModel.Description; await _blogService.UpdateAsync(updatedBlog); return(NoContent()); } else { return(BadRequest(uploadModel.ErrorMessage)); } }
public async Task <IActionResult> PutBlog(int id, [FromForm] BlogUpdateModel blogUpdateModel) { if (id != blogUpdateModel.Id) { return(BadRequest("geçersiz id")); } var uploadModel = await UploadImage(blogUpdateModel.Image, "image/jpeg"); if (uploadModel.UploadState == UploadState.success) { var updatedblog = await _blogService.FindByIdAsync(blogUpdateModel.Id); updatedblog.Description = blogUpdateModel.Description; updatedblog.ShortDescription = blogUpdateModel.ShortDescription; updatedblog.Title = blogUpdateModel.Title; updatedblog.ImagePath = uploadModel.NewName; await _blogService.UpdateAsync(updatedblog); } else if (uploadModel.UploadState == UploadState.notExist) { var updatedblog = await _blogService.FindByIdAsync(blogUpdateModel.Id); updatedblog.Description = blogUpdateModel.Description; updatedblog.ShortDescription = blogUpdateModel.ShortDescription; updatedblog.Title = blogUpdateModel.Title; //updatedblog.ImagePath = blogUpdateModel.ImagePath; await _blogService.UpdateAsync(updatedblog); } else { return(BadRequest("Resim kaydedilirken problem oluştu.")); } return(NoContent()); }