public async Task <Unit> Handle(UpdateFamilyTreeMainPersonCommand request, CancellationToken cancellationToken) { FamilyTreeEntity familyTree = await _context.FamilyTrees .Include(ft => ft.People) .SingleOrDefaultAsync(ft => ft.CreatedBy.Equals(request.UserId) && ft.Id == request.Id, cancellationToken); if (familyTree == null) { throw new NotFoundException(nameof(FamilyTreeEntity), request.Id); } Person person = familyTree.People .SingleOrDefault(p => p.Id == request.PersonId); if (person == null) { throw new NotFoundException(nameof(Person), request.PersonId); } familyTree.MainPerson = person; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreateFamilyTreeCommand request, CancellationToken cancellationToken) { FamilyTreeEntity entity = new FamilyTreeEntity(); entity.Name = request.Name; entity.UserId = request.UserId; _context.FamilyTrees.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <Unit> Handle(DeleteFamilyTreeCommand request, CancellationToken cancellationToken) { FamilyTreeEntity entity = await _context.FamilyTrees .Where(t => t.CreatedBy.Equals(request.UserId) && (t.Id == request.Id)) .SingleOrDefaultAsync(cancellationToken); if (entity == null) { throw new NotFoundException(nameof(FamilyTreeEntity), request.Id); } _context.FamilyTrees.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreatePersonCommand request, CancellationToken cancellationToken) { FamilyTreeEntity familyTree = await _context.FamilyTrees .Where(t => t.CreatedBy.Equals(request.UserId) && t.Id == request.TreeId) .Include(ft => ft.People) .SingleOrDefaultAsync(cancellationToken); if (familyTree == null) { throw new NotFoundException(nameof(FamilyTreeEntity), request.TreeId); } Person person = new Person(); if (familyTree.People.Count == 0) { FamilyTie tie = new FamilyTie(); tie.Person = person; familyTree.MainPerson = person; _context.FamilyTies.Add(tie); } else { FamilyTie mainPersonTie = null; if (request.WifeId == 0) { mainPersonTie = await _context.FamilyTies .FirstAsync(p => p.PersonId == request.MainPersonId, cancellationToken); } else { mainPersonTie = await _context.FamilyTies .FirstAsync(p => p.PersonId == request.MainPersonId && p.MarriagePersonId == request.WifeId, cancellationToken); } switch (request.PersonRelationType) { case PersonRelationType.Sibling: await AddSibling(mainPersonTie, person, cancellationToken); break; case PersonRelationType.Parent: await AddParent(mainPersonTie, person, request.ParentNumber, cancellationToken); break; case PersonRelationType.Child: await AddChild(mainPersonTie, person, cancellationToken); break; case PersonRelationType.Lover: await AddLover(mainPersonTie, person, cancellationToken); break; default: break; } } familyTree.People.Add(person); CreateDefaults(request, person); _context.People.Add(person); await _context.SaveChangesAsync(cancellationToken); return(person.Id); }