Minimal-APIs / minimal-apis.github.io

Tutorials and samples for ASP.NET Core Minimal APIs
https://minimal-apis.github.io/
MIT License
65 stars 19 forks source link

Tutorial- Let's Build- Update and Item #73

Open DaneeTheDeveloper opened 2 years ago

DaneeTheDeveloper commented 2 years ago

`using Microsoft.OpenApi.Models; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer();

builder.Services.AddDbContext(options => options.UseInMemoryDatabase("items")); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo API", Description = "Keep track of your tasks", Version = "v1" }); }); var app = builder.Build();

app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Todo API V1"); });

app.MapGet("/", () => "Hello World!"); app.MapGet("/todos", async (TodoDb db) => await db.Todos.ToListAsync()); app.MapPost("/todos", async (TodoDb db, TodoItem todo) => { await db.Todos.AddAsync(todo); await db.SaveChangesAsync(); return Results.Created($"/todo/{todo.Id}", todo); }); app.MapGet("/todos/{id}", async (TodoDb db, int id) => await db.Todos.FindAsync(id)); app.MapPut("/todos/{id}", async ( TodoDb db, TodoItem updateTodo ,int id) => { var todo = await db.Todos.FindAsync(id);

        if (todo is null) return NotFound();

        todo.Item = updateTodo.Item;
        todo.IsComplete = updateTodo.IsComplete;

        await db.SaveChangesAsync();

        return Results.NoContent();

});

app.Run();

class TodoItem { public int Id { get; set; } public string? Item { get; set; } public bool IsComplete { get; set; }

}

class TodoDb : DbContext { public TodoDb(DbContextOptions options) : base(options) { } public DbSet Todos { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseInMemoryDatabase("Todos");
}

}`

DaneeTheDeveloper commented 2 years ago

`error CS0103: The name 'NotFound' does not exist in the current context

error CS4010: Cannot convert async lambda expression to delegate type 'Task<?>'. An async lambda expression may return void, Task or Task, none of which are convertible to 'Task<?>'. `

DaneeTheDeveloper commented 2 years ago

When walking through the tutorial, I received the above errors. Not sure why these errors are popping up. I even tried to see if I needed to add the attached namespace for the first error, but it did not work.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.notfound?view=aspnetcore-5.0#Microsoft_AspNetCore_Mvc_ControllerBase_NotFound