SHARE
TWEET

Untitled

a guest May 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. internal class ArtistHelper
  2. {
  3.     internal void CalculateUpdatedMembers(Artist old, Dtos.Artist @new, MusicDemonsContext dbContext, out IEnumerable<ArtistPerson> to_add, out IEnumerable<ArtistPerson> to_update, out IEnumerable<ArtistPerson> to_remove)
  4.     {
  5.         // Compute members to remove
  6.         to_remove = old.Members.Where(ap => {
  7.             if (@new.PastMembers.Any(p => p.Id == ap.PersonId)) return false;
  8.             if (@new.CurrentMembers.Any(p => p.Id == ap.PersonId)) return false;
  9.             return true;
  10.         });
  11.  
  12.         // Compute members to add
  13.         to_add = @new.CurrentMembers
  14.             .Select(p => new { PersonId = p.Id, Active = true })
  15.             .Concat(@new.PastMembers.Select(p => new { PersonId = p.Id, Active = false }))
  16.             .Where(p => !old.Members.Any(ap => ap.PersonId == p.PersonId))
  17.             .Select(p => new ArtistPerson
  18.             {
  19.                 Artist = old,
  20.                 ArtistId = old.Id,
  21.                 Person = dbContext.People.Find(p.PersonId),
  22.                 PersonId = p.PersonId,
  23.                 Active = p.Active
  24.             }).ToArray(); // Yield the result already
  25.  
  26.         // Compute members to update
  27.         to_update = old.Members.Except(to_remove);
  28.         foreach (var item in to_update)
  29.         {
  30.             if (@new.CurrentMembers.Any(m => m.Id == item.PersonId))
  31.                 item.Active = true;
  32.             else if (@new.PastMembers.Any(m => m.Id == item.PersonId))
  33.                 item.Active = false;
  34.             else
  35.                 throw new Exception("Not supposed to happen");
  36.         }
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top