Skip to content

Instantly share code, notes, and snippets.

@Chriz76
Created May 15, 2021 18:53
Show Gist options
  • Save Chriz76/f04560ba20f4a055582584edf0c07e2c to your computer and use it in GitHub Desktop.
Save Chriz76/f04560ba20f4a055582584edf0c07e2c to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using UserService.Data;
using UserService.Entities;
namespace UserService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly UserServiceContext _context;
public UsersController(UserServiceContext context)
{
_context = context;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUser()
{
return await _context.User.ToListAsync();
}
[HttpPut("{id}")]
public async Task<IActionResult> PutUser(int id, User user)
{
_context.Entry(user).State = EntityState.Modified;
await _context.SaveChangesAsync();
return NoContent();
}
[HttpPost]
public async Task<ActionResult<User>> PostUser(User user)
{
_context.User.Add(user);
await _context.SaveChangesAsync();
return CreatedAtAction("GetUser", new { id = user.ID }, user);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment