Skip to content

Commit 48a71f7

Browse files
committed
set up minimal API endpoints
1 parent 4737754 commit 48a71f7

File tree

82 files changed

+75000
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+75000
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,7 @@ $RECYCLE.BIN/
475475

476476
# Windows shortcuts
477477
*.lnk
478+
/Todo.frockett/Todo.frockett/todos.db
479+
/Todo.frockett/Todo.frockett/todos.db-shm
480+
/Todo.frockett/Todo.frockett/todos.db-wal
481+
/Todo.frockett/Todo.frockett/Todo.frockett.http
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Todo.frockett.Data;
3+
using Todo.frockett.Models;
4+
5+
namespace Todo.frockett.API;
6+
7+
public class TodoEndpoints
8+
{
9+
// Referenced https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/route-handlers?view=aspnetcore-8.0#endpoint-defined-outside-of-programcs
10+
11+
public static void Map(WebApplication app)
12+
{
13+
// Minimal API Endpoints
14+
app.MapGet("/todoitems", async (TodoContext context) =>
15+
await context.TodoItems.ToListAsync());
16+
17+
app.MapGet("/todoitems/complete", async (TodoContext context) =>
18+
await context.TodoItems.Where(t => t.IsComplete == true).ToListAsync());
19+
20+
app.MapGet("/todoitems/{id}", async (int id, TodoContext context) =>
21+
22+
await context.TodoItems.FindAsync(id)
23+
is TodoItem item
24+
? Results.Ok(item)
25+
: Results.NotFound());
26+
27+
app.MapPost("/todoitems", async (TodoItem item, TodoContext context) =>
28+
{
29+
context.TodoItems.Add(item);
30+
await context.SaveChangesAsync();
31+
32+
return Results.Created($"/todoitems/{item.Id}", item);
33+
});
34+
35+
app.MapPut("/todoitems/{id}", async (int id, TodoItem inputTodo, TodoContext context) =>
36+
{
37+
var item = await context.TodoItems.FindAsync(id);
38+
39+
if (item is null) return Results.NotFound();
40+
41+
item.Name = inputTodo.Name;
42+
item.IsComplete = inputTodo.IsComplete;
43+
44+
await context.SaveChangesAsync();
45+
46+
return Results.NoContent();
47+
});
48+
49+
app.MapDelete("/todoitems/{id}", async (int id, TodoContext context) =>
50+
{
51+
if (await context.TodoItems.FindAsync(id) is TodoItem todo)
52+
{
53+
context.TodoItems.Remove(todo);
54+
await context.SaveChangesAsync();
55+
return Results.NoContent();
56+
}
57+
58+
return Results.NotFound();
59+
});
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.Diagnostics;
3+
using Todo.frockett.Models;
4+
5+
namespace Todo.frockett.Controllers;
6+
7+
public class HomeController : Controller
8+
{
9+
private readonly ILogger<HomeController> _logger;
10+
11+
public HomeController(ILogger<HomeController> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public IActionResult Index()
17+
{
18+
return View();
19+
}
20+
21+
public IActionResult Privacy()
22+
{
23+
return View();
24+
}
25+
26+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
27+
public IActionResult Error()
28+
{
29+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Design;
3+
using Todo.frockett.Models;
4+
5+
namespace Todo.frockett.Data;
6+
7+
public class TodoContext : DbContext
8+
{
9+
public TodoContext(DbContextOptions<TodoContext> options)
10+
: base(options)
11+
{
12+
}
13+
14+
public DbSet<TodoItem> TodoItems { get; set; }
15+
}

Todo.frockett/Todo.frockett/Migrations/20240508052804_InitialCreate.Designer.cs

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace Todo.frockett.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class InitialCreate : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.CreateTable(
14+
name: "TodoItems",
15+
columns: table => new
16+
{
17+
Id = table.Column<int>(type: "INTEGER", nullable: false)
18+
.Annotation("Sqlite:Autoincrement", true),
19+
Name = table.Column<string>(type: "TEXT", nullable: true),
20+
IsComplete = table.Column<bool>(type: "INTEGER", nullable: false)
21+
},
22+
constraints: table =>
23+
{
24+
table.PrimaryKey("PK_TodoItems", x => x.Id);
25+
});
26+
}
27+
28+
/// <inheritdoc />
29+
protected override void Down(MigrationBuilder migrationBuilder)
30+
{
31+
migrationBuilder.DropTable(
32+
name: "TodoItems");
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <auto-generated />
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
5+
using Todo.frockett.Data;
6+
7+
#nullable disable
8+
9+
namespace Todo.frockett.Migrations
10+
{
11+
[DbContext(typeof(TodoContext))]
12+
partial class TodoContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder.HasAnnotation("ProductVersion", "8.0.4");
18+
19+
modelBuilder.Entity("Todo.frockett.Models.TodoItem", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.Property<bool>("IsComplete")
26+
.HasColumnType("INTEGER");
27+
28+
b.Property<string>("Name")
29+
.HasColumnType("TEXT");
30+
31+
b.HasKey("Id");
32+
33+
b.ToTable("TodoItems");
34+
});
35+
#pragma warning restore 612, 618
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Todo.frockett.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Todo.frockett.Models;
2+
3+
public class TodoItem
4+
{
5+
public int Id { get; set; }
6+
public string? Name { get; set; }
7+
public bool IsComplete { get; set; }
8+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Todo.frockett.API;
3+
using Todo.frockett.Data;
4+
using Todo.frockett.Models;
5+
6+
var builder = WebApplication.CreateBuilder(args);
7+
8+
// Add services to the container.
9+
builder.Services.AddControllersWithViews();
10+
builder.Services.AddEndpointsApiExplorer();
11+
builder.Services.AddSwaggerGen();
12+
13+
builder.Services.AddDbContext<TodoContext>(options =>
14+
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
15+
16+
var app = builder.Build();
17+
18+
// Configure the HTTP request pipeline.
19+
if (!app.Environment.IsDevelopment())
20+
{
21+
app.UseExceptionHandler("/Home/Error");
22+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
23+
app.UseHsts();
24+
}
25+
26+
if (app.Environment.IsDevelopment())
27+
{
28+
app.UseSwagger();
29+
app.UseSwaggerUI();
30+
}
31+
32+
app.UseHttpsRedirection();
33+
app.UseStaticFiles();
34+
35+
app.UseRouting();
36+
37+
app.UseAuthorization();
38+
39+
app.MapControllerRoute(
40+
name: "default",
41+
pattern: "{controller=Home}/{action=Index}/{id?}");
42+
43+
TodoEndpoints.Map(app);
44+
45+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:46731",
8+
"sslPort": 44332
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5183",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7164;http://localhost:5183",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
15+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
16+
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>

0 commit comments

Comments
 (0)