Untitled
unknown
plain_text
a year ago
1.3 kB
3
Indexable
Never
using Microsoft.AspNetCore.Mvc; [ApiController] [Route("api/todos")] public class TodoController : ControllerBase { private readonly TodoRepository _repository; public TodoController(TodoRepository repository) { _repository = repository; } [HttpGet] public IActionResult Get() { var todos = _repository.GetAll(); return Ok(todos); } [HttpGet("{id}")] public IActionResult Get(int id) { var todo = _repository.Get(id); if (todo == null) return NotFound(); return Ok(todo); } [HttpPost] public IActionResult Post([FromBody] Todo todo) { _repository.Add(todo); return CreatedAtAction(nameof(Get), new { id = todo.Id }, todo); } [HttpPut("{id}")] public IActionResult Put(int id, [FromBody] Todo todo) { var existingTodo = _repository.Get(id); if (existingTodo == null) return NotFound(); _repository.Update(todo); return NoContent(); } [HttpDelete("{id}")] public IActionResult Delete(int id) { var existingTodo = _repository.Get(id); if (existingTodo == null) return NotFound(); _repository.Delete(id); return NoContent(); } }