Merge remote-tracking branch 'origin/master' into feature/update-current-user

# Conflicts:
#	src/Infrastructure/Data/ApplicationDbContextInitializer.cs
This commit is contained in:
Dominic Villemure
2024-07-04 20:48:35 -04:00
28 changed files with 1037 additions and 41 deletions

View File

@@ -1,6 +1,6 @@
using System.Security.Claims;
namespace Hutopy.Web.Messages;
namespace Hutopy.Web.Common;
public class Shared(string claimName) : Exception;

View File

@@ -0,0 +1,13 @@
namespace Hutopy.Web.Contents.Data;
public class Content
{
public Guid Id { get; init; }
public Guid CreatedBy { get; init; }
public DateTime CreatedAt { get; }
public string? Title { get; init; } = null!;
public string? Description { get; init; } = null!;
public string? Uri { get; init; } = null!;
}

View File

@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Contents.Data;
public class ContentDbContext(
DbContextOptions<ContentDbContext> options)
: DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Content>()
.Property(c => c.CreatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
}
public DbSet<Content> Contents { get; set; }
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Hutopy.Web.Contents.Data;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Contents.Handlers;
public class GetContents(
ContentDbContext context)
: EndpointWithoutRequest<Content>
{
public override void Configure()
{
Tags("Contents");
Get("/api/contents/{ContentId:guid}");
AllowAnonymous();
}
public override async Task HandleAsync(
CancellationToken ct)
{
var contentId = Route<Guid>("ContentId");
var comments = await context
.Contents
.Where(c => c.Id == contentId)
.ToListAsync(cancellationToken: ct);
await SendAsync(comments.First(), cancellation: ct);
}
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Hutopy.Web.Contents.Data;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Contents.Handlers;
public class GetContentsByUser(
ContentDbContext context)
: EndpointWithoutRequest<List<Content>>
{
public override void Configure()
{
Tags("Contents");
Get("/api/contents/user/{UserId:guid}");
AllowAnonymous();
}
public override async Task HandleAsync(
CancellationToken ct)
{
var userId = Route<Guid>("UserId");
var posts = await context
.Contents
.Where(c => c.CreatedBy == userId)
.ToListAsync(cancellationToken: ct);
await SendAsync(posts, cancellation: ct);
}
}

View File

@@ -0,0 +1,39 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Contents.Data;
namespace Hutopy.Web.Contents.Handlers;
public record struct PostContentRequest(
string? Title,
string? Description,
string? Uri);
public class PostMessage(
ContentDbContext context)
: Endpoint<PostContentRequest>
{
public override void Configure()
{
// TODO: Find how to specify the name we see in Swagger
Tags("Contents");
Post("/api/contents");
}
public override async Task HandleAsync(
PostContentRequest req,
CancellationToken ct)
{
await context.Contents.AddAsync(
new Content
{
CreatedBy = User.GetUserId(),
Title = req.Title,
Description = req.Description,
Uri = req.Uri
},
ct);
await context.SaveChangesAsync(ct);
}
}

View File

@@ -0,0 +1,58 @@
// <auto-generated />
using System;
using Hutopy.Web.Contents.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Hutopy.Web.Contents.Migrations
{
[DbContext(typeof(ContentDbContext))]
[Migration("20240702034957_Initial")]
partial class Initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Hutopy.Web.Contents.Data.Content", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid>("CreatedBy")
.HasColumnType("uniqueidentifier");
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.HasColumnType("nvarchar(max)");
b.Property<string>("Uri")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Contents");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Hutopy.Web.Contents.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Contents",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
CreatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
Uri = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Contents", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Contents");
}
}
}

View File

@@ -0,0 +1,55 @@
// <auto-generated />
using System;
using Hutopy.Web.Contents.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Hutopy.Web.Contents.Migrations
{
[DbContext(typeof(ContentDbContext))]
partial class ContentDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Hutopy.Web.Contents.Data.Content", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid>("CreatedBy")
.HasColumnType("uniqueidentifier");
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.HasColumnType("nvarchar(max)");
b.Property<string>("Uri")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Contents");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,29 @@
using FastEndpoints;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Common.Models;
namespace Hutopy.Web.Creators.Handlers;
public class GetCreatorByAlias(
IIdentityService identityService)
: EndpointWithoutRequest<UserModel?>
{
public override void Configure()
{
Tags("Creators");
Get("/api/creators/@{CreatorAlias}");
AllowAnonymous();
}
public override async Task HandleAsync(
CancellationToken ct)
{
var creatorAlias = Route<string>("CreatorAlias");
ArgumentException.ThrowIfNullOrEmpty(creatorAlias);
var user = await identityService.FindUserByCreatorAliasAsync(creatorAlias, ct);
await SendAsync(user, cancellation: ct);
}
}

View File

@@ -5,9 +5,6 @@ using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Messages.Handlers;
public record GetMessagesByUserRequest(
[FromRoute] Guid UserId);
public class GetMessagesByUser(
MessagingDbContext context)
: EndpointWithoutRequest<List<Message>>
@@ -15,7 +12,7 @@ public class GetMessagesByUser(
public override void Configure()
{
Tags("Messages");
Get("/api/messages/by-user/{UserId:guid}");
Get("/api/messages/user/{UserId:guid}");
}
public override async Task HandleAsync(

View File

@@ -1,4 +1,5 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Messages.Data;
namespace Hutopy.Web.Messages.Handlers;

View File

@@ -1,4 +1,5 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Messages.Data;
namespace Hutopy.Web.Messages.Handlers;

View File

@@ -1,7 +1,6 @@
@using Hutopy.Infrastructure.Identity
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@{
string? returnUrl = null;

View File

@@ -4,6 +4,7 @@ using Hutopy.Application;
using Hutopy.Infrastructure;
using Hutopy.Infrastructure.Data;
using Hutopy.Web;
using Hutopy.Web.Contents.Data;
using Hutopy.Web.Messages.Data;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
@@ -21,24 +22,24 @@ if (!builder.Environment.IsDevelopment())
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
options.AddPolicy("AllowAll", policy =>
{
builder.AllowAnyOrigin()
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
options.AddPolicy("AllowHutopyUi", builder =>
options.AddPolicy("AllowHutopyUi", policy =>
{
builder.WithOrigins("https://zealous-bay-08204590f.5.azurestaticapps.net")
policy.WithOrigins("https://zealous-bay-08204590f.5.azurestaticapps.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
options.AddPolicy("AllowHutopyUiPreview", builder =>
options.AddPolicy("AllowHutopyUiPreview", policy =>
{
builder.WithOrigins("https://zealous-bay-08204590f-preview.eastus2.5.azurestaticapps.net")
policy.WithOrigins("https://zealous-bay-08204590f-preview.eastus2.5.azurestaticapps.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
@@ -83,6 +84,11 @@ builder.Services.AddDbContext<MessagingDbContext>((_, options) =>
options.UseSqlServer(builder.Configuration.GetConnectionString("CommentStore"));
});
builder.Services.AddDbContext<ContentDbContext>((_, options) =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("ContentStore"));
});
var app = builder.Build();
app.UseForwardedHeaders(

View File

@@ -30,8 +30,4 @@
<PackageReference Include="FluentValidation.AspNetCore" />
</ItemGroup>
<ItemGroup>
<Folder Include="Messages\Migrations\" />
</ItemGroup>
</Project>

View File

@@ -9,7 +9,8 @@
},
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True",
"CommentStore": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True"
"CommentStore": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True",
"ContentStore": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True"
},
"Authentication": {
"Jwt": {