async Task PatchProfile(string userId, PatchProfileModel profileModel, bool remove = false) { var update = Builders <Profile> .Update; var updates = new List <UpdateDefinition <Profile> >(); // todo: reflection? if (remove) { if (profileModel.BookmarkIds?.Length > 0) { updates.Add(update.PullAll(e => e.BookmarkIds, profileModel.BookmarkIds)); } if (profileModel.WatchIds?.Length > 0) { updates.Add(update.PullAll(e => e.WatchIds, profileModel.WatchIds)); } } else { if (profileModel.BookmarkIds?.Length > 0) { updates.Add(update.AddToSetEach(e => e.BookmarkIds, profileModel.BookmarkIds)); } if (profileModel.WatchIds?.Length > 0) { updates.Add(update.AddToSetEach(e => e.WatchIds, profileModel.WatchIds)); } } await dbContext.Profiles.FindOneAndUpdateAsync( ud => ud.UserId == userId, update.Combine(updates)); }
public async Task <IActionResult> PatchRemove([FromBody] PatchProfileModel profileModel) { await profileService.RemoveFromProfile(User.GetUserId(), profileModel); return(NoContent()); }
public Task AddToProfile(string userId, PatchProfileModel profileModel) => PatchProfile(userId, profileModel);
public Task RemoveFromProfile(string userId, PatchProfileModel profileModel) => PatchProfile(userId, profileModel, remove: true);
public static PatchProfileModel WithBookmarkId(this PatchProfileModel model, string bookmarkId) => model with