private static SinglePersonDto SinglePersonToDo(Person person) { var ret = new SinglePersonDto { ID = person.ID, Name = person.Name, Surname = person.Surname, Horses = new List <SingleHorseDto>() }; foreach (var horse in person.Horses) { var tmpHorse = new SingleHorseDto { ID = horse.ID, BoxID = horse.BoxID, OwnerID = horse.OwnerID, OwnerFullName = new string(horse.Owner.Name + " " + horse.Owner.Surname), Name = horse.Name, MedicEntries = horse.MedicEntries }; ret.Horses.Add(tmpHorse); } return(ret); }
public async Task <IActionResult> UpdatePerson(int id, SinglePersonDto personDto) { if (personDto.Name == null || personDto.Surname == null) { return(BadRequest()); } var person = await _context.Persons .FindAsync(id); if (person == null) { return(NotFound()); } person.Name = personDto.Name; person.Surname = personDto.Surname; if (personDto.Horses != null) { var listHorse = new List <Horse>(); foreach (var horse in personDto.Horses) { var tmpHorse = new Horse { ID = horse.ID, BoxID = horse.BoxID, OwnerID = horse.OwnerID, Owner = person, Name = horse.Name, MedicEntries = horse.MedicEntries }; listHorse.Add(tmpHorse); } person.Horses = listHorse; } await _context.SaveChangesAsync(); return(Ok()); }