You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using AspNetCoreMVC.DataContext;
|
|
|
|
|
using AspNetCoreMVC.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace AspNetCoreMVC.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/user")] // 컨트롤러 전체 라우터 설정
|
|
|
|
|
public class UserController : Controller
|
|
|
|
|
{
|
|
|
|
|
private ModelDbContext _db;
|
|
|
|
|
|
|
|
|
|
public UserController(ModelDbContext db)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public JsonResult GetUser(int id)
|
|
|
|
|
{
|
|
|
|
|
var user = _db.Users.SingleOrDefault(u => u.Id == id);
|
|
|
|
|
return Json(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("v2/{id}")]
|
|
|
|
|
public IActionResult GetUser2(int id)
|
|
|
|
|
{
|
|
|
|
|
var user = _db.Users.SingleOrDefault(u => u.Id == id);
|
|
|
|
|
if (user == null)
|
|
|
|
|
return BadRequest("No data found");
|
|
|
|
|
|
|
|
|
|
return Ok(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public JsonResult Save([FromBody] User user)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
return Json(false);
|
|
|
|
|
|
|
|
|
|
_db.Users.Add(user);
|
|
|
|
|
_db.SaveChanges();
|
|
|
|
|
return Json(true);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(ex);
|
|
|
|
|
return Json(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|