public async Task <IActionResult> PutContato(int id, Contato contato) { if (id != contato.ContatoId) { return(BadRequest()); } _context.Update(contato); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ContatoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Contato> > AlterarContato( [FromServices] ContatoContext context, [FromBody] Contato model) { try { if (ModelState.IsValid) { context.Contatos.Update(model); await context.SaveChangesAsync(); return(Ok()); } else { return(BadRequest(ModelState)); } } catch { return(NotFound()); throw; } }
public async Task <IActionResult> PutContato(string id, [FromBody] Contato contato) { bool HasAuth = Request.Headers.TryGetValue("Authorization", out APIKEY); if (!HasAuth) { return(Unauthorized("Não autorizado.")); } var oldContato = await _context.Contatos.FindAsync(id); if (oldContato == null) { return(NotFound("Contato não encontrado.")); } contato.IdContato = oldContato.IdContato; oldContato.Canal = contato.Canal; oldContato.Nome = contato.Nome; oldContato.Obs = contato.Obs; oldContato.Valor = contato.Valor; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ContatoExists(id)) { return(NotFound("Contato não encontrado.")); } else { return(BadRequest("Bad Request.")); } } return(NoContent()); }
public async Task <ActionResult <Contato> > CriarContato( [FromServices] ContatoContext context, [FromBody] Contato model) { if (ModelState.IsValid) { context.Contatos.Add(model); await context.SaveChangesAsync(); return(Ok(model.ContatoID)); } else { return(BadRequest(ModelState)); } }
public async Task <ActionResult <Contato> > ApagarContatoPorId(int id, [FromServices] ContatoContext context) { try { var contato = await context.Contatos .FirstOrDefaultAsync(x => x.ContatoID == id); context.Remove(contato); await context.SaveChangesAsync(); return(Ok()); } catch (System.Exception) { return(NotFound()); throw; } }
public async Task <int> Incluir(Contato contato) { context.contatos.Add(contato); return(await context.SaveChangesAsync()); }