-
-
Save bjornmicallef/e89935c7f65236bba03b0ab77c847afc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserSettingsService : IUserSettingsService | |
{ | |
private readonly StockNotificationContext _dbContext; | |
public UserSettingsService(StockNotificationContext dbContext) | |
{ | |
_dbContext = dbContext; | |
} | |
public async Task UpdateUserSellerPreferences(string username, IEnumerable<int> sellerIds) | |
{ | |
var allSellerIds = await _dbContext.tblSellers.Select(x => x.Id).ToListAsync(); | |
var currentExclusions = await _dbContext.tblExcludedSellers.Where(x => x.Username == username).Select(x => x.SellerId).ToListAsync(); | |
// Determine insertions and removals based on provided ids and what's currently in db | |
var correctExclusions = allSellerIds.Except(sellerIds); | |
var removals = currentExclusions.Except(correctExclusions) | |
.Select(x => new tblExcludedSellers | |
{ | |
Username = username, | |
SellerId = x, | |
}); | |
var insertions = correctExclusions.Except(currentExclusions) | |
.Select(x => new tblExcludedSellers | |
{ | |
Username = username, | |
SellerId = x, | |
}); | |
_dbContext.tblExcludedSellers.AddRange(insertions); | |
_dbContext.tblExcludedSellers.RemoveRange(removals); | |
await _dbContext.SaveChangesAsync(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment