Spaces:
Sleeping
Sleeping
File size: 1,648 Bytes
ccc2569 | 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 | using DbConnect.Data;
using DbConnect.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Backend.Features.Wishlist
{
public interface IWishlistService
{
Task<bool> SyncWishlistAsync(Guid userId, List<int> bookIds);
Task<List<int>> GetWishlistBookIdsAsync(Guid userId);
}
public class WishlistService : IWishlistService
{
private readonly AppDbContext _context;
public WishlistService(AppDbContext context)
{
_context = context;
}
public async Task<bool> SyncWishlistAsync(Guid userId, List<int> bookIds)
{
// 1. Remove existing wishlist items for this user
var existing = await _context.WishlistItems
.Where(w => w.UserId == userId)
.ToListAsync();
_context.WishlistItems.RemoveRange(existing);
// 2. Add new wishlist items
var newItems = bookIds.Select(bookId => new WishlistItem
{
UserId = userId,
BookId = bookId,
AddedAt = DateTime.UtcNow
});
await _context.WishlistItems.AddRangeAsync(newItems);
await _context.SaveChangesAsync();
return true;
}
public async Task<List<int>> GetWishlistBookIdsAsync(Guid userId)
{
return await _context.WishlistItems
.Where(w => w.UserId == userId)
.Select(w => w.BookId)
.ToListAsync();
}
}
}
|