Beispiel #1
0
        public async Task <Bookmark?> GetBookmark(string userId, int bookmarkId)
        {
            if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
            {
                return(null);
            }

            return(await this.db.Bookmarks
                   .SingleOrDefaultAsync(b => b.BookmarkId == bookmarkId));
        }
Beispiel #2
0
        public async Task <BookmarkGroup?> GetBookmarkGroup(string userId, int bookmarkGroupId,
                                                            bool includeBookmarks = false)
        {
            BookmarkGroup?group = await db.BookmarkGroups
                                  .Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
                                  .If(includeBookmarks, q => q.Include(bg => bg.Bookmarks))
                                  .SingleOrDefaultAsync();

            if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId))
            {
                return(null);
            }

            return(group);
        }
Beispiel #3
0
        public async Task <BookmarkGroup?> CreateBookmarkGroup(string userId, string title,
                                                               string color, int sortOrder, int bookmarkContainerId)
        {
            if (!BookmarkOwnershipTools
                .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
            {
                return(null);
            }

            BookmarkGroup newBookmarkGroup = new(title, color, sortOrder, bookmarkContainerId);

            await this.db.BookmarkGroups.AddAsync(newBookmarkGroup);

            await this.db.SaveChangesAsync();

            return(newBookmarkGroup);
        }
Beispiel #4
0
        public async Task <BookmarkContainer?> UpdateBookmarkContainer(string userId,
                                                                       BookmarkContainer bookmarkContainer)
        {
            BookmarkContainer?existingBookmarkContainer = await this.db.BookmarkContainers
                                                          .SingleOrDefaultAsync(bc =>
                                                                                bc.BookmarkContainerId == bookmarkContainer.BookmarkContainerId);

            if (existingBookmarkContainer == null ||
                !BookmarkOwnershipTools
                .IsBookmarkContainerOwner(this.db, userId, bookmarkContainer.BookmarkContainerId))
            {
                return(null);
            }

            // If the sort order has changed, then the other containers need to be shuffled around
            if (existingBookmarkContainer.SortOrder < bookmarkContainer.SortOrder)
            {
                // The container has been moved to a higher sort order
                var affectedContainers = db.BookmarkContainers
                                         .Where(bc => bc.ApplicationUserId == userId)
                                         .Where(bc => bc.SortOrder > existingBookmarkContainer.SortOrder)
                                         .Where(bc => bc.SortOrder <= bookmarkContainer.SortOrder)
                                         .ToList();

                affectedContainers.ForEach(bc => bc.SortOrder -= 1);
                // Let the save changes below save this
            }
            else if (existingBookmarkContainer.SortOrder > bookmarkContainer.SortOrder)
            {
                // The container has been moved to a lower sort order
                var affectedContainers = db.BookmarkContainers
                                         .Where(bc => bc.ApplicationUserId == userId)
                                         .Where(bc => bc.SortOrder < existingBookmarkContainer.SortOrder)
                                         .Where(bc => bc.SortOrder >= bookmarkContainer.SortOrder)
                                         .ToList();

                affectedContainers.ForEach(bc => bc.SortOrder += 1);
                // Let the save changes below save this
            }

            this.db.Entry(bookmarkContainer).State = EntityState.Modified;
            await this.db.SaveChangesAsync();

            return(bookmarkContainer);
        }
Beispiel #5
0
        public async Task <bool> DeleteBookmark(string userId, int bookmarkId)
        {
            Bookmark?bookmark = db.Bookmarks
                                .SingleOrDefault(b => b.BookmarkId == bookmarkId);

            if (bookmark == null)
            {
                return(false);
            }

            if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
            {
                return(false);
            }

            db.Bookmarks.Remove(bookmark);
            await db.SaveChangesAsync();

            return(true);
        }
Beispiel #6
0
        public async Task <bool> DeleteBookmarkGroup(string userId, int bookmarkGroupId)
        {
            BookmarkGroup?bookmarkGroup = await this.db.BookmarkGroups
                                          .SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroupId);

            if (bookmarkGroup == null)
            {
                return(false);
            }

            if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
            {
                return(false);
            }

            this.db.BookmarkGroups.Remove(bookmarkGroup);
            await this.db.SaveChangesAsync();

            return(true);
        }
Beispiel #7
0
        public async Task <Bookmark?> CreateBookmark(string userId, string title, string url,
                                                     string?notes, int sortOrder, int bookmarkGroupId)
        {
            if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
            {
                return(null);
            }

            Bookmark newBookmark = new(title, url, notes, sortOrder, bookmarkGroupId);

            await db.Bookmarks.AddAsync(newBookmark);

            await db.SaveChangesAsync();

            if (newBookmark.BookmarkId <= 0)
            {
                return(null);
            }

            return(newBookmark);
        }
Beispiel #8
0
        public async Task <bool> DeleteBookmarkContainer(string userId, int bookmarkContainerId)
        {
            BookmarkContainer?bookmarkContainer = await this.db.BookmarkContainers
                                                  .Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
                                                  .SingleOrDefaultAsync();

            if (bookmarkContainer == null)
            {
                return(false);
            }

            if (!BookmarkOwnershipTools.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
            {
                return(false);
            }

            this.db.BookmarkContainers.Remove(bookmarkContainer);
            await this.db.SaveChangesAsync();

            List <BookmarkContainer>?containers = this.db.BookmarkContainers
                                                  .Where(bc => bc.ApplicationUserId == userId)
                                                  .SortContainers()
                                                  .ToList();

            if (containers == null)
            {
                // The container *was* deleted, so indicate as such
                return(true);
            }

            // Fix up sort order just in case
            for (int i = 0; i < containers.Count; i++)
            {
                containers[i].SortOrder = i;
            }

            await this.db.SaveChangesAsync();

            return(true);
        }
Beispiel #9
0
        public async Task <BookmarkContainer?> GetBookmarkContainer(string userId,
                                                                    int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false)
        {
            BookmarkContainer?bookmarkContainer = await this.db.BookmarkContainers
                                                  .Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
                                                  .If(includeGroups, q => q.Include(bc => bc.BookmarkGroups))
                                                  .If(includeBookmarks, q => q
                                                      .Include(bc => bc.BookmarkGroups !)
                                                      .ThenInclude(bg => bg.Bookmarks))
                                                  .SingleOrDefaultAsync();

            if (bookmarkContainer == null)
            {
                return(null);
            }

            if (!BookmarkOwnershipTools
                .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
            {
                return(null);
            }

            return(bookmarkContainer);
        }
Beispiel #10
0
        public async Task <Bookmark?> UpdateBookmark(string userId, Bookmark bookmark)
        {
            Bookmark?existingBookmark = await db.Bookmarks
                                        .SingleOrDefaultAsync(b => b.BookmarkId == bookmark.BookmarkId);

            if (existingBookmark == null)
            {
                return(null);
            }

            if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
            {
                return(null);
            }

            // Could be moving to a different group
            if (!BookmarkOwnershipTools
                .IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
            {
                return(null);
            }

            if (bookmark.BookmarkGroupId != existingBookmark.BookmarkGroupId)
            {
                // The bookmark was moved to a different group - shuffle sort order around
                List <Bookmark>?oldGroupBookmarks = await db.Bookmarks
                                                    .Where(b => b.BookmarkGroupId == existingBookmark.BookmarkGroupId)
                                                    .Where(b => b.SortOrder > existingBookmark.SortOrder)
                                                    .ToListAsync();

                oldGroupBookmarks.ForEach(b => b.SortOrder -= 1);

                List <Bookmark>?newGroupBookmarks = await db.Bookmarks
                                                    .Where(b => b.BookmarkGroupId == bookmark.BookmarkGroupId)
                                                    .Where(b => b.SortOrder >= bookmark.SortOrder)
                                                    .ToListAsync();

                newGroupBookmarks.ForEach(b => b.SortOrder += 1);
            }
            else if (bookmark.SortOrder != existingBookmark.SortOrder)
            {
                // The bookmark has been moved within the same group

                List <Bookmark>?groupBookmarks = await db.Bookmarks
                                                 .Where(b => b.BookmarkGroupId == bookmark.BookmarkGroupId)
                                                 .Where(b => b.BookmarkId != bookmark.BookmarkId)
                                                 .ToListAsync();

                groupBookmarks
                .Where(b => b.SortOrder > existingBookmark.SortOrder)
                .ToList()
                .ForEach(b => b.SortOrder -= 1);

                groupBookmarks
                .Where(b => b.SortOrder > bookmark.SortOrder)
                .ToList()
                .ForEach(b => b.SortOrder += 1);
            }

            db.Entry(bookmark).State = EntityState.Modified;
            await db.SaveChangesAsync();

            return(bookmark);
        }
Beispiel #11
0
        public async Task <BookmarkGroup?> UpdateBookmarkGroup(string userId,
                                                               BookmarkGroup bookmarkGroup)
        {
            BookmarkGroup?existingGroup = await this.db.BookmarkGroups
                                          .SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId);

            if (existingGroup == null)
            {
                return(null);
            }

            if (!BookmarkOwnershipTools
                .IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId))
            {
                return(null);
            }

            // If it's been moved to a new container
            if (existingGroup.BookmarkContainerId != bookmarkGroup.BookmarkContainerId &&
                !BookmarkOwnershipTools
                .IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId))
            {
                return(null);
            }

            if (existingGroup.BookmarkContainerId != bookmarkGroup.BookmarkContainerId)
            {
                // It's been moved to a different container - shuffle the sort order around

                List <BookmarkGroup>?oldContainerGroups = await db.BookmarkGroups
                                                          .Where(bg => bg.BookmarkContainerId == existingGroup.BookmarkContainerId)
                                                          .Where(bg => bg.SortOrder > existingGroup.SortOrder)
                                                          .ToListAsync();

                oldContainerGroups.ForEach(bg => bg.SortOrder -= 1);

                List <BookmarkGroup>?newContainerGroups = await db.BookmarkGroups
                                                          .Where(bg => bg.BookmarkContainerId == bookmarkGroup.BookmarkContainerId)
                                                          .Where(bg => bg.SortOrder >= bookmarkGroup.SortOrder)
                                                          .ToListAsync();

                newContainerGroups.ForEach(bg => bg.SortOrder += 1);
            }
            else if (existingGroup.SortOrder != bookmarkGroup.SortOrder)
            {
                // The group was moved within the same container

                List <BookmarkGroup>?containerGroups = await db.BookmarkGroups
                                                       .Where(bg => bg.BookmarkContainerId == bookmarkGroup.BookmarkContainerId)
                                                       .Where(bg => bg.BookmarkGroupId != bookmarkGroup.BookmarkGroupId)
                                                       .ToListAsync();

                containerGroups
                .Where(bg => bg.SortOrder > existingGroup.SortOrder)
                .ToList()
                .ForEach(bg => bg.SortOrder -= 1);

                containerGroups
                .Where(bg => bg.SortOrder > bookmarkGroup.SortOrder)
                .ToList()
                .ForEach(bg => bg.SortOrder += 1);
            }

            this.db.Entry(bookmarkGroup).State = EntityState.Modified;
            await this.db.SaveChangesAsync();

            return(bookmarkGroup);
        }