Skip to content

Commit 3a055fe

Browse files
committed
#35050 Sample copied from caching
1 parent 2ad8d96 commit 3a055fe

46 files changed

Lines changed: 1314 additions & 0 deletions

Some content is hidden

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

src/aspire/logging-metalama/TodoList/.idea/.idea.TodoList/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aspire/logging-metalama/TodoList/.idea/.idea.TodoList/.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aspire/logging-metalama/TodoList/.idea/.idea.TodoList/.idea/indexLayout.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aspire/logging-metalama/TodoList/.idea/.idea.TodoList/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aspire/logging-metalama/TodoList/TodoList.Api/Migrations/20240617131158_InitialCreate.Designer.cs

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace TodoList.ApiService.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: "Todos",
15+
columns: table => new
16+
{
17+
Id = table.Column<int>(type: "INTEGER", nullable: false)
18+
.Annotation("Sqlite:Autoincrement", true),
19+
Title = table.Column<string>(type: "TEXT", nullable: false)
20+
},
21+
constraints: table =>
22+
{
23+
table.PrimaryKey("PK_Todos", x => x.Id);
24+
});
25+
}
26+
27+
/// <inheritdoc />
28+
protected override void Down(MigrationBuilder migrationBuilder)
29+
{
30+
migrationBuilder.DropTable(
31+
name: "Todos");
32+
}
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <auto-generated />
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
5+
using TodoList.ApiService.Model;
6+
7+
#nullable disable
8+
9+
namespace TodoList.ApiService.Migrations
10+
{
11+
[DbContext(typeof(ApplicationDbContext))]
12+
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder.HasAnnotation("ProductVersion", "8.0.6");
18+
19+
modelBuilder.Entity("Todo", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.Property<string>("Title")
26+
.IsRequired()
27+
.HasColumnType("TEXT");
28+
29+
b.HasKey("Id");
30+
31+
b.ToTable("Todos");
32+
});
33+
#pragma warning restore 612, 618
34+
}
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace TodoList.ApiService.Model;
4+
5+
public class ApplicationDbContext : DbContext
6+
{
7+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
8+
: base(options) { }
9+
10+
public DbSet<Todo> Todos { get; set; } = null!;
11+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Metalama.Patterns.Caching.Backends.Redis;
2+
using Metalama.Patterns.Caching.Building;
3+
using StackExchange.Redis;
4+
using System.Globalization;
5+
using TodoList.ApiService.Model;
6+
using TodoList.ApiService.Services;
7+
8+
var builder = WebApplication.CreateBuilder(args);
9+
10+
// Add service defaults & Aspire components.
11+
builder.AddServiceDefaults();
12+
13+
// [<snippet AddRedis>]
14+
builder.AddRedisClient( "cache" );
15+
// [<endsnippet AddRedis>]
16+
17+
// [<snippet AddMetalamaCaching>]
18+
builder.Services.AddMetalamaCaching(
19+
caching => caching.WithBackend( backend => backend.Redis( )) );
20+
// [<endsnippet AddMetalamaCaching>]
21+
22+
// Add services to the container.
23+
builder.AddSqlServerDbContext<ApplicationDbContext>( "database" );
24+
builder.Services.AddScoped<TodoService>();
25+
26+
var app = builder.Build();
27+
28+
app.MapDefaultEndpoints();
29+
30+
// Configure the HTTP request pipeline.
31+
app.UseHttpsRedirection();
32+
33+
// Ensure the schema is created.
34+
using ( var scope = app.Services.CreateScope() )
35+
{
36+
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
37+
await db.Database.EnsureCreatedAsync();
38+
}
39+
40+
app.MapGet( "/todo", ( TodoService todos, CancellationToken cancellationToken )
41+
=> todos.GetTodosAsync( cancellationToken ) );
42+
43+
app.MapGet( "/todo/{id}", ( TodoService todos, int id, CancellationToken cancellationToken )
44+
=> todos.GetTodoAsync( id, cancellationToken ) );
45+
46+
app.MapPost( "/todo", async ( TodoService todos, Todo todo, CancellationToken cancellationToken ) =>
47+
{
48+
var newTodo = await todos.AddTodoAsync( todo, cancellationToken );
49+
return Results.Created( $"/todo/{newTodo.Id}", newTodo );
50+
} );
51+
52+
app.MapPut( "/todo/{id}", async ( TodoService todos, int id, Todo todo, CancellationToken cancellationToken ) =>
53+
{
54+
if ( id != todo.Id )
55+
{
56+
return Results.BadRequest( "The ID in the URL does not match the ID in the request body." );
57+
}
58+
59+
if ( await todos.UpdateTodoAsync( todo, cancellationToken ) )
60+
{
61+
return Results.NoContent();
62+
63+
}
64+
else
65+
{
66+
return Results.NotFound();
67+
}
68+
} );
69+
70+
app.MapDelete( "/todo/{id}", async ( TodoService todos, int id, CancellationToken cancellationToken ) =>
71+
{
72+
if ( await todos.DeleteTodoAsync( id, cancellationToken ) )
73+
{
74+
return Results.NoContent();
75+
76+
}
77+
else
78+
{
79+
return Results.NotFound();
80+
}
81+
} );
82+
83+
app.Run();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:2741",
8+
"sslPort": 44375
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "weatherforecast",
17+
"applicationUrl": "http://localhost:5251",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"https": {
23+
"commandName": "Project",
24+
"dotnetRunMessages": true,
25+
"launchBrowser": true,
26+
"launchUrl": "weatherforecast",
27+
"applicationUrl": "https://localhost:7212;http://localhost:5251",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
},
32+
"IIS Express": {
33+
"commandName": "IISExpress",
34+
"launchBrowser": true,
35+
"launchUrl": "weatherforecast",
36+
"environmentVariables": {
37+
"ASPNETCORE_ENVIRONMENT": "Development"
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)