Created
April 20, 2019 23:41
-
-
Save gnzandrs/a1d7d073ca8449b38b4e48ba5cd17c6f to your computer and use it in GitHub Desktop.
dotnet core user controller crud methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using DotNetApi.Models; | |
namespace DotNetApi.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class UsuarioController : ControllerBase | |
{ | |
private readonly dbContext _context; | |
public UsuarioController(dbContext context) | |
{ | |
_context = context; | |
} | |
// GET: api/usuario | |
[HttpGet] | |
public async Task<ActionResult<IEnumerable<Usuario>>> GetUsuarios() | |
{ | |
return await _context.Usuario.Take(10).ToListAsync(); | |
} | |
// GET: api/usuario/2 | |
[HttpGet("{id}")] | |
public async Task<ActionResult<Usuario>> GetUsuario(int id) | |
{ | |
var usuario = await _context.Usuario.FindAsync(id); | |
if (usuario == null) | |
return NotFound(); | |
return usuario; | |
} | |
// POST: api/usuario | |
[HttpPost] | |
public async Task<ActionResult<Usuario>> AddUsuario(Usuario usuario) | |
{ | |
_context.Usuario.Add(usuario); | |
await _context.SaveChangesAsync(); | |
return CreatedAtAction(nameof(GetUsuario), new { id = usuario.IdUsuario }, usuario); | |
} | |
// PUT: api/usuario/2 | |
[HttpPut("{id}")] | |
public async Task<IActionResult> UpdateUsuario(int id, Usuario usuario) | |
{ | |
if (id != usuario.IdUsuario) | |
return BadRequest(); | |
_context.Entry(usuario).State = EntityState.Modified; | |
await _context.SaveChangesAsync(); | |
return NoContent(); | |
} | |
// DELETE: api/usuario/2 | |
[HttpDelete("{id}")] | |
public async Task<IActionResult> DeleteUsuario(int id) | |
{ | |
var usuario = await _context.Usuario.FindAsync(id); | |
if (usuario == null) | |
return NotFound(); | |
_context.Usuario.Remove(usuario); | |
await _context.SaveChangesAsync(); | |
return NoContent(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment