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

HTML decode category names #102

Merged
merged 1 commit into from
Sep 14, 2023
Merged
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
2 changes: 2 additions & 0 deletions src/tools/MZikmund.LegacyMigration/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Spacing="8" VerticalAlignment="Center">
<Button Command="{x:Bind ViewModel.OpenSourceFolderCommand}">Open source folder</Button>
<TextBox PlaceholderText="OpenAI API key" Text="{x:Bind ViewModel.OpenAiApiKey, Mode=TwoWay}" />
<TextBox PlaceholderText="Connection string override" Text="{x:Bind ViewModel.ConnectionStringOverride, Mode=TwoWay}" />
<Button Command="{x:Bind ViewModel.AddExcerptsCommand}">Add excerpts</Button>
<Button Command="{x:Bind ViewModel.TryParseSourcesCommand}">Parse and upload</Button>
<Button Command="{x:Bind ViewModel.DecodeCategoriesCommand}">Decode categories</Button>
</StackPanel>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IDictionary<long, CategoryEntity> Process()
var category = new CategoryEntity
{
Id = Guid.NewGuid(),
DisplayName = term.Name,
DisplayName = System.Net.WebUtility.HtmlDecode(term.Name),
RouteName = term.Slug,
//ParentId = termTaxonomy.Parent,
Description = termTaxonomy.Description,
Expand Down
37 changes: 34 additions & 3 deletions src/tools/MZikmund.LegacyMigration/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public MainViewModel(Window window)

public string OpenAiApiKey { get; set; } = string.Empty;

public string ConnectionStringOverride { get; set; } = string.Empty;

[RelayCommand]
public async Task OpenSourceFolderAsync()
{
Expand Down Expand Up @@ -77,9 +79,7 @@ public async Task TryParseSourcesAsync()
var postProcessor = new PostProcessor(posts, postMeta, terms, termRelationships, termTaxonomies, tags, categories);
var processedPosts = postProcessor.Process();

var contextOptionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
contextOptionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=MZikmundDb;Trusted_Connection=True;");
using var databaseContext = new DatabaseContext(contextOptionsBuilder.Options);
using var databaseContext = CreateDatabaseContext();
//databaseContext.Database.Migrate();

var existingCategories = new HashSet<string>(await databaseContext.Category.Select(c => c.RouteName).ToListAsync());
Expand Down Expand Up @@ -150,6 +150,24 @@ public async Task TryParseSourcesAsync()
await databaseContext.SaveChangesAsync();
}

private DatabaseContext CreateDatabaseContext()
{
var contextOptionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
contextOptionsBuilder.UseSqlServer(GetConnectionString());
var databaseContext = new DatabaseContext(contextOptionsBuilder.Options);
return databaseContext;
}

private string GetConnectionString()
{
if (string.IsNullOrEmpty(ConnectionStringOverride))
{
return "Server=(localdb)\\MSSQLLocalDB;Database=MZikmundDb;Trusted_Connection=True;";
}

return ConnectionStringOverride;
}

[RelayCommand]
public async Task AddExcerptsAsync()
{
Expand All @@ -174,6 +192,19 @@ public async Task AddExcerptsAsync()
await FileIO.WriteTextAsync(targetFile, content);
}

[RelayCommand]
public async Task DecodeCategoriesAsync()
{
using var databaseContext = CreateDatabaseContext();
var allCategories = await databaseContext.Category.ToListAsync();
foreach (var category in allCategories)
{
category.DisplayName = System.Net.WebUtility.HtmlDecode(category.DisplayName);
}

await databaseContext.SaveChangesAsync();
}

private async Task<IList<T>> Parse<T>(StorageFile? sourceFile)
{
if (sourceFile is null)
Expand Down