public async Task <IActionResult> Edit(TutorialPostViewModel viewModel, int id) { if (ModelState.IsValid) { var response = await _tutorialPostService.UpdateAsync(viewModel); if (response == null) { return(NotFound()); } if (response.Success) { return(RedirectToAction(nameof(Index), new { categoryId = viewModel.Post.TutorialCategoryId })); } ModelState.AddModelError(response.ModelStateErrorKey, response.Message); } var vm = await _tutorialPostService.GetEditPostViewModel(id, viewModel.Post); if (vm == null) { return(NotFound()); } return(View(vm)); }
public async Task <Response> CreateAsync(TutorialPostViewModel viewModel) { var category = await GetCategoryAsync(viewModel.Post.TutorialCategoryId); if (category == null) { return(new Response($"Kategorie nebyla nalezena", "")); } if (!await IsUrlUniqueInCategory(viewModel.Post)) { return(new Response($"Zadane url již existuje", "Post.UrlTitle")); } try { var newPost = _mapper.Map <TutorialPost>(viewModel.Post); newPost.OwnerId = _loggedUserId; var tagIds = await _tagParser.ParseTags(viewModel.Tagy); tagIds.ForEach(id => newPost.TutorialPostTags.Add(new TutorialPostTag() { TagId = id })); _unitOfWork.TutorialPosts.Add(newPost); await _unitOfWork.SaveAsync(); return(new Response()); } catch (Exception ex) { return(new Response($"An error occurred when saving the category: {ex.Message}", "")); } }
public async Task <IActionResult> Index(int categoryId) { var postsAndCategory = await _tutorialPostService.GetAllAsync(categoryId); if (postsAndCategory == null) { return(NotFound()); } var dtos = _mapper.Map <List <TutorialPostDto> >(postsAndCategory.Item2); var viewModel = new TutorialPostViewModel() { Posts = dtos, CategoryName = postsAndCategory.Item1.Name, Category = _mapper.Map <TutorialCategoryDto>(postsAndCategory.Item1), }; return(View(viewModel)); }
public async Task <Response> UpdateAsync(TutorialPostViewModel viewModel) { var postToUpdate = await _unitOfWork.TutorialPosts.FindByIdAsync(viewModel.Post.Id); if (!await IsUrlUniqueInCategory(viewModel.Post)) { return(new Response($"Zadane url již existuje", "Post.UrlTitle")); } // when category is loaded, ef automatically fills post.category relation object, then it requires it to be not null in post, otherwise will delet //var category = await GetCategoryAsync(viewModel.Post.TutorialCategoryId); //if (category == null) // return new Response($"Kategorie nebyla nalezena", ""); if (postToUpdate == null || (_loggedUser.IsInRole(Constants.Roles.Lector) && postToUpdate.OwnerId != _loggedUserId)) { return(null); } try { _mapper.Map(viewModel.Post, postToUpdate); // ef will delete post instead of update when category == null // postToUpdate.Category = category; var tagIds = await _tagParser.ParseTags(viewModel.Tagy); tagIds.ForEach(id => postToUpdate.TutorialPostTags.Add(new TutorialPostTag() { TagId = id, TutorialPostId = postToUpdate.Id })); await _unitOfWork.SaveAsync(); return(new Response()); } catch (Exception ex) { return(new Response($"An error occurred when saving the category: {ex.Message}", "")); } }
public async Task <TutorialPostViewModel> GetEditPostViewModel(int id, TutorialPostDto editedPost = null) { var postToEdit = await _unitOfWork.TutorialPosts.FindByIdAsync(id); if (postToEdit == null) { return(null); } var category = await GetCategoryAsync(postToEdit.TutorialCategoryId); if (category == null) { return(null); } var tags = await _unitOfWork.Tags.GetAllAsync(); var courses = await _unitOfWork.Courses.GetAllAsync(); var post = _mapper.Map <TutorialPostDto>(postToEdit); if (editedPost != null) { post = editedPost; } var viewModel = new TutorialPostViewModel() { CategoryName = category.Name, Post = post, Tags = tags.ToList(), Courses = courses.Select(course => new CourseDto() { Id = course.Id, Name = course.Name }).ToList(), }; return(viewModel); }
public async Task <TutorialPostViewModel> GetNewPostViewModel(int categoryId, TutorialPostDto existingPost = null) { var category = await GetCategoryAsync(categoryId); if (category == null) { return(null); } var tags = await _unitOfWork.Tags.GetAllAsync(); var courses = await _unitOfWork.Courses.GetAllAsync(); var post = new TutorialPostDto() { Category = _mapper.Map <TutorialCategoryDto>(category) }; if (existingPost != null) { existingPost.Category = post.Category; post = existingPost; } var viewModel = new TutorialPostViewModel() { CategoryName = category.Name, Post = post, Tags = tags.ToList(), Courses = courses.Select(course => new CourseDto() { Id = course.Id, Name = course.Name }).ToList(), }; return(viewModel); }