private void UpdateTutorialCategories(string[] selectedCategories, Tutorial tutorialToUpdate) { if (selectedCategories == null) { tutorialToUpdate.TutorialCategories = new List <TutorialCategory>(); return; } var selectedCategoriesHS = new HashSet <string>(selectedCategories); var tutorialCategories = new HashSet <int>(tutorialToUpdate.TutorialCategories.Select(t => t.Category.Id)); foreach (var category in _context.Categories) { if (selectedCategoriesHS.Contains(category.Id.ToString())) { if (!tutorialCategories.Contains(category.Id)) { tutorialToUpdate.TutorialCategories.Add(new TutorialCategory { TutorialId = tutorialToUpdate.Id, CategoryId = category.Id }); } } else { if (tutorialCategories.Contains(category.Id)) { TutorialCategory categoryToRemove = tutorialToUpdate.TutorialCategories.FirstOrDefault(i => i.CategoryId == category.Id); _context.Remove(categoryToRemove); } } } }
public static SideMenuModelItem HydrateSideMenuModel(TutorialCategory tutorial) { return(new SideMenuModelItem { ID = tutorial.ID, Title = tutorial.Name }); }
public async Task <TutorialCategoryResponse> CreateAsync(TutorialCategory category) { try { category.OwnerId = _loggedUserId; _unitOfWork.TutorialCategories.Add(category); await _unitOfWork.SaveAsync(); return(new TutorialCategoryResponse(category)); } catch (DbUpdateException sqlEx) when(sqlEx.InnerException.HResult == (-2146232060)) { return(new TutorialCategoryResponse($"Zadane url již existuje", "UrlTitle")); } catch (Exception ex) { return(new TutorialCategoryResponse($"An error occurred when saving the category: {ex.Message}", "")); } }
public async Task <IActionResult> Create([Bind("Id,Title,Content,AuthorId")] Tutorial tutorial, string[] selectedCategories) { if (selectedCategories != null) { tutorial.TutorialCategories = new List <TutorialCategory>(); foreach (var category in selectedCategories) { var categoryToAdd = new TutorialCategory { TutorialId = tutorial.Id, CategoryId = int.Parse(category) }; tutorial.TutorialCategories.Add(categoryToAdd); } } if (ModelState.IsValid) { _context.Add(tutorial); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["AuthorId"] = new SelectList(_context.Users, "Id", "UserName", tutorial.AuthorId); PopulateCategoryData(tutorial); return(View(tutorial)); }
/// <summary> /// Creates a success response. /// </summary> /// <param name="category">Saved category.</param> /// <returns>Response.</returns> public TutorialCategoryResponse(TutorialCategory category) : this(true, string.Empty, category, "") { }
private TutorialCategoryResponse(bool success, string message, TutorialCategory category, string modelStateErrorKey) : base(success, message, modelStateErrorKey) { Category = category; }
public void Update(TutorialCategory entity) { this.Name = entity.Name; }