Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DaneeTheDeveloper opened this issue Oct 7, 2021 · 2 comments
Open

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

DaneeTheDeveloper opened this issue Oct 7, 2021 · 2 comments

Comments

@DaneeTheDeveloper
Copy link

`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
Copy link
Author

`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
Copy link
Author

DaneeTheDeveloper commented Oct 7, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants