/// <summary> /// Add or delete a link between any two records involving a person (ignores other types) /// </summary> internal static void WritePersonLink(SystematizerContext db, LinkInstruction cmd) { //sort out what to do bool writeBoxPerson = false, writePersonPerson = false; long boxId = 0, person1Id = 0, person2Id = 0; if (cmd.Link == LinkType.FromBoxToPerson) { writeBoxPerson = true; boxId = cmd.FromId; person1Id = cmd.ToId; } else if (cmd.Link == LinkType.FromPersonToBox) { writeBoxPerson = true; person1Id = cmd.FromId; boxId = cmd.ToId; } else if (cmd.Link == LinkType.FromPersonToPerson) { writePersonPerson = true; person1Id = cmd.FromId; person2Id = cmd.ToId; } //write BoxPerson if (writeBoxPerson) { var bp = db.BoxPerson.FirstOrDefault(r => r.BoxId == boxId && r.PersonId == person1Id); if (bp == null && !cmd.IsRemove) { bp = new BoxPerson { BoxId = boxId, PersonId = person1Id }; db.BoxPerson.Add(bp); } if (bp != null && cmd.IsRemove) { db.BoxPerson.Remove(bp); } } //write PersonPerson if (writePersonPerson) { var pp = db.PersonPerson.FirstOrDefault(r => r.Person1Id == person1Id && r.Person2Id == person2Id); if (pp == null && !cmd.IsRemove) { pp = new PersonPerson { Person1Id = person1Id, Person2Id = person2Id }; db.PersonPerson.Add(pp); } if (pp != null && cmd.IsRemove) { db.PersonPerson.Remove(pp); } var ppBack = db.PersonPerson.FirstOrDefault(r => r.Person1Id == person2Id && r.Person2Id == person1Id); if (ppBack == null) { ppBack = new PersonPerson { Person1Id = person2Id, Person2Id = person1Id }; db.PersonPerson.Add(ppBack); } if (ppBack != null && cmd.IsRemove) { db.PersonPerson.Remove(ppBack); } } db.SaveChanges(); }
/// <summary> /// Add or delete a link between any two records involving a person (ignores other types) /// </summary> public void WritePersonLink(LinkInstruction cmd) { using var db = new SystematizerContext(); DBUtil.WritePersonLink(db, cmd); }