Exemplo n.º 1
0
 public async Task <IActionResult> DeleteBookmark(int id)
 {
     if (!await _BookmarkRepository.Delete(id))
     {
         return(NotFound());
     }
     return(Json(NoContent()));
 }
Exemplo n.º 2
0
        public string CreateOrDeleteBookmark(int postId)
        {
            var myUser = _userRepository.Where(p => p.Username == HttpContext.Current.User.Identity.Name).Single();

            if (!myUser.Bookmarks.Any(p => p.postId == postId))
            {
                _bookmarkRepository.Add(new Bookmark {
                    postId = postId, userId = myUser.Id
                });
                return("toastr.success('مطلب نشانه گذاری شد .')");
            }
            else
            {
                var bookmark = myUser.Bookmarks.FirstOrDefault(p => p.postId == postId);
                _bookmarkRepository.Delete(bookmark);
                return("toastr.success('مطلب از لیست نشانه گذاری ها حذف شد .')");
            }
        }
Exemplo n.º 3
0
        public IActionResult ToggleBookmark([FromBody] IDRequest request)
        {
            var response = new BaseResponse <bool>();

            var control = bookMarkRepo.FirstOrDefaultBy(x => x.UserID == CurrentUserID && x.RecipeID == request.Id);

            if (control != null)
            {
                bookMarkRepo.Delete(control);
            }
            else
            {
                bookMarkRepo.Add(new Bookmark
                {
                    UserID   = CurrentUserID,
                    RecipeID = request.Id
                });
                response.Data = true;
            }

            return(Ok(response));
        }
 public void Delete(Bookmark entity)
 {
     _repository.Delete(entity);
 }
Exemplo n.º 5
0
        public async Task TestCreateFolderHierarchy()
        {
            Assert.NotNull(context);

            // create a folder-hierarchy /A/B/C
            await repo.Create(new BookmarkEntity {
                Id          = NewId,
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "A",
                Path        = "/",
                SortOrder   = 0,
                Type        = ItemType.Folder,
                Url         = "http://url",
                UserName    = Username
            });

            await repo.Create(new BookmarkEntity {
                Id          = NewId,
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "B",
                Path        = "/A",
                SortOrder   = 0,
                Type        = ItemType.Folder,
                Url         = "http://url",
                UserName    = Username
            });

            await repo.Create(new BookmarkEntity {
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "C",
                Path        = "/A/B",
                SortOrder   = 0,
                Type        = ItemType.Folder,
                Url         = "http://url",
                UserName    = Username
            });

            var bms = await repo.GetBookmarksByPathStart("/A", Username);

            Assert.NotNull(bms);
            Assert.Equal(2, bms.Count);

            bms = await repo.GetAllBookmarks(Username);

            Assert.NotNull(bms);
            Assert.Equal(3, bms.Count);
            Assert.Equal("A", bms[0].DisplayName);
            Assert.Equal("B", bms[1].DisplayName);
            Assert.Equal("C", bms[2].DisplayName);

            bms = await repo.GetBookmarksByName("B", Username);

            Assert.NotNull(bms);
            Assert.Single(bms);
            Assert.Equal("/A", bms[0].Path);

            bms = await repo.GetBookmarksByPath("/A/B", Username);

            Assert.NotNull(bms);
            Assert.Single(bms);
            Assert.Equal("C", bms[0].DisplayName);

            var bm = await repo.GetFolderByPath("/A/B", Username);

            Assert.NotNull(bm);
            Assert.Equal("B", bm.DisplayName);
            Assert.Equal("/A", bm.Path);

            // invalid folder
            bm = await repo.GetFolderByPath("|A|B", Username);

            Assert.Null(bm);

            // use the structure from above and get the child-count of nodes
            // this is just a folder-structure of /A/B/C
            var nodes = await repo.GetChildCountOfPath("/A", Username);

            Assert.NotNull(nodes);
            Assert.Single(nodes);
            Assert.Equal(1, nodes[0].Count);
            Assert.Equal("/A", nodes[0].Path);

            nodes = await repo.GetChildCountOfPath("", Username);

            Assert.NotNull(nodes);
            Assert.Equal(3, nodes.Count);
            Assert.Equal(1, nodes[0].Count);
            Assert.Equal("/", nodes[0].Path);

            // create a node to an existing path
            var nodeID = NewId;

            bm = await repo.Create(new BookmarkEntity {
                Id          = nodeID,
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "URL",
                Path        = "/A/B",
                SortOrder   = 0,
                Type        = ItemType.Node,
                Url         = "http://url",
                UserName    = Username
            });

            Assert.NotNull(bm);

            // get the folder
            bm = await repo.GetFolderByPath("/A/B", Username);

            Assert.NotNull(bm);
            Assert.Equal("B", bm.DisplayName);
            Assert.Equal("/A", bm.Path);
            Assert.Equal(2, bm.ChildCount); // sub-folder and the newly created node

            nodes = await repo.GetChildCountOfPath("/A/B", Username);

            Assert.NotNull(nodes);
            Assert.Single(nodes);
            Assert.Equal(2, nodes[0].Count);
            Assert.Equal("/A/B", nodes[0].Path);

            // unknown path
            nodes = await repo.GetChildCountOfPath("/A/B/C/D/E", Username);

            Assert.Empty(nodes);

            // remove a node
            Assert.True(await repo.Delete(new BookmarkEntity {
                Id = nodeID, UserName = Username
            }));

            bm = await repo.GetFolderByPath("/A/B", Username);

            Assert.NotNull(bm);
            Assert.Equal("B", bm.DisplayName);
            Assert.Equal("/A", bm.Path);
            Assert.Equal(1, bm.ChildCount); // sub-folder only

            Assert.False(await repo.Delete(new BookmarkEntity {
                Id = "-1", UserName = Username
            }));

            // we have the path /A/B/C
            // if we delete /A/B the only thing left will be /A
            Assert.True(await repo.DeletePath("/A/B", Username));

            bms = await repo.GetAllBookmarks(Username);

            Assert.NotNull(bms);
            Assert.Single(bms);

            await Assert.ThrowsAsync <ArgumentException>(() => {
                return(repo.DeletePath("", Username));
            });

            await Assert.ThrowsAsync <ArgumentException>(() => {
                return(repo.DeletePath("/", Username));
            });

            Assert.False(await repo.DeletePath("/D/E", Username));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> Delete(string id)
        {
            _logger.LogDebug($"Will try to delete existing bookmark with ID '{id}'");

            if (string.IsNullOrEmpty(id))
            {
                return(InvalidArguments($"Invalid request data supplied. Missing ID!"));
            }

            try
            {
                var user    = this.User.Get();
                var outcome = await _repository.InUnitOfWorkAsync <ActionResult>(async() => {
                    var existing = await _repository.GetBookmarkById(id, user.Username);
                    if (existing == null)
                    {
                        _logger.LogWarning($"Could not find a bookmark with the given ID '{id}'");
                        return(true, ProblemDetailsResult(
                                   detail: $"No bookmark found by ID: {id}",
                                   statusCode: StatusCodes.Status404NotFound,
                                   title: Errors.NotFoundError,
                                   instance: HttpContext.Request.Path));
                    }

                    // if the element is a folder and there are child-elements
                    // prevent the deletion - this can only be done via a recursive deletion like rm -rf
                    if (existing.Type == Store.ItemType.Folder && existing.ChildCount > 0)
                    {
                        _logger.LogWarning($"Cannot delete folder-elements '{existing.Path}/{existing.DisplayName}' because the item has child-elements '{existing.ChildCount}!");
                        return(false, ProblemDetailsResult(
                                   detail: $"Could not delete '{existing.DisplayName}' because of child-elements!",
                                   statusCode: StatusCodes.Status500InternalServerError,
                                   title: Errors.DeleteBookmarkError,
                                   instance: HttpContext.Request.Path));
                    }

                    var ok = await _repository.Delete(existing);
                    if (ok)
                    {
                        _logger.LogInformation($"Updated Bookmark with ID {id}");
                        var result = new OkObjectResult(new Result <string> {
                            Success = true,
                            Message = $"Bookmark with ID '{existing.Id}' was deleted.",
                            Value   = existing.Id
                        });
                        return(ok, result);
                    }
                    return(false, ProblemDetailsResult(
                               detail: $"Could not delete bookmark by ID: {id}",
                               statusCode: StatusCodes.Status500InternalServerError,
                               title: Errors.DeleteBookmarkError,
                               instance: HttpContext.Request.Path));
                });

                return(outcome.value);
            }
            catch (Exception EX)
            {
                _logger.LogError($"Could not delete the bookmark entry with ID '{id}': {EX.Message}\nstack: {EX.StackTrace}");
                return(ProblemDetailsResult(
                           detail: $"Could not delete bookmark because of error: {EX.Message}",
                           statusCode: StatusCodes.Status500InternalServerError,
                           title: Errors.DeleteBookmarkError,
                           instance: HttpContext.Request.Path));
            }
        }