public async Task <IActionResult> Edit(int id, [Bind("PersonId,Person1Id,RelationshipId,Id")] PersonInRelationship personInRelationship)
        {
            if (id != personInRelationship.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(personInRelationship);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PersonInRelationshipExists(personInRelationship.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PersonId"]       = new SelectList(_context.Persons, "Id", "Id", personInRelationship.PersonId);
            ViewData["Person1Id"]      = new SelectList(_context.Persons, "Id", "Id", personInRelationship.Person1Id);
            ViewData["RelationshipId"] = new SelectList(_context.Relationships, "Id", "Id", personInRelationship.RelationshipId);
            return(View(personInRelationship));
        }
        private async Task <bool> ValidateRelationship(PersonInRelationship personInRelationship)
        {
            if (personInRelationship.Person1Id == personInRelationship.PersonId)
            {
                return(false);
            }

            var relation = await _uow.Relationships.FindAsync(personInRelationship.RelationshipId);

            var person = await _uow.Persons.FindAsync(personInRelationship.PersonId);

            var person1 = await _uow.Persons.FindAsync(personInRelationship.Person1Id);

            // Mom/Dad older than child
            if ((relation.Relation == Relation.Ema || relation.Relation == Relation.Isa) && person1.Age > person.Age)
            {
                return(false);
            }

            // Granddad/Grandmother older than grandchild
            if ((relation.Relation == Relation.Vanaema || relation.Relation == Relation.Vanaisa) && person1.Age > person.Age)
            {
                return(false);
            }

            return(true);
        }
        public async Task <IActionResult> Create([Bind("PersonId,Person1Id,RelationshipId,Id")] PersonInRelationship personInRelationship)
        {
            if (ModelState.IsValid)
            {
                _context.Add(personInRelationship);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PersonId"]       = new SelectList(_context.Persons, "Id", "Id", personInRelationship.PersonId);
            ViewData["Person1Id"]      = new SelectList(_context.Persons, "Id", "Id", personInRelationship.Person1Id);
            ViewData["RelationshipId"] = new SelectList(_context.Relationships, "Id", "Id", personInRelationship.RelationshipId);
            return(View(personInRelationship));
        }
        public async Task <IActionResult> PutPersonInRelationship(int id, PersonInRelationship personInRelationship)
        {
            if (id != personInRelationship.Id)
            {
                return(BadRequest());
            }

            if ((await ValidateRelationship(personInRelationship)) == false)
            {
                return(BadRequest());
            }

            await AddBackwardsRelationIfNeeded(personInRelationship);

            _uow.PersonInRelationships.Update(personInRelationship);
            await _uow.SaveChangesAsync();

            return(NoContent());
        }
        private async Task AddBackwardsRelationIfNeeded(PersonInRelationship personInRelationship)
        {
            var relation = (await _uow.Relationships.FindAsync(personInRelationship.RelationshipId)).Relation;

            if (relation == Relation.Abikaasa)
            {
                await _uow.PersonInRelationships.AddAsync(new PersonInRelationship()
                {
                    Person1Id      = personInRelationship.PersonId,
                    PersonId       = personInRelationship.Person1Id,
                    RelationshipId = personInRelationship.RelationshipId
                });
            }
            else if (relation == Relation.Vanaema || relation == Relation.Vanaisa)
            {
                await _uow.PersonInRelationships.AddAsync(new PersonInRelationship()
                {
                    Person1Id      = personInRelationship.PersonId,
                    PersonId       = personInRelationship.Person1Id,
                    RelationshipId = await _uow.Relationships.FindIdByString(Relation.Lapselaps)
                });
            }
            await _uow.SaveChangesAsync();
        }
        public async Task <ActionResult <PersonInRelationship> > PostPersonInRelationship(PersonInRelationship personInRelationship)
        {
            if ((await ValidateRelationship(personInRelationship)) == false)
            {
                return(BadRequest());
            }

            await AddBackwardsRelationIfNeeded(personInRelationship);

            await _uow.PersonInRelationships.AddAsync(personInRelationship);

            await _uow.SaveChangesAsync();

            return(CreatedAtAction("GetPersonInRelationship", new { id = personInRelationship.Id }, personInRelationship));
        }