Refactoring

This commit is contained in:
Vitaliy 2024-06-19 01:45:06 +03:00
parent 304e7bf5f5
commit 75ca4337b9
14 changed files with 85 additions and 85 deletions

View File

@ -1,10 +1,12 @@
using Microsoft.AspNetCore.Http.HttpResults; using HackathonPreparing.ApiService.UserSlice.EfCore.Models;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
namespace HackathonPreparing.ApiService.GlobalHelpers;
public static class DbHelpersEndpointsExtensions public static class DbHelpersEndpointsExtensions
{ {
public static RouteGroupBuilder AddDbHelpersEndpoints(this WebApplication app) public static RouteGroupBuilder AddDbHelpersEndpoints(this WebApplication app)

View File

@ -1,9 +1,8 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HackathonPreparing.ApiService.GlobalHelpers;
public static class ServicesExtensions public static class ServicesExtensions
{ {

View File

@ -1,4 +1,9 @@
using HackathonPreparing.ApiService; using HackathonPreparing.ApiService.GlobalHelpers;
using HackathonPreparing.ApiService.UserSlice.EfCore;
using HackathonPreparing.ApiService.UserSlice.EfCore.Models;
using HackathonPreparing.ApiService.UserSlice.OpenApi;
using HackathonPreparing.ApiService.WeatherForecastSlice.EfCore;
using HackathonPreparing.ApiService.WeatherForecastSlice.Endpoints;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;

View File

@ -1,12 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
//Example how add Fluent Building in project
public class UserEntityTypeConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
}
}

View File

@ -1,9 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace HackathonPreparing.ApiService.User
{
public class UserController : Controller
{
}
}

View File

@ -2,6 +2,9 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace HackathonPreparing.ApiService.UserSlice.EfCore.EntityTypeConfigurations;
public class IdentityRoleEntityTypeConfiguration : IEntityTypeConfiguration<IdentityRole<int>> public class IdentityRoleEntityTypeConfiguration : IEntityTypeConfiguration<IdentityRole<int>>
{ {
public void Configure(EntityTypeBuilder<IdentityRole<int>> builder) public void Configure(EntityTypeBuilder<IdentityRole<int>> builder)

View File

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
namespace HackathonPreparing.ApiService.UserSlice.EfCore.Models;
public class User : IdentityUser<int> public class User : IdentityUser<int>
{ {
public string? AboutMe { get; set; } public string? AboutMe { get; set; }

View File

@ -1,9 +1,11 @@
using HackathonPreparing.ApiService.UserSlice.EfCore.Models;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HackathonPreparing.ApiService.UserSlice.EfCore;
public class UserContext : IdentityDbContext<User, IdentityRole<int>, int> public class UserContext : IdentityDbContext<User, IdentityRole<int>, int>
{ {
public UserContext (DbContextOptions<UserContext> options) : base(options) {} public UserContext (DbContextOptions<UserContext> options) : base(options) {}

View File

@ -1,11 +1,11 @@
using HackathonPreparing.ApiService.Migrations;
using Microsoft.AspNetCore.Identity.Data; using Microsoft.AspNetCore.Identity.Data;
using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
namespace HackathonPreparing.ApiService.UserSlice.OpenApi;
public class UserOpenApiSchemeFilter : ISchemaFilter public class UserOpenApiSchemeFilter : ISchemaFilter
{ {
public void Apply(OpenApiSchema schema, SchemaFilterContext context) public void Apply(OpenApiSchema schema, SchemaFilterContext context)
@ -14,13 +14,11 @@ public class UserOpenApiSchemeFilter : ISchemaFilter
{ {
schema.Example = new OpenApiObject() schema.Example = new OpenApiObject()
{ {
["email"] = new OpenApiString("email@gmail.com"), ["email"] = new OpenApiString("m@gmail.com"),
["password"] = new OpenApiString("PassPass123_"), ["password"] = new OpenApiString("PassPass123_"),
}; };
} }
if (context.Type == typeof(RegisterRequest)) if (context.Type == typeof(RegisterRequest))
{ {
schema.Example = new OpenApiObject() schema.Example = new OpenApiObject()

View File

@ -1,41 +0,0 @@
using Microsoft.EntityFrameworkCore;
namespace HackathonPreparing.ApiService;
public sealed class WeatherForecastContext : DbContext
{
public WeatherForecastContext(DbContextOptions<WeatherForecastContext> options)
: base(options)
{
}
public DbSet<WeatherForecast> Forecasts => Set<WeatherForecast>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<WeatherForecast>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Id).ValueGeneratedOnAdd();
// Generate data
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
index,
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
entity.HasData(forecast);
});
base.OnModelCreating(modelBuilder);
}
}

View File

@ -0,0 +1,30 @@
using HackathonPreparing.ApiService.WeatherForecastSlice.EfCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace HackathonPreparing.ApiService.WeatherForecastSlice.EfCore.EntityTypeConfigurations;
public class WeatherForecastEntityTypeConfiguration : IEntityTypeConfiguration<WeatherForecast>
{
public void Configure(EntityTypeBuilder<WeatherForecast> builder)
{
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).ValueGeneratedOnAdd();
// Generate data
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
index,
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
builder.HasData(forecast);
}
}

View File

@ -1,4 +1,5 @@
namespace HackathonPreparing.ApiService;
namespace HackathonPreparing.ApiService.WeatherForecastSlice.EfCore.Models;
public class WeatherForecast public class WeatherForecast
{ {

View File

@ -0,0 +1,22 @@
using HackathonPreparing.ApiService.WeatherForecastSlice.EfCore.Models;
using Microsoft.EntityFrameworkCore;
namespace HackathonPreparing.ApiService.WeatherForecastSlice.EfCore;
public sealed class WeatherForecastContext : DbContext
{
public WeatherForecastContext(DbContextOptions<WeatherForecastContext> options)
: base(options)
{
}
public DbSet<WeatherForecast> Forecasts => Set<WeatherForecast>();
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(typeof(Program).Assembly);
builder.HasDefaultSchema("weatherforecast");
}
}

View File

@ -1,10 +1,10 @@
using HackathonPreparing.ApiService.WeatherForecastSlice.EfCore;
using HackathonPreparing.ApiService.WeatherForecastSlice.EfCore.Models;
using HackathonPreparing.ApiService;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
public static class WeatherForecastExtensions namespace HackathonPreparing.ApiService.WeatherForecastSlice.Endpoints;
public static class WeatherForecastEndpointsExtensions
{ {
public static void AddWeatherForecastEndpoints (this WebApplication app) public static void AddWeatherForecastEndpoints (this WebApplication app)
{ {