public IActionResult MoveCategory(MoveCategoryCommand cmd) { var parentCategory = _db.TestCategories.Find(cmd.ParentCategoryId); if (parentCategory == null) { return(BadRequest($"Parent test category with id={cmd.ParentCategoryId} doesn't exist")); } var categories = _db.TestCategories.Where(c => c.ParentId == cmd.ParentCategoryId).ToList().OrderBy(c => c.Position); if (categories.Count() < 2) { return(BadRequest($"Parent test category with id={cmd.ParentCategoryId} contain less then 2 nodes")); } var category = categories.FirstOrDefault(c => c.Id == cmd.CategoryId); if (category == null) { return(NotFound($"Test category with id={cmd.CategoryId} and ParentId={cmd.ParentCategoryId} doesn't exist")); } var maxPosition = Math.Max(category.Position, cmd.Position); var minPosition = Math.Min(category.Position, cmd.Position); category.Position = cmd.Position; int shift = cmd.Position == minPosition ? 1 : -1; categories.Where(c => (c.Position >= minPosition && c.Position <= maxPosition && c.Id != category.Id)) .ToList() .ForEach(c => c.Position += shift); _db.SaveChanges(); return(Ok()); }
public IActionResult MoveCategory(MoveCategoryCommand cmd) { throw new NotImplementedException(); }