diff options
Diffstat (limited to 'back-end/src')
-rw-r--r-- | back-end/src/Endpoints/BookmarkEndpoint.cs | 2 | ||||
-rw-r--r-- | back-end/src/Model/BookmarkStore.cs | 36 |
2 files changed, 36 insertions, 2 deletions
diff --git a/back-end/src/Endpoints/BookmarkEndpoint.cs b/back-end/src/Endpoints/BookmarkEndpoint.cs index b7825d6..ec46097 100644 --- a/back-end/src/Endpoints/BookmarkEndpoint.cs +++ b/back-end/src/Endpoints/BookmarkEndpoint.cs @@ -317,7 +317,7 @@ namespace SimpleBookmark.Endpoints } //Try to update the records - ERRNO result = await Bookmarks.AddBulkAsync(sanitized, entity.Session.UserID, false, entity.EventCancellation); + ERRNO result = await Bookmarks.AddBulkAsync(sanitized, entity.Session.UserID, entity.RequestedTimeUtc, entity.EventCancellation); webm.Result = $"Successfully added {result} of {batch.Length} bookmarks"; webm.Success = true; diff --git a/back-end/src/Model/BookmarkStore.cs b/back-end/src/Model/BookmarkStore.cs index fbd3213..8578976 100644 --- a/back-end/src/Model/BookmarkStore.cs +++ b/back-end/src/Model/BookmarkStore.cs @@ -19,10 +19,13 @@ using System; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Collections.Generic; +using VNLib.Utils; using VNLib.Plugins.Extensions.Data; -using VNLib.Plugins.Extensions.Data.Abstractions; using VNLib.Plugins.Extensions.Loading; +using VNLib.Plugins.Extensions.Data.Abstractions; + namespace SimpleBookmark.Model { @@ -113,6 +116,37 @@ namespace SimpleBookmark.Model .ToArray(); } + public async Task<ERRNO> AddBulkAsync(IEnumerable<BookmarkEntry> bookmarks, string userId, DateTimeOffset now, CancellationToken cancellation) + { + //Init new db connection + await using SimpleBookmarkContext context = new(dbOptions.Value); + await context.OpenTransactionAsync(cancellation); + + //Setup clean bookmark instances + bookmarks = bookmarks.Select(b => new BookmarkEntry + { + Id = GetNewRecordId(), //new uuid + UserId = userId, //Set userid + LastModified = now.DateTime, + + //Allow reuse of created time + Created = b.Created, + Description = b.Description, + Name = b.Name, + Tags = b.Tags, + Url = b.Url, + }); + + //Filter out bookmarks that already exist + bookmarks = bookmarks.Where(b => !context.Bookmarks.Any(b2 => b2.UserId == userId && b2.Url == b.Url)); + + //Add bookmarks to db + context.AddRange(bookmarks); + + //Commit transaction + return await context.SaveAndCloseAsync(true, cancellation); + } + private sealed class BookmarkQueryLookup : IDbQueryLookup<BookmarkEntry> { public IQueryable<BookmarkEntry> GetCollectionQueryBuilder(IDbContextHandle context, params string[] constraints) |