SuperAPI AI Directory

d1-06-crud-endpoints

Coding & development

View repository

Updated 10-16 02:08

About this skill

Day 1 · Step 6 — CRUD endpoints, then redeploy

Before starting: Ask which entity to create endpoints for (must match one already migrated in D1_05). Do not assume or proceed without confirmation.

up-api/Controllers/<Entity>sController.cs:

[ApiController]
[Route("api/[controller]")]
public class <Entity>sController : ControllerBase
{
    private readonly AppDbContext _db;
    public <Entity>sController(AppDbContext db) => _db = db;

    [HttpGet]
    public async Task<IActionResult> All() => Ok(await _db.<Entity>s.ToListAsync());
[HttpGet("detail/{id}")]
public async Task<IActionResult> Detail(int id)
{
    var item = await _db.<Entity>s.FindAsync(id);
    return item is null ? NotFound() : Ok(item);
}

[HttpPost("save")]
public async Task<IActionResult> Save(<Entity> input)
{
    _db.<Entity>s.Add(input);
    await _db.SaveChangesAsync();
    return Ok(input);
}

[HttpPut("update/{id}")]
public async Task<IActionResult> Update(int id, <Entity> input)
{
    var item = await _db.<Entity>s.FindAsync(id);
    if (item is null) return NotFound();
    // copy updated fields from input onto item
    await _db.SaveChangesAsync();
    return Ok(item);
}

[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
    var item = await _db.<Entity>s.FindAsync(id);
    if (item is null) return NotFound();
    _db.<Entity>s.Remove(item);
    await _db.SaveChangesAsync();
    return NoContent();
}

}