Esempio n. 1
0
        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));
            }
        }
Esempio n. 2
0
        public async Task TestCreateBookmarkAndGetById()
        {
            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
            });

            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);
        }