public async Task <ActionResult> Create([FromBody] BookmarkModel bookmark) { _logger.LogDebug($"Will try to create a new bookmark entry: {bookmark}"); string uri = "/api/v1/bookmarks/{0}"; if (string.IsNullOrEmpty(bookmark.Path) || string.IsNullOrEmpty(bookmark.DisplayName)) { return(InvalidArguments($"Invalid request data supplied. Missing Path or DisplayName!")); } try { var user = this.User.Get(); var outcome = await _repository.InUnitOfWorkAsync(async() => { // even if an id is supplied it is deliberately ignored var entity = await _repository.Create(new BookmarkEntity { Id = bookmark.Id, DisplayName = bookmark.DisplayName, Path = bookmark.Path, SortOrder = bookmark.SortOrder, Type = bookmark.Type == ItemType.Node ? Store.ItemType.Node : Store.ItemType.Folder, Url = bookmark.Url, UserName = user.Username, Favicon = bookmark.Favicon, AccessCount = bookmark.AccessCount }); return(true, entity); }); _logger.LogInformation($"Bookmark created with ID {outcome.value.Id}"); return(Created(string.Format(uri, outcome.value.Id), new Result <string> { Success = true, Message = $"Bookmark created with ID {outcome.value.Id}", Value = outcome.value.Id })); } catch (Exception EX) { _logger.LogError($"Could not create a new bookmark entry: {EX.Message}\nstack: {EX.StackTrace}"); return(ProblemDetailsResult( detail: $"Could not create bookmark because of error: {EX.Message}", statusCode: StatusCodes.Status500InternalServerError, title: Errors.CreateBookmarksError, instance: HttpContext.Request.Path)); } }
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)); } }