From 56e0a38b2ca246e8beeaef3c6c4b9c0ce7d0f09b Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 9 Apr 2024 17:35:13 -0400 Subject: chore(app): Update deps, login spinner, curl msg, view prep --- back-end/src/Model/BookmarkStore.cs | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'back-end/src/Model/BookmarkStore.cs') 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 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 SearchBookmarksAsync(string userId, string? query, string[] tags, int limit, int page, CancellationToken cancellation) { -- cgit