File size: 4,279 Bytes
4433399
1fe511c
 
 
 
 
 
 
 
ccc2569
1fe511c
 
 
ccc2569
1fe511c
 
ccc2569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe511c
 
 
 
 
 
 
 
 
ccc2569
 
 
 
 
 
 
 
 
 
 
 
1fe511c
 
 
 
 
 
 
 
 
 
 
 
 
ccc2569
1fe511c
 
 
 
 
 
 
 
 
 
 
ccc2569
1fe511c
 
 
 
 
 
 
 
 
 
ccc2569
 
 
 
 
 
 
 
 
 
 
 
1fe511c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using LibraryManagement.Shared.Models;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using System.Text.Json;

namespace BlazorFrontend.Services;

public class WishlistService
{
    private readonly ProtectedLocalStorage _localStorage;
    private readonly LibraryApiClient _apiClient;
    private const string WishlistKey = "user_wishlist";
    private const string FavouriteKey = "user_favourites";

    public WishlistService(ProtectedLocalStorage localStorage, LibraryApiClient apiClient)
    {
        _localStorage = localStorage;
        _apiClient = apiClient;
    }

    public async Task SyncCurrentWishlistAsync()
    {
        var list = await GetWishlistAsync();
        await SyncWithServerAsync(list);
    }

    private async Task SyncWithServerAsync(List<BookDto> list)
    {
        try
        {
            var bookIds = list.Select(b => b.Id).ToList();
            await _apiClient.SyncWishlistAsync(bookIds);
        }
        catch { }
    }

    public async Task<List<BookDto>> GetWishlistAsync()
    {
        try
        {
            var result = await _localStorage.GetAsync<string>(WishlistKey);
            if (result.Success && !string.IsNullOrWhiteSpace(result.Value))
            {
                var localList = JsonSerializer.Deserialize<List<BookDto>>(result.Value) ?? new List<BookDto>();
                if (!localList.Any()) return localList;

                // Refresh from server
                var latestList = await _apiClient.GetBooksByIdsAsync(localList.Select(b => b.Id).ToList());
                if (latestList.Any())
                {
                    // Update local storage with fresh data
                    await _localStorage.SetAsync(WishlistKey, JsonSerializer.Serialize(latestList));
                    return latestList;
                }
                return localList;
            }
        }
        catch { }
        return new List<BookDto>();
    }

    public async Task AddToWishlistAsync(BookDto book)
    {
        var list = await GetWishlistAsync();
        if (!list.Any(b => b.Id == book.Id))
        {
            list.Add(book);
            await _localStorage.SetAsync(WishlistKey, JsonSerializer.Serialize(list));
            await SyncWithServerAsync(list);
        }
    }

    public async Task RemoveFromWishlistAsync(int bookId)
    {
        var list = await GetWishlistAsync();
        var item = list.FirstOrDefault(b => b.Id == bookId);
        if (item != null)
        {
            list.Remove(item);
            await _localStorage.SetAsync(WishlistKey, JsonSerializer.Serialize(list));
            await SyncWithServerAsync(list);
        }
    }

    public async Task<List<BookDto>> GetFavouritesAsync()
    {
        try
        {
            var result = await _localStorage.GetAsync<string>(FavouriteKey);
            if (result.Success && !string.IsNullOrWhiteSpace(result.Value))
            {
                var localList = JsonSerializer.Deserialize<List<BookDto>>(result.Value) ?? new List<BookDto>();
                if (!localList.Any()) return localList;

                // Refresh from server
                var latestList = await _apiClient.GetBooksByIdsAsync(localList.Select(b => b.Id).ToList());
                if (latestList.Any())
                {
                    // Update local storage with fresh data
                    await _localStorage.SetAsync(FavouriteKey, JsonSerializer.Serialize(latestList));
                    return latestList;
                }
                return localList;
            }
        }
        catch { }
        return new List<BookDto>();
    }

    public async Task AddToFavouritesAsync(BookDto book)
    {
        var list = await GetFavouritesAsync();
        if (!list.Any(b => b.Id == book.Id))
        {
            list.Add(book);
            await _localStorage.SetAsync(FavouriteKey, JsonSerializer.Serialize(list));
        }
    }

    public async Task RemoveFromFavouriteAsync(int bookId)
    {
        var list = await GetFavouritesAsync();
        var item = list.FirstOrDefault(b => b.Id == bookId);
        if (item != null)
        {
            list.Remove(item);
            await _localStorage.SetAsync(FavouriteKey, JsonSerializer.Serialize(list));
        }
    }
}