initial commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using Demo001.Data;
|
||||
using Demo001.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Demo001.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class TodoItemsController : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public TodoItemsController(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
|
||||
{
|
||||
return await _context.TodoItems.ToListAsync();
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<TodoItem>> GetTodoItem(int id)
|
||||
{
|
||||
var item = await _context.TodoItems.FindAsync(id);
|
||||
return item is null ? NotFound() : item;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<TodoItem>> CreateTodoItem(TodoItem item)
|
||||
{
|
||||
_context.TodoItems.Add(item);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetTodoItem), new { id = item.Id }, item);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> UpdateTodoItem(int id, TodoItem item)
|
||||
{
|
||||
if (id != item.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if (!await _context.TodoItems.AnyAsync(x => x.Id == id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Entry(item).State = EntityState.Modified;
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteTodoItem(int id)
|
||||
{
|
||||
var item = await _context.TodoItems.FindAsync(id);
|
||||
if (item is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.TodoItems.Remove(item);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user