Skip to content

Commit

Permalink
Merge pull request #58 from PauloFDO/initial-setup-fix-tweaks
Browse files Browse the repository at this point in the history
Initial setup fix and tweaks
  • Loading branch information
JBeni authored Oct 5, 2023
2 parents 2c6aea9 + 95d55d8 commit a220f72
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
16 changes: 13 additions & 3 deletions BlazorShop.Infrastructure/Persistence/ApplicationDbContextSeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public static async Task SeedAdminUserAsync(UserManager<User> userManager, RoleM
{
var admin = new User
{
UserName = seedData.FirstName + "@" + seedData.LastName,
UserName = seedData.Username,
Email = seedData.Email,
FirstName = seedData.FirstName,
LastName = seedData.LastName,
IsActive = true,
SecurityStamp = Guid.NewGuid().ToString(),
};

var adminRole = roleManager.Roles.Where(x => x.Name == seedData.RoleName).FirstOrDefault();
if (adminRole == null)
{
Expand All @@ -74,7 +76,14 @@ public static async Task SeedAdminUserAsync(UserManager<User> userManager, RoleM

if (userManager.Users.All(u => u.Email != admin.Email))
{
await userManager.CreateAsync(admin, seedData.Password);
var result = await userManager.CreateAsync(admin, seedData.Password);

if (result.Errors.Any())
{
// likely password requirement errors
throw new Exception("Errors creating admin: " + string.Join(",", result.Errors.Select(x => x.Description)));
}

await userManager.AddToRoleAsync(admin, adminRole.Name);
}
}
Expand All @@ -92,7 +101,8 @@ public static async Task SeedClothesDataAsync(ApplicationDbContext context)
{
Name = "Jeans 1",
Description = "asdsad sdasd sad asd dsa",
Price = new decimal(344.00), Amount = 12,
Price = new decimal(344.00),
Amount = 12,
ImageName = "buy-3",
ImagePath = "buy-3.jpg",
IsActive = true,
Expand Down
5 changes: 5 additions & 0 deletions BlazorShop.Infrastructure/Utils/AdminSeedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace BlazorShop.Infrastructure.Utils
/// </summary>
public class AdminSeedModel
{
/// <summary>
/// Gets or Sets the username.
/// </summary>
public string? Username { get; set; }

/// <summary>
/// Gets or Sets the firstname.
/// </summary>
Expand Down
16 changes: 8 additions & 8 deletions BlazorShop.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
options.AddPolicy(corsPolicy, builder =>
{
builder.WithOrigins(allowedOrigins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});

Expand All @@ -41,10 +41,10 @@

// Add JWT TOKEN Settings
builder.Services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Audience = builder.Configuration["JwtToken:Audience"];
Expand Down Expand Up @@ -188,4 +188,4 @@
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
}
25 changes: 13 additions & 12 deletions BlazorShop.WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@
"RunSeedingOnStartup": "false"

//"RolesSeedModel": {
// "AdminRoleName": "",
// "AdmintRoleNormalizedName": "",
// "UserRoleName": "",
// "UserRoleNormalizedName": "",
// "DefaultRoleName": "",
// "DefaultRoleNormalizedName": ""
// "AdminRoleName": "",
// "AdminRoleNormalizedName": "",
// "UserRoleName": "",
// "UserRoleNormalizedName": "",
// "DefaultRoleName": "",
// "DefaultRoleNormalizedName": ""
//},
//"AdminSeedModel": {
// "FirstName": "",
// "LastName": "",
// "Email": "",
// "Password": "",
// "RoleName": ""
//},
// "Username": "",
// "FirstName": "",
// "LastName": "",
// "Email": "",
// "Password": "", // caps, numeric and symbol required
// "RoleName": "" // use admin role set above
//}
//"BusinessEmail": {
// "Host": "smtp.gmail.com",
// "Port": 587,
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
* <strike>Video Demo Link: (In the Future)</strike>
* <strike>Unit of Work Project to demonstrate the Unit of Work with Repository Pattern design pattern</strike>

# Setup Project

* Run 'update-database' command to setup migrations.
* Uncomment and update the seed fields in appsettings.json.
* Run application to apply seeding.
* Empty the seed fields from appsettings.json and comment again.

# Web API functionalities
* Worker Service Project - to deactivate a user subscription
* Unit Test Project (Under Development)
Expand Down

0 comments on commit a220f72

Please sign in to comment.