initial commit

This commit is contained in:
Ariel Roffman
2026-05-13 19:12:36 -04:00
parent 4ccd0481bc
commit 233da7e7ea
16 changed files with 372 additions and 2 deletions
+74
View File
@@ -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();
}
}