Created
May 15, 2021 18:53
-
-
Save Chriz76/f04560ba20f4a055582584edf0c07e2c to your computer and use it in GitHub Desktop.
This file contains 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.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