diff options
author | vnugent <public@vaughnnugent.com> | 2024-04-09 17:35:13 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-04-09 17:35:13 -0400 |
commit | 56e0a38b2ca246e8beeaef3c6c4b9c0ce7d0f09b (patch) | |
tree | 5ba2556e42510cbbdf9c287f67041c1b1eb46206 /back-end/src/Model/BookmarkStore.cs | |
parent | 0945210c0492dd8a8de99ccd8e5e66cf05e3a1c1 (diff) |
chore(app): Update deps, login spinner, curl msg, view prep
Diffstat (limited to 'back-end/src/Model/BookmarkStore.cs')
-rw-r--r-- | back-end/src/Model/BookmarkStore.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/back-end/src/Model/BookmarkStore.cs b/back-end/src/Model/BookmarkStore.cs index ec020e8..d53ab01 100644 --- a/back-end/src/Model/BookmarkStore.cs +++ b/back-end/src/Model/BookmarkStore.cs @@ -52,6 +52,44 @@ namespace SimpleBookmark.Model existing.Description = newRecord.Description; existing.JsonTags = newRecord.JsonTags; } + + public async Task<int> AddSingleIfNotExists(string userId, BookmarkEntry entry, DateTime now, uint maxRecords, CancellationToken cancellation) + { + ArgumentNullException.ThrowIfNull(userId); + ArgumentNullException.ThrowIfNull(entry); + + //Init new db connection + await using SimpleBookmarkContext context = new(dbOptions.Value); + + //Check if any bookmarks exist for the user with a given url + bool exists = await context.Bookmarks.AnyAsync(b => b.UserId == userId && b.Url == entry.Url, cancellation); + + //If no bookmarks exist, add a new one + if (!exists) + { + //Check if the user has reached the maximum number of bookmarks + if (await context.Bookmarks.CountAsync(b => b.UserId == userId, cancellation) >= maxRecords) + { + await context.SaveAndCloseAsync(true, cancellation); + return -1; + } + + context.Bookmarks.Add(new () + { + Id = GetNewRecordId(), //Overwrite with new record id + UserId = userId, //Enforce user id + Created = now, + LastModified = now, + Name = entry.Name, //Copy over the entry data + Url = entry.Url, + Description = entry.Description, + Tags = entry.Tags + }); + } + + await context.SaveAndCloseAsync(true, cancellation); + return exists ? 0 : 1; //1 if added, 0 if already exists + } public async Task<BookmarkEntry[]> SearchBookmarksAsync(string userId, string? query, string[] tags, int limit, int page, CancellationToken cancellation) { |