-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Added support for minimal APIs (#43)
- Loading branch information
1 parent
c127894
commit c753264
Showing
27 changed files
with
239 additions
and
13 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class ApplicationDbContext : DbContext | ||
{ | ||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } | ||
|
||
public DbSet<User> Users => Set<User>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
public record UserDto | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public string Firstname { get; set; } = string.Empty; | ||
|
||
public string Lastname { get; set; } = string.Empty; | ||
|
||
public string Email { get; set; } = string.Empty; | ||
|
||
public string AvatarUrl { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("displayName")] | ||
public string UserName { get; set; } = string.Empty; | ||
|
||
public string Gender { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using Bogus.DataSets; | ||
|
||
public record User | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
[Column(TypeName = "varchar(128)")] | ||
public string Firstname { get; set; } = string.Empty; | ||
|
||
[Column(TypeName = "varchar(128)")] | ||
public string Lastname { get; set; } = string.Empty; | ||
|
||
[Column(TypeName = "varchar(320)")] | ||
public string Email { get; set; } = string.Empty; | ||
|
||
[Column(TypeName = "varchar(1024)")] | ||
public string AvatarUrl { get; set; } = string.Empty; | ||
public bool IsDeleted { get; set; } | ||
|
||
[Column(TypeName = "varchar(64)")] | ||
public string UserName { get; set; } = string.Empty; | ||
|
||
[Column("PersonSex", TypeName = "varchar(32)")] | ||
public string Gender { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public interface IUserService | ||
{ | ||
IQueryable<UserDto> GetAllUsers(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using AutoMapper; | ||
|
||
public class AutoMapperProfile : Profile | ||
{ | ||
public AutoMapperProfile() | ||
{ | ||
CreateMap<User, UserDto>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Linq.Dynamic.Core; | ||
using System.Reflection; | ||
using Bogus; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
|
||
builder.Services.AddSwaggerGen(options => | ||
{ | ||
options.OperationFilter<GoatQueryOpenAPIFilter>(); | ||
}); | ||
|
||
builder.Services.AddDbContext<ApplicationDbContext>(options => | ||
{ | ||
options.UseInMemoryDatabase("minimal"); | ||
}); | ||
|
||
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly()); | ||
|
||
builder.Services.AddScoped<IUserService, UserService>(); | ||
|
||
builder.Services.AddSingleton<ISearchBinder<UserDto>, UserDtoSearchBinder>(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
|
||
using (var scope = app.Services.CreateScope()) | ||
{ | ||
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); | ||
await context.Database.EnsureCreatedAsync(); | ||
|
||
// Seed data | ||
if (!context.Users.Any()) | ||
{ | ||
var users = new Faker<User>() | ||
.RuleFor(x => x.Firstname, f => f.Person.FirstName) | ||
.RuleFor(x => x.Lastname, f => f.Person.LastName) | ||
.RuleFor(x => x.Email, f => f.Person.Email) | ||
.RuleFor(x => x.AvatarUrl, f => f.Internet.Avatar()) | ||
.RuleFor(x => x.UserName, f => f.Person.UserName) | ||
.RuleFor(x => x.Gender, f => f.Person.Gender.ToString()) | ||
.RuleFor(x => x.IsDeleted, f => f.Random.Bool()); | ||
|
||
context.Users.AddRange(users.Generate(1_000)); | ||
context.SaveChanges(); | ||
|
||
Console.WriteLine("Seeded 1,000 fake users!"); | ||
} | ||
} | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapGet("/users", (ApplicationDbContext db, [FromServices] IUserService userService, [AsParameters] Query query) => | ||
{ | ||
var (users, count) = userService.GetAllUsers().Apply(query); | ||
|
||
return TypedResults.Ok(new PagedResponse<object>(users.ToDynamicList(), count)); | ||
}); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:42100", | ||
"sslPort": 44374 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5240", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7077;http://localhost:5240", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Linq.Expressions; | ||
|
||
public class UserDtoSearchBinder : ISearchBinder<UserDto> | ||
{ | ||
public Expression<Func<UserDto, bool>> Bind(string searchTerm) | ||
{ | ||
Expression<Func<UserDto, bool>> exp = x => x.Firstname.Contains(searchTerm, StringComparison.OrdinalIgnoreCase) || x.Lastname.Contains(searchTerm, StringComparison.OrdinalIgnoreCase); | ||
|
||
return exp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class UserService : IUserService | ||
{ | ||
private readonly ApplicationDbContext _context; | ||
private readonly IMapper _mapper; | ||
|
||
public UserService(ApplicationDbContext context, IMapper mapper) | ||
{ | ||
_context = context; | ||
_mapper = mapper; | ||
} | ||
|
||
public IQueryable<UserDto> GetAllUsers() | ||
{ | ||
return _context.Users.AsNoTracking() | ||
.Where(x => !x.IsDeleted) | ||
.ProjectTo<UserDto>(_mapper.ConfigurationProvider); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /> | ||
<PackageReference Include="Bogus" Version="35.3.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
<ProjectReference Include="../../src/goatquery-dotnet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
public record Query | ||
{ | ||
public int Top { get; set; } | ||
public int Skip { get; set; } | ||
public bool Count { get; set; } | ||
public string OrderBy { get; set; } = string.Empty; | ||
public string Select { get; set; } = string.Empty; | ||
public string Search { get; set; } = string.Empty; | ||
public string Filter { get; set; } = string.Empty; | ||
public int? Top { get; set; } | ||
public int? Skip { get; set; } | ||
public bool? Count { get; set; } | ||
public string? OrderBy { get; set; } = string.Empty; | ||
public string? Select { get; set; } = string.Empty; | ||
public string? Search { get; set; } = string.Empty; | ||
public string? Filter { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters