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

feat: Update to .NET 8 and C# 12 #43

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Chrono.Tests/Chrono.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<PackageId>Chrono.Tests</PackageId>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
<PackageReference Include="Microsoft.Playwright.NUnit" Version="1.38.0"/>
<PackageReference Include="nunit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Microsoft.Playwright.NUnit" Version="1.40.0"/>
<PackageReference Include="nunit" Version="4.0.1"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.10.0"/>
<PackageReference Include="FluentAssertions" Version="6.12.0"/>
<PackageReference Include="TngTech.ArchUnitNET" Version="0.10.6"/>
<PackageReference Include="TngTech.ArchUnitNET.NUnit" Version="0.10.6"/>
</ItemGroup>
Expand Down
14 changes: 5 additions & 9 deletions src/Chrono.Tests/E2E/00_E2ETestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

namespace Chrono.Tests.E2E;

[Parallelizable(ParallelScope.None)] [Category("E2E")]
[Parallelizable(ParallelScope.None)]
[Category("E2E")]
public partial class E2ETests : PlaywrightTest
{
private static readonly string[] PlaywrightArgs = { "install" };
private IBrowser _browser;
private IConfiguration _config;
private IPage _page;
Expand All @@ -28,10 +30,7 @@ public async Task Init()
private async Task InitPlaywright()
{
// Ensure the required web driver is installed.
Program.Main(new[]
{
"install"
});
Program.Main(PlaywrightArgs);

await PlaywrightSetup();

Expand All @@ -52,9 +51,6 @@ private async Task Login_User()

await _page.GetByLabel("Email address").FillAsync(_config["TestUser:Username"]!);
await _page.GetByLabel("Password").FillAsync(_config["TestUser:Password"]!);
await _page.GetByRole(AriaRole.Button, new PageGetByRoleOptions
{
Name = "Continue"
}).ClickAsync();
await _page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Continue" }).ClickAsync();
}
}
17 changes: 9 additions & 8 deletions src/Chrono/Chrono.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
Expand All @@ -13,17 +13,18 @@
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Chrono</RootNamespace>
<AssemblyName>Chrono</AssemblyName>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.6.0"/>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0"/>
<PackageReference Include="IdentityModel.AspNetCore" Version="4.3.0"/>
<PackageReference Include="MediatR" Version="12.1.1"/>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="7.0.9"/>
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="7.0.9"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.9"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
<PackageReference Include="MediatR" Version="12.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.0"/>
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
14 changes: 7 additions & 7 deletions src/Chrono/ClientApp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Chrono/ClientApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"prettier": "3.0.3",
"react-router-dom": "^6.14.2",
"typescript": "^5.2.2",
"vite": "^4.4.5",
"vite": "^4.4.12",
"vite-plugin-mkcert": "^1.16.0"
}
}
17 changes: 6 additions & 11 deletions src/Chrono/ConfigureSecurity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Security.Claims;
using Chrono.Shared.Interfaces;
using Chrono.Entities;
using Chrono.Shared.Interfaces;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
Expand All @@ -26,7 +26,7 @@ public static void AddWebUiSecurityServices(this IServiceCollection services, IC
//options.Cookie.SameSite = SameSiteMode.Strict;

options.Cookie.Name = configuration["IdentityProvider:CookieName"];
options.Events.OnSigningOut = async e => await e.HttpContext.RevokeUserRefreshTokenAsync();
options.Events.OnSigningOut = e => e.HttpContext.RevokeUserRefreshTokenAsync();
})
.AddOpenIdConnect(options =>
{
Expand Down Expand Up @@ -75,10 +75,7 @@ public static void AddWebUiSecurityServices(this IServiceCollection services, IC
var user = db.Users.FirstOrDefault(x => x.UserId == userId);
if (user == null)
{
db.Users.Add(new User
{
UserId = userId, Name = userName
});
db.Users.Add(new User { UserId = userId, Name = userName });
}
else
{
Expand All @@ -93,11 +90,9 @@ public static void AddWebUiSecurityServices(this IServiceCollection services, IC
});

// Ensures endpoints are secured by default.
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
services.AddAuthorizationBuilder()
.SetFallbackPolicy(new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
.Build());
}
}
1 change: 0 additions & 1 deletion src/Chrono/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static void AddWebUiServices(this IServiceCollection services, IConfigura

services.AddHttpContextAccessor();
services.AddScoped<ICurrentUserService, CurrentUserService>();
services.AddScoped<TextService>();

services.AddControllersWithViews();
services.AddSwaggerGen(options => options.CustomSchemaIds(type => type.FullName));
Expand Down
26 changes: 9 additions & 17 deletions src/Chrono/Features/Categories/CreateCategory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Chrono.Shared.Api;
using Chrono.Entities;
using Chrono.Shared.Api;
using Chrono.Shared.Interfaces;
using Chrono.Entities;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -9,30 +9,22 @@ namespace Chrono.Features.Categories;

public record CreateCategory(string Name) : IRequest<int>;

public class CreateCategoryHandler : IRequestHandler<CreateCategory, int>
public class CreateCategoryHandler(IApplicationDbContext context) : IRequestHandler<CreateCategory, int>
{
private readonly IApplicationDbContext _context;

public CreateCategoryHandler(IApplicationDbContext context)
{
_context = context;
}

public async Task<int> Handle(CreateCategory request, CancellationToken cancellationToken)
{
var entity = new Category
{
Name = request.Name
};
_context.Categories.Add(entity);
var entity = new Category { Name = request.Name };
context.Categories.Add(entity);

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

return entity.Id;
}
}

[Authorize] [Route("api/categories")] [Tags("Categories")]
[Authorize]
[Route("api/categories")]
[Tags("Categories")]
public class CreateCategoryController : ApiControllerBase
{
[HttpPost]
Expand Down
26 changes: 10 additions & 16 deletions src/Chrono/Features/Categories/DeleteCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ namespace Chrono.Features.Categories;

public record DeleteCategory(int Id) : IRequest;

public class DeleteCategoryHandler : IRequestHandler<DeleteCategory>
public class DeleteCategoryHandler(IApplicationDbContext context, ICurrentUserService currentUserService)
: IRequestHandler<DeleteCategory>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUserService;

public DeleteCategoryHandler(IApplicationDbContext context, ICurrentUserService currentUserService)
{
_context = context;
_currentUserService = currentUserService;
}

public async Task Handle(DeleteCategory request, CancellationToken cancellationToken)
{
var entity = await _context.Categories
var entity = await context.Categories
.Include(x => x.Tasks)
.SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

Expand All @@ -34,20 +26,22 @@ public async Task Handle(DeleteCategory request, CancellationToken cancellationT
throw new NotFoundException($"Category \"{request.Id}\" not found.");
}

if (!entity.IsPermitted(_currentUserService.UserId))
if (!entity.IsPermitted(currentUserService.UserId))
{
throw new ForbiddenAccessException();
}

// Ensures only the TaskCategories are getting deleted and not the actual tasks too.
_context.TaskCategories.RemoveRange(entity.Tasks);
_context.Categories.Remove(entity);
context.TaskCategories.RemoveRange(entity.Tasks);
context.Categories.Remove(entity);

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);
}
}

[Authorize] [Route("api/categories")] [Tags("Categories")]
[Authorize]
[Route("api/categories")]
[Tags("Categories")]
public class DeleteCategoryController : ApiControllerBase
{
[HttpDelete("{id:int}")]
Expand Down
20 changes: 7 additions & 13 deletions src/Chrono/Features/Categories/GetCategories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,25 @@ namespace Chrono.Features.Categories;

public record GetCategories : IRequest<CategoryDto[]>;

public class GetCategoriesHandler : IRequestHandler<GetCategories, CategoryDto[]>
public class GetCategoriesHandler(IApplicationDbContext context, ICurrentUserService currentUserService)
: IRequestHandler<GetCategories, CategoryDto[]>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUserService;

public GetCategoriesHandler(IApplicationDbContext context, ICurrentUserService currentUserService)
{
_context = context;
_currentUserService = currentUserService;
}

public Task<CategoryDto[]> Handle(GetCategories request, CancellationToken cancellationToken)
{
var result = _context.Categories
var result = context.Categories
.OrderBy(x => x.Name)
.AsEnumerable()
.Where(x => x.IsPermitted(_currentUserService.UserId))
.Where(x => x.IsPermitted(currentUserService.UserId))
.Select(CategoryDto.FromEntity)
.ToArray();

return Task.FromResult(result);
}
}

[Authorize] [Route("api/categories")] [Tags("Categories")]
[Authorize]
[Route("api/categories")]
[Tags("Categories")]
public class GetCategoriesController : ApiControllerBase
{
[HttpGet]
Expand Down
31 changes: 10 additions & 21 deletions src/Chrono/Features/Notes/CreateNote.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Chrono.Shared.Api;
using Chrono.Entities;
using Chrono.Shared.Api;
using Chrono.Shared.Interfaces;
using Chrono.Entities;
using FluentValidation;
using MediatR;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -23,40 +23,29 @@ public CreateNoteValidator()
}
}

public class CreateNoteHandler : IRequestHandler<CreateNote, int>
public class CreateNoteHandler(IApplicationDbContext context) : IRequestHandler<CreateNote, int>
{
private readonly IApplicationDbContext _context;

public CreateNoteHandler(IApplicationDbContext context)
{
_context = context;
}

public async Task<int> Handle(CreateNote request, CancellationToken cancellationToken)
{
var entity = new Note
{
Title = request.Title, Text = request.Text
};
_context.Notes.Add(entity);
var entity = new Note { Title = request.Title, Text = request.Text };
context.Notes.Add(entity);

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

return entity.Id;
}
}

[Authorize] [Route("api/notes")] [Tags("Notes")]
[Authorize]
[Route("api/notes")]
[Tags("Notes")]
public class CreateNoteController : ApiControllerBase
{
[HttpPost]
[ProducesDefaultResponseType]
public async Task<IActionResult> Create(CreateNote command)
{
var result = await Mediator.Send(command);
return CreatedAtRoute("GetNote", new
{
id = result
}, JSendResponseBuilder.Success(result));
return CreatedAtRoute("GetNote", new { id = result }, JSendResponseBuilder.Success(result));
}
}
Loading