public async Task <IActionResult> PostClient([FromBody] ClientWithPhones clientWithPhones) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Client client = ConvertToModelClient(clientWithPhones); _context.Clients.Add(client); await _context.SaveChangesAsync(); return(Ok(client.id)); }
private Client ConvertToModelClient(ClientWithPhones clientWithPhones) { List <Phone> phones = new List <Phone>(); foreach (string phoneNumber in clientWithPhones.phones) { phones.Add(new Phone { number = HttpUtility.HtmlEncode(phoneNumber) }); } Client client = new Client { client_name = HttpUtility.HtmlEncode(clientWithPhones.name), birth_date = HttpUtility.HtmlEncode(clientWithPhones.birthDate), cpf = HttpUtility.HtmlEncode(clientWithPhones.cpf), Phones = phones }; return(client); }
public async Task <IActionResult> PutClient([FromRoute] int id, [FromBody] ClientWithPhones clientWithPhones) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != clientWithPhones.id) { return(BadRequest()); } Client client = ConvertToModelClient(clientWithPhones); client.id = clientWithPhones.id; _context.Entry(client).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ClienteExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }