public async Task <Dtos.Person> InsertPerson(Dtos.Person person) { // Convert to entity var entity_person = ToEntity(person, db_context); // Add to database db_context.People.Add(entity_person); await db_context.SaveChangesAsync(); return(ToDto(entity_person)); }
internal static Entities.Person ToEntity(Dtos.Person person, AspNetCoreSpaPrerenderingDbContext db_context) { if (person == null) { return(null); } return(new Entities.Person { Id = person.Id, FirstName = person.FirstName, LastName = person.LastName }); }
public async Task <Dtos.Person> UpdatePerson(Dtos.Person person) { // Find existing person var entity_person = db_context.People.Find(person.Id); // Set new properties entity_person.FirstName = person.FirstName; entity_person.LastName = person.LastName; // Update in database db_context.Entry(entity_person).State = EntityState.Modified; return(ToDto(entity_person)); }
public async Task <ActionResult <int> > Post([FromBody] Dtos.Person person) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var id = await _personService.AddPersonAsync(person); return(CreatedAtAction(nameof(Get), new { id }, id)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <int> AddPersonAsync(Dtos.Person person) { if (!(await _groupRepository.DoesGroupExistAsnyc(person.GroupId))) { throw new ArgumentException($"{nameof(person.GroupId)} is not valid"); } var personEntity = new Person { FirstName = person.FirstName, LastName = person.LastName, PersonGroup = new PersonGroup { GroupId = person.GroupId }, CreatedDate = DateTimeOffset.UtcNow }; var personId = await _personRepository.AddPersonAsync(personEntity); return(personId); }