public async Task TestUpdateBookmark() { Assert.NotNull(context); // create one item var itemId = NewId; var item = await repo.Create(new BookmarkEntity { Id = itemId, ChildCount = 0, Created = DateTime.UtcNow, DisplayName = "displayName", Path = "/", SortOrder = 0, Type = ItemType.Node, Url = "http://url", UserName = Username, Favicon = "favicon.ico" }); Assert.NotNull(item); Assert.Equal(itemId, item.Id); Assert.Equal("displayName", item.DisplayName); Assert.Equal(Username, item.UserName); // get item by id var bm = await repo.GetBookmarkById(itemId, Username); Assert.NotNull(bm); Assert.Equal(itemId, item.Id); Assert.Equal("displayName", bm.DisplayName); Assert.Equal(Username, bm.UserName); await repo.Update(new BookmarkEntity { Id = bm.Id, DisplayName = bm.DisplayName + "_changed", Path = bm.Path, SortOrder = 10, Url = "http://new-url", UserName = bm.UserName, AccessCount = 99, Favicon = "favicon1.ico" }); bm = await repo.GetBookmarkById(itemId, Username); Assert.NotNull(bm); Assert.Equal(itemId, item.Id); Assert.Equal("displayName_changed", bm.DisplayName); Assert.Equal(Username, bm.UserName); Assert.Equal(10, bm.SortOrder); Assert.Equal("http://new-url", bm.Url); Assert.Equal(99, bm.AccessCount); Assert.Equal("favicon1.ico", bm.Favicon); // failed update - empty path await Assert.ThrowsAsync <ArgumentException>(() => { return(repo.Update(new BookmarkEntity { Id = bm.Id, DisplayName = bm.DisplayName + "_changed", Path = "", SortOrder = 10, Url = "http://new-url", UserName = bm.UserName })); }); // failed update - unavalable path await Assert.ThrowsAsync <InvalidOperationException>(() => { return(repo.Update(new BookmarkEntity { Id = bm.Id, DisplayName = bm.DisplayName + "_changed", Path = "/this/path/is/not/known", SortOrder = 10, Url = "http://new-url", UserName = bm.UserName })); }); // bookmark not found bm = await repo.Update(new BookmarkEntity { Id = "some-id", DisplayName = bm.DisplayName + "_changed", Path = bm.Path, SortOrder = 10, Url = "http://new-url", UserName = bm.UserName }); Assert.Null(bm); }
public void Update(Bookmark entity) { _repository.Update(entity); }
public async Task <ActionResult> Update([FromBody] BookmarkModel bookmark) { _logger.LogDebug($"Will try to update existing bookmark entry: {bookmark}"); if (string.IsNullOrEmpty(bookmark.Path) || string.IsNullOrEmpty(bookmark.DisplayName) || string.IsNullOrEmpty(bookmark.Id) ) { return(InvalidArguments($"Invalid request data supplied. Missing ID, Path or DisplayName!")); } try { var user = this.User.Get(); var outcome = await _repository.InUnitOfWorkAsync <ActionResult>(async() => { var existing = await _repository.GetBookmarkById(bookmark.Id, user.Username); if (existing == null) { _logger.LogWarning($"Could not find a bookmark with the given ID '{bookmark.Id}'"); return(true, ProblemDetailsResult( detail: $"No bookmark found by ID: {bookmark.Id}", statusCode: StatusCodes.Status404NotFound, title: Errors.NotFoundError, instance: HttpContext.Request.Path)); } var childCount = existing.ChildCount; if (existing.Type == Store.ItemType.Folder) { // on save of a folder, update the child-count! var parentPath = existing.Path; var path = EnsureFolderPath(parentPath, existing.DisplayName); var nodeCounts = await _repository.GetChildCountOfPath(path, user.Username); if (nodeCounts != null && nodeCounts.Count > 0) { var nodeCount = nodeCounts.Find(x => x.Path == path); if (nodeCount != null) { childCount = nodeCount.Count; } } } var existingDisplayName = existing.DisplayName; var existingPath = existing.Path; var item = await _repository.Update(new BookmarkEntity { Id = bookmark.Id, Created = existing.Created, DisplayName = bookmark.DisplayName, Path = bookmark.Path, SortOrder = bookmark.SortOrder, Type = existing.Type, // it does not make any sense to change the type of a bookmark! Url = bookmark.Url, UserName = user.Username, ChildCount = childCount, Favicon = bookmark.Favicon, AccessCount = bookmark.AccessCount }); if (existing.Type == Store.ItemType.Folder && existingDisplayName != bookmark.DisplayName) { // if we have a folder and change the displayname this also affects ALL sub-elements // therefore all paths of sub-elements where this folder-path is present, need to be updated var newPath = EnsureFolderPath(bookmark.Path, bookmark.DisplayName); var oldPath = EnsureFolderPath(existingPath, existingDisplayName); _logger.LogDebug($"will update all old paths '{oldPath}' to new path '{newPath}'."); var bookmarks = await _repository.GetBookmarksByPathStart(oldPath, user.Username); if (bookmarks == null) { bookmarks = new List <BookmarkEntity>(); } foreach (var bm in bookmarks) { var updatedPath = bm.Path.Replace(oldPath, newPath); await _repository.Update(new BookmarkEntity { Id = bm.Id, Created = existing.Created, DisplayName = bm.DisplayName, Path = updatedPath, SortOrder = bm.SortOrder, Type = bm.Type, Url = bm.Url, UserName = bm.UserName, ChildCount = bm.ChildCount }); } } _logger.LogInformation($"Updated Bookmark with ID {item.Id}"); var result = new OkObjectResult(new Result <string> { Success = true, Message = $"Bookmark with ID '{existing.Id}' was updated.", Value = existing.Id }); return(true, result); }); return(outcome.value); } catch (Exception EX) { _logger.LogError($"Could not update bookmark entry: {EX.Message}\nstack: {EX.StackTrace}"); return(ProblemDetailsResult( detail: $"Could not update bookmark because of error: {EX.Message}", statusCode: StatusCodes.Status500InternalServerError, title: Errors.UpdateBookmarksError, instance: HttpContext.Request.Path)); } }