Skip to content

Commit

Permalink
#4 - Initialize EF
Browse files Browse the repository at this point in the history
  • Loading branch information
itbeard committed Jan 27, 2021
1 parent 01739f7 commit 499577a
Show file tree
Hide file tree
Showing 17 changed files with 555 additions and 64 deletions.
42 changes: 42 additions & 0 deletions Pds/Pds.Api/AppStart/AuthenticationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Pds.Api.AppStart
{
public static class AuthenticationExtensions
{
private const string CorsPolicy = "PdsCorsPolicy";

public static void AddAuth0Authentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = configuration["Auth0:Authority"];
options.Audience = configuration["Auth0:ApiIdentifier"];
});
}

public static void AddPdsCorsPolicy(this IServiceCollection services, IConfiguration configuration)
{
services.AddCors(options =>
{
options.AddPolicy(CorsPolicy,
builder => builder.WithOrigins(configuration.GetSection("AllowedOrigins").Get<List<string>>().ToArray())
.AllowAnyMethod()
.AllowAnyHeader());
});
}

public static void UsePdsCorsPolicy(this IApplicationBuilder app)
{
app.UseCors("PdsUiPolicy");
}
}
}
16 changes: 16 additions & 0 deletions Pds/Pds.Api/AppStart/DatabaseContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pds.Data;

namespace Pds.Api.AppStart
{
public static class DatabaseContextExtensions
{
public static void AddSqlContext(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ApplicationDbContext>(o => o.UseSqlServer(connectionString));
}
}
}
60 changes: 60 additions & 0 deletions Pds/Pds.Api/AppStart/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Pds.Data;

namespace Pds.Api.AppStart
{
public static class SwaggerExtensions
{
public static void AddCustomSwagger(this IServiceCollection services)
{
services.AddSwaggerGen(swagger =>
{
swagger.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Pds.Api",
Description = "People Discovery System API"
});
swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
swagger.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
}

public static void UseCustomSwaggerUI(this IApplicationBuilder app)
{
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Pds.Api v1"));
}
}
}
9 changes: 9 additions & 0 deletions Pds/Pds.Api/Pds.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Pds.Data\Pds.Data.csproj" />
</ItemGroup>

</Project>
72 changes: 8 additions & 64 deletions Pds/Pds.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Pds.Api.AppStart;

namespace Pds.Api
{
Expand All @@ -19,78 +15,26 @@ public Startup(IConfiguration configuration)
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = Configuration["Auth0:Authority"];
options.Audience = Configuration["Auth0:ApiIdentifier"];
});
services.AddCors(options =>
{
options.AddPolicy("PdsUiPolicy",
builder => builder.WithOrigins(Configuration.GetSection("AllowedOrigins").Get<List<string>>().ToArray())
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddAuth0Authentication(Configuration);
services.AddPdsCorsPolicy(Configuration);
services.AddControllers();
services.AddSwaggerGen(swagger =>
{
//This is to generate the Default UI of Swagger Documentation
swagger.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Pds.Api",
Description = "People Discovery System API"
});
swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
swagger.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
services.AddCustomSwagger();
services.AddSqlContext(Configuration);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Pds.Api v1"));
app.UseCustomSwaggerUI();
}

app.UseCors("PdsUiPolicy");
app.UsePdsCorsPolicy();
app.UseHttpsRedirection();

app.UseRouting();
Expand Down
3 changes: 3 additions & 0 deletions Pds/Pds.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"Auth0": {
"Authority": "https://itbeard.eu.auth0.com",
"ApiIdentifier": "https://psd-api.itbeard.com"
},
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Initial Catalog=pds;User ID=admin;Password=123qweQWE;MultipleActiveResultSets=False;"
}
}
24 changes: 24 additions & 0 deletions Pds/Pds.Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Pds.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace Pds.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){}

public DbSet<Person> Persons { get; set; }
public DbSet<Resource> Resources { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
UpdateStructure(builder);
base.OnModelCreating(builder);
}

private void UpdateStructure(ModelBuilder modelBuilder)
{
// Place for specific fluent properties
}
}
}
9 changes: 9 additions & 0 deletions Pds/Pds.Data/Entities/EntityBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Pds.Data.Entities
{
public abstract class EntityBase
{
public Guid ID { get; set; }
}
}
39 changes: 39 additions & 0 deletions Pds/Pds.Data/Entities/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Pds.Data.Enums;

namespace Pds.Data.Entities
{
public class Person : EntityBase
{
[Required]
[Column(TypeName = "varchar(300)")]
public string FirstName { get; set; }

[Required]
[Column(TypeName = "varchar(300)")]
public string LastName { get; set; }

[Required]
[Column(TypeName = "varchar(300)")]
public string ThirdName { get; set; }

public string Info { get; set; }

public int Rate { get; set; }

public PersonStatus Status { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime? UpdatedAt { get; set; }

public DateTime? ArchivedAt { get; set; }

public DateTime? UnarchivedAt { get; set; }

public ICollection<Resource> Resources { get; set; }
}
}
17 changes: 17 additions & 0 deletions Pds/Pds.Data/Entities/Resource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Pds.Data.Entities
{
public class Resource : EntityBase
{
public string Name { get; set; }

public string Url { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime? UpdatedAt { get; set; }

public Person Person { get; set; }
}
}
8 changes: 8 additions & 0 deletions Pds/Pds.Data/Enums/PersonStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Pds.Data.Enums
{
public enum PersonStatus
{
Archived = 0,
Active = 1
}
}
Loading

0 comments on commit 499577a

Please sign in to comment.