Skip to content

Commit

Permalink
Migrate from localdb to postgre
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1ngekyo committed Oct 10, 2023
1 parent f51a7c3 commit f6b1a5c
Show file tree
Hide file tree
Showing 12 changed files with 894 additions and 888 deletions.
2 changes: 1 addition & 1 deletion src/Infrastructure/BlogTemplate.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
81 changes: 81 additions & 0 deletions src/Infrastructure/DatabaseInitialBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using BlogTemplate.Application.Abstractions;
using BlogTemplate.Domain;
using BlogTemplate.Domain.Models;
using BlogTemplate.Infrastructure.Data;

using Microsoft.AspNetCore.Identity;

namespace BlogTemplate.Infrastructure;

public class DatabaseInitialBuilder : IDatabaseInitialBuilder
{
private readonly ApplicationDbContext _context;
private readonly IUserManagerProxy<ApplicationUser> _userManager;
private readonly IRoleManagerProxy<IdentityRole> _roleManager;
public DatabaseInitialBuilder(ApplicationDbContext context,
IUserManagerProxy<ApplicationUser> userManager,
IRoleManagerProxy<IdentityRole> roleManager)
{
_context = context;
_userManager = userManager;
_roleManager = roleManager;
if (!_roleManager.RoleExistsAsync(WebsiteRoles.WebsiteAdmin!).GetAwaiter().GetResult())
{
_roleManager.CreateAsync(new IdentityRole(WebsiteRoles.WebsiteAdmin!)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(WebsiteRoles.WebsiteAuthor!)).GetAwaiter().GetResult();
}
}

public IDatabaseInitialBuilder AddAdminUser()
{
var user = new ApplicationUser()
{
UserName = "Admin",
Email = "[email protected]",
FirstName = "TestName",
LastName = "TestSurname"
};
_userManager.CreateAsync(user, "Admin@1234").Wait();
_userManager.AddToRoleAsync(user, WebsiteRoles.WebsiteAdmin!).GetAwaiter().GetResult();
return this;
}

public IDatabaseInitialBuilder AddPages()
{
_context.Pages!.AddRange(new List<Page>()
{
new Page()
{
Title = "About Us",
Slug = "about"
},
new Page()
{
Title = "Contact Us",
Slug = "contact"
},
new Page()
{
Title = "Privacy Policy",
Slug = "privacy"
}
});
return this;
}

public IDatabaseInitialBuilder AddSettings()
{
_context.Settings!.Add(new Setting
{
SiteName = "Site Name",
Title = "Site Title",
ShortDescription = "Short Description of site"
});
return this;
}

public void Build()
{
_context.SaveChanges();
}
}
78 changes: 0 additions & 78 deletions src/Infrastructure/DbInitializer.cs

This file was deleted.

11 changes: 11 additions & 0 deletions src/Infrastructure/IDatabaseInitialBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BlogTemplate.Domain.Models;

namespace BlogTemplate.Infrastructure;

public interface IDatabaseInitialBuilder
{
IDatabaseInitialBuilder AddAdminUser();
IDatabaseInitialBuilder AddPages();
IDatabaseInitialBuilder AddSettings();
void Build();
}
6 changes: 0 additions & 6 deletions src/Infrastructure/IDbInitializer.cs

This file was deleted.

Loading

0 comments on commit f6b1a5c

Please sign in to comment.