// GET: Posts/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var post = await _apiService.GetAsync <PostDto>($"/post/{id}"); if (post == null) { return(NotFound()); } var savePostViewModel = new SavePostViewModel { Id = post.Id, IsPublished = post.IsPublished, Title = post.Title, ShortDescription = post.ShortDescription, Content = post.Content, CategoryId = post.CategoryId, Tags = string.Join(";", post.Tags), FileUploadId = post.FileUploadId, }; var categories = await _apiService.GetAsync <IEnumerable <Category> >("/api/category"); ViewData["CategoryId"] = new SelectList(categories, "Id", "Name"); return(View(savePostViewModel)); }
public async Task <IActionResult> Edit(int id, [Bind("Title,CategoryId,Tags,File,ShortDescription,Content,IsPublished,FileUploadId,Id")] SavePostViewModel post) { if (id != post.Id) { return(NotFound()); } var categories = await _apiService.GetAsync <IEnumerable <Category> >("/api/category"); if (ModelState.IsValid) { var postDto = new PostDto { Id = post.Id, Title = post.Title, CategoryId = post.CategoryId, Content = post.Content, ShortDescription = post.ShortDescription, IsPublished = post.IsPublished, Tags = !string.IsNullOrEmpty(post.Tags) ? post.Tags.Split(";").ToList() : null }; if (post.File != null) { var fileUploadId = await _apiService.PostFileAsync("/file", post.File); if (string.IsNullOrEmpty(fileUploadId)) { ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", post.CategoryId); return(View(post)); } postDto.FileUploadId = Guid.Parse(fileUploadId); } var response = await _apiService.PostAsync("/post", postDto); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", post.CategoryId); return(View(post)); }
public async Task <IActionResult> Create([Bind("Title,CategoryId,Tags,File,ShortDescription,Content,IsPublished,Id")] SavePostViewModel post) { var categories = await _apiService.GetAsync <IEnumerable <Category> >("/api/category"); if (ModelState.IsValid) { var postDto = new PostDto { Title = post.Title, CategoryId = post.CategoryId, Content = post.Content, ShortDescription = post.ShortDescription, IsPublished = post.IsPublished, Tags = !string.IsNullOrEmpty(post.Tags) ? post.Tags.Split(";").ToList() : null }; var result = await _apiService.PostFileAsync("/file", post.File); if (result == "Unauthorized request! Please log out then log in again!" || string.IsNullOrEmpty(result)) { ViewData["Error"] = result; ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", post.CategoryId); return(View(post)); } postDto.FileUploadId = Guid.Parse(result); var response = await _apiService.PostAsync("/post", postDto); if (!response.IsSuccessStatusCode) { ViewData["Error"] = "An Error has occurred"; ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", post.CategoryId); return(View(post)); } return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(categories, "Id", "Name", post.CategoryId); return(View(post)); }