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

Michael #2

Closed
wants to merge 18 commits into from
Closed
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
21 changes: 0 additions & 21 deletions API/API.csproj

This file was deleted.

10 changes: 4 additions & 6 deletions API/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using API.DTOs.Users;
using API.Services.Users;
using Common.DTOs.Users;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Service.Users;

namespace API.Controllers
{
Expand All @@ -20,14 +18,14 @@ public UserController(ILogger<UserController> logger
_logger = logger;
}

[HttpGet]
[HttpGet(Name = "GetUserList")]
public async Task<IActionResult> Get([FromQuery] GetUserRequest request)
{
var users = await _service.SearchAsync(request);
return Ok(users);
}

[HttpPost]
[HttpPost(Name = "AddNewUser")]
public async Task<IActionResult> Add([FromBody] AddUserRequest request)
{
var users = await _service.AddNewAsync(request);
Expand Down
4 changes: 3 additions & 1 deletion API/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public static IServiceCollection AddUnitOfWork(this IServiceCollection services)
public static IServiceCollection AddDatabase(this IServiceCollection services
, IConfiguration configuration)
{
//return services.AddDbContext<EFContext>(options =>
// options.UseSqlServer(configuration.GetConnectionString("DDDConnectionString")));
return services.AddDbContext<EFContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DDDConnectionString")));
options.UseSqlServer(configuration.GetConnectionString("DDDConnectionString"), b => b.MigrationsAssembly("API")));
}

public static IServiceCollection AddBusinessServices(this IServiceCollection services
Expand Down
42 changes: 42 additions & 0 deletions API/P1.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Compile Remove="DTOs\**" />
<Compile Remove="Extensions\**" />
<Compile Remove="Migrations\**" />
<Content Remove="DTOs\**" />
<Content Remove="Extensions\**" />
<Content Remove="Migrations\**" />
<EmbeddedResource Remove="DTOs\**" />
<EmbeddedResource Remove="Extensions\**" />
<EmbeddedResource Remove="Migrations\**" />
<None Remove="DTOs\**" />
<None Remove="Extensions\**" />
<None Remove="Migrations\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Data\P3.Data.csproj" />
<ProjectReference Include="..\Service\P2.Service.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="P6.StoryTest" />
</ItemGroup>
</Project>
107 changes: 83 additions & 24 deletions API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,85 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace API
using Microsoft.EntityFrameworkCore;
using Data.EF.Interfaces;
using Data.EF.Repositories;
using Data.EF;
using Service.Users;
using Microsoft.OpenApi.Models;
using Autofac.Extensions.DependencyInjection;
using Autofac;
using AutoMapper;
using Service.MapperProfiles;

const string AllowCors = "AllowCors";
const string CORS_ORIGINS = "CorsOrigins";

var builder = WebApplication.CreateBuilder(args);
//var builder = WebApplication.CreateBuilder(new WebApplicationOptions
//{
// Args = args,
// // Look for static files in webroot
// WebRootPath = "esafety"
//});

// allow CORS
builder.Services.AddCors(option => option.AddPolicy(
AllowCors,
policy =>
policy.WithOrigins(builder.Configuration.GetSection(CORS_ORIGINS).Get<string[]>()).AllowAnyHeader().AllowCredentials().AllowAnyMethod()
));
// Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(cbuilder => cbuilder.RegisterModule(new API.RegisterModule(builder.Configuration)));

// Moved to Autofac
// Add services to the container.
//var temp = builder.Configuration.GetConnectionString("DDDConnectionString");
//builder.Services.AddDbContext<EFContext>(options =>
// options
// // .UseLazyLoadingProxies()
// .UseSqlServer(builder.Configuration.GetConnectionString("DDDConnectionString"), b => b.MigrationsAssembly("P3.Data")));

//builder.Services
// .AddScoped<IUnitOfWork, UnitOfWork>();

//builder.Services
// .AddScoped(typeof(IAsyncRepository<>), typeof(RepositoryBase<>))
// .AddScoped<IUserRepository, UserRepository>()
// .AddScoped<IDepartmentRepository, DepartmentRepository>();

//builder.Services
// .AddScoped<UserService>();

// Add AutoMapper
builder.Services.AddAutoMapper(typeof(Service.MapperProfiles.UserProfile).Assembly);

// Add other features
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
});

builder.Services.AddAuthentication();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
}

app.UseCors(AllowCors);

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

//app.Urls.Add("http://localhost:3000");
app.Run();
public partial class Program { }
6 changes: 3 additions & 3 deletions API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:54399",
"applicationUrl": "http://localhost:3000",
"sslPort": 0
}
},
Expand Down
46 changes: 46 additions & 0 deletions API/RegisterModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Autofac;
using Microsoft.EntityFrameworkCore;
using Data.EF.Interfaces;
using Data.EF.Repositories;
using Data.EF;
using Service.Users;

namespace API
{
public class RegisterModule : Module
{
ConfigurationManager _conf;
protected override void Load(ContainerBuilder builder)
{
string dbconstr = _conf.GetConnectionString("DDDConnectionString");

builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
builder.RegisterType<DepartmentRepository>().As<IDepartmentRepository>().InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IAsyncRepository<>)).InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.Register(c => {
var options = new DbContextOptionsBuilder<EFContext>();
options.UseLoggerFactory(c.Resolve<ILoggerFactory>()).EnableSensitiveDataLogging();
options.UseSqlServer(dbconstr, b => b.MigrationsAssembly("P3.Data"));
return options.Options;
}).InstancePerLifetimeScope();
builder.RegisterType<EFContext>()
.AsSelf()
.InstancePerLifetimeScope();
builder.RegisterType<UserService>().AsSelf();
}

public RegisterModule(ConfigurationManager conf)
{
this._conf = conf;
}
}

// Other Lifetime
// .InstancePerDependency();
// .InstancePerLifetimeScope(); = DI scoped
// .SingleInstance();
//builder.RegisterAssemblyModules(typeof(ServerSettings).Assembly);

}
57 changes: 0 additions & 57 deletions API/Startup.cs

This file was deleted.

6 changes: 5 additions & 1 deletion API/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DDDConnectionString": "Server=(localdb)\\ProjectsV13;Database=DDDSample;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
6 changes: 2 additions & 4 deletions API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DDDConnectionString": "Server=(localdb)\\mssqllocaldb;Database=DDDSample;Trusted_Connection=True;MultipleActiveResultSets=true"
}
"CorsOrigins": [ "http://localhost:3000", "http://localhost:3001" ],
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using MediatR;
using System;

namespace Domain.Base
namespace Business.Base
{
public abstract class BaseDomainEvent : INotification
{
Expand Down
7 changes: 7 additions & 0 deletions Business/Base/BaseDomainService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

namespace Business.Base
{
class BaseDomainService
{
}
}
Loading