-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathBookmarkService.cs
More file actions
59 lines (47 loc) · 1.63 KB
/
Copy pathBookmarkService.cs
File metadata and controls
59 lines (47 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LinkDotNet.Blog.Web.Features.Services;
namespace LinkDotNet.Blog.Web.Features.Bookmarks;
public class BookmarkService : IBookmarkService
{
private readonly ILocalStorageService localStorageService;
public BookmarkService(ILocalStorageService localStorageService)
{
this.localStorageService = localStorageService;
}
public async Task<bool> IsBookmarked(string postId)
{
ArgumentException.ThrowIfNullOrEmpty(postId);
await InitializeIfNotExists();
var bookmarks = await localStorageService.GetItemAsync<HashSet<string>>("bookmarks");
return bookmarks.Contains(postId);
}
public async Task<IReadOnlyList<string>> GetBookmarkedPostIds()
{
await InitializeIfNotExists();
return await localStorageService.GetItemAsync<IReadOnlyList<string>>("bookmarks");
}
public async Task SetBookmark(string postId, bool isBookmarked)
{
ArgumentException.ThrowIfNullOrEmpty(postId);
await InitializeIfNotExists();
var bookmarks = await localStorageService.GetItemAsync<HashSet<string>>("bookmarks");
if (!isBookmarked)
{
bookmarks.Remove(postId);
}
else
{
bookmarks.Add(postId);
}
await localStorageService.SetItemAsync("bookmarks", bookmarks);
}
private async Task InitializeIfNotExists()
{
if (!await localStorageService.ContainsKeyAsync("bookmarks"))
{
await localStorageService.SetItemAsync("bookmarks", new List<string>());
}
}
}