diff --git a/ApplicationServices/ApplicationServices.csproj b/ApplicationServices/ApplicationServices.csproj index 516c02c4..e738d34d 100644 --- a/ApplicationServices/ApplicationServices.csproj +++ b/ApplicationServices/ApplicationServices.csproj @@ -4,6 +4,11 @@ netstandard2.1 + + + + + diff --git a/ApplicationServices/UNTESTED/LibraryCommands.cs b/ApplicationServices/UNTESTED/LibraryCommands.cs index a2ddd05f..5393abc7 100644 --- a/ApplicationServices/UNTESTED/LibraryCommands.cs +++ b/ApplicationServices/UNTESTED/LibraryCommands.cs @@ -24,7 +24,7 @@ namespace ApplicationServices var totalCount = importItems.Count; Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}"); - var newCount = await getNewCountAsync(importItems); + var newCount = await importIntoDbAsync(importItems); Log.Logger.Information($"Import: New count {newCount}"); await Task.Run(() => SearchEngineCommands.FullReIndex()); @@ -75,7 +75,7 @@ namespace ApplicationServices return dtoItems.Select(d => new ImportItem { DtoItem = d, AccountId = account.AccountId, LocaleName = localeName }).ToList(); } - private static async Task getNewCountAsync(List importItems) + private static async Task importIntoDbAsync(List importItems) { using var context = DbContexts.GetContext(); var libraryImporter = new LibraryImporter(context); diff --git a/ApplicationServices/UNTESTED/LibraryExporter.cs b/ApplicationServices/UNTESTED/LibraryExporter.cs new file mode 100644 index 00000000..5880b2d5 --- /dev/null +++ b/ApplicationServices/UNTESTED/LibraryExporter.cs @@ -0,0 +1,232 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CsvHelper; +using CsvHelper.Configuration.Attributes; +using DataLayer; +using NPOI.XSSF.UserModel; +using Serilog; + +namespace ApplicationServices +{ + public class ExportDto + { + [Name("Account")] + public string Account { get; set; } + + [Name("Date Added to library")] + public DateTime DateAdded { get; set; } + + [Name("Audible Product Id")] + public string AudibleProductId { get; set; } + + [Name("Locale")] + public string Locale { get; set; } + + [Name("Title")] + public string Title { get; set; } + + [Name("Authors")] + public string AuthorNames { get; set; } + + [Name("Narrators")] + public string NarratorNames { get; set; } + + [Name("Length In Minutes")] + public int LengthInMinutes { get; set; } + + [Name("Publisher")] + public string Publisher { get; set; } + + [Name("Pdf url")] + public string PdfUrl { get; set; } + + [Name("Series Names")] + public string SeriesNames{ get; set; } + + [Name("Series Order")] + public string SeriesOrder { get; set; } + + [Name("Community Rating: Overall")] + public float? CommunityRatingOverall { get; set; } + + [Name("Community Rating: Performance")] + public float? CommunityRatingPerformance { get; set; } + + [Name("Community Rating: Story")] + public float? CommunityRatingStory { get; set; } + + [Name("Cover Id")] + public string PictureId { get; set; } + + [Name("Is Abridged?")] + public bool IsAbridged { get; set; } + + [Name("Date Published")] + public DateTime? DatePublished { get; set; } + + [Name("Categories")] + public string CategoriesNames { get; set; } + + [Name("My Rating: Overall")] + public float? MyRatingOverall { get; set; } + + [Name("My Rating: Performance")] + public float? MyRatingPerformance { get; set; } + + [Name("My Rating: Story")] + public float? MyRatingStory { get; set; } + + [Name("My Libation Tags")] + public string MyLibationTags { get; set; } + } + public static class LibToDtos + { + public static List ToDtos(this IEnumerable library) + => library.Select(a => new ExportDto + { + Account = a.Account, + DateAdded = a.DateAdded, + AudibleProductId = a.Book.AudibleProductId, + Locale = a.Book.Locale, + Title = a.Book.Title, + AuthorNames = a.Book.AuthorNames, + NarratorNames = a.Book.NarratorNames, + LengthInMinutes = a.Book.LengthInMinutes, + Publisher = a.Book.Publisher, + PdfUrl = a.Book.Supplements?.FirstOrDefault()?.Url, + SeriesNames = a.Book.SeriesNames, + SeriesOrder = a.Book.SeriesLink.Any() ? a.Book.SeriesLink?.Select(sl => $"{sl.Index} : {sl.Series.Name}").Aggregate((a, b) => $"{a}, {b}") : "", + CommunityRatingOverall = a.Book.Rating?.OverallRating, + CommunityRatingPerformance = a.Book.Rating?.PerformanceRating, + CommunityRatingStory = a.Book.Rating?.StoryRating, + PictureId = a.Book.PictureId, + IsAbridged = a.Book.IsAbridged, + DatePublished = a.Book.DatePublished, + CategoriesNames = a.Book.CategoriesNames.Any() ? a.Book.CategoriesNames.Aggregate((a, b) => $"{a}, {b}") : "", + MyRatingOverall = a.Book.UserDefinedItem.Rating.OverallRating, + MyRatingPerformance = a.Book.UserDefinedItem.Rating.PerformanceRating, + MyRatingStory = a.Book.UserDefinedItem.Rating.StoryRating, + MyLibationTags = a.Book.UserDefinedItem.Tags + }).ToList(); + } + public static class LibraryExporter + { + private static List GetAccts() + { + var ia = true; + var userAccounts = new List(); + for (var i = 0; i < 7; i++) + userAccounts.Add(new acct { UserName = "u" + i, Email = "e" + i, CreationDate = DateTime.Now.AddDays(-i * 2), LastLoginDate = DateTime.Now.AddDays(-i), IsApproved = (ia = !ia), Comment = "c [ ] * % , ' \" \\ \n " + i }); + + return userAccounts; + } + + public static void ToCsv(string saveFilePath) + { + using var context = DbContexts.GetContext(); + var dtos = context.GetLibrary_Flat_NoTracking().ToDtos(); + + if (!dtos.Any()) + return; + + using var writer = new System.IO.StreamWriter(saveFilePath); + using var csv = new CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture); + + csv.WriteHeader(typeof(ExportDto)); + csv.NextRecord(); + csv.WriteRecords(dtos); + } + + public static void ToXlsx(string saveFilePath) + { + using var context = DbContexts.GetContext(); + + var library = context.GetLibrary_Flat_NoTracking(); + } + + public static void TEST_ToXlsx(string saveFilePath) + { + // https://steemit.com/utopian-io/@haig/how-to-create-excel-spreadsheets-using-npoi + + var workbook = new XSSFWorkbook(); + var sheet = workbook.CreateSheet("Library"); + + var detailSubtotalFont = workbook.CreateFont(); + detailSubtotalFont.IsBold = true; + + var detailSubtotalCellStyle = workbook.CreateCellStyle(); + detailSubtotalCellStyle.SetFont(detailSubtotalFont); + + // headers + var rowIndex = 0; + var row = sheet.CreateRow(rowIndex); + + { + var cell = row.CreateCell(0); + cell.SetCellValue("Username"); + cell.CellStyle = detailSubtotalCellStyle; + } + { + var cell = row.CreateCell(1); + cell.SetCellValue("Email"); + cell.CellStyle = detailSubtotalCellStyle; + } + { + var cell = row.CreateCell(2); + cell.SetCellValue("Joined"); + cell.CellStyle = detailSubtotalCellStyle; + } + { + var cell = row.CreateCell(3); + cell.SetCellValue("Last Login"); + cell.CellStyle = detailSubtotalCellStyle; + } + { + var cell = row.CreateCell(4); + cell.SetCellValue("Approved?"); + cell.CellStyle = detailSubtotalCellStyle; + } + { + var cell = row.CreateCell(5); + cell.SetCellValue("Comments"); + cell.CellStyle = detailSubtotalCellStyle; + } + + rowIndex++; + + // Add data rows + foreach (acct account in GetAccts()) + { + row = sheet.CreateRow(rowIndex); + row.CreateCell(0).SetCellValue(account.UserName); + row.CreateCell(1).SetCellValue(account.Email); + row.CreateCell(2).SetCellValue(account.CreationDate.ToShortDateString()); + row.CreateCell(3).SetCellValue(account.LastLoginDate.ToShortDateString()); + row.CreateCell(4).SetCellValue(account.IsApproved); + row.CreateCell(5).SetCellValue(account.Comment); + rowIndex++; + } + + using var fileData = new System.IO.FileStream(saveFilePath, System.IO.FileMode.Create); + workbook.Write(fileData); + } + class acct + { + public string UserName { get; set; } + public string Email { get; set; } + public DateTime CreationDate { get; set; } + public DateTime LastLoginDate { get; set; } + public bool IsApproved { get; set; } + public string Comment { get; set; } + } + + public static void ToJson(string saveFilePath) + { + using var context = DbContexts.GetContext(); + + var library = context.GetLibrary_Flat_NoTracking(); + + } + } +} diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 32054d9a..8f5b6829 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 4.0.3.1 + 4.0.4.4 diff --git a/LibationWinForms/UNTESTED/Form1.Designer.cs b/LibationWinForms/UNTESTED/Form1.Designer.cs index 71b735dc..dcd3a292 100644 --- a/LibationWinForms/UNTESTED/Form1.Designer.cs +++ b/LibationWinForms/UNTESTED/Form1.Designer.cs @@ -35,12 +35,14 @@ this.filterSearchTb = new System.Windows.Forms.TextBox(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.importToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.noAccountsYetAddAccountToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scanLibraryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scanLibraryOfAllAccountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scanLibraryOfSomeAccountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.liberateToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.beginBookBackupsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.beginPdfBackupsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.quickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.firstFilterIsDefaultToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.editQuickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -56,6 +58,7 @@ this.pdfsCountsLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.addFilterBtn = new System.Windows.Forms.Button(); this.noAccountsYetAddAccountToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exportLibraryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -106,6 +109,7 @@ this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.importToolStripMenuItem, this.liberateToolStripMenuItem, + this.exportToolStripMenuItem, this.quickFiltersToolStripMenuItem, this.settingsToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); @@ -125,6 +129,12 @@ this.importToolStripMenuItem.Size = new System.Drawing.Size(55, 20); this.importToolStripMenuItem.Text = "&Import"; // + // noAccountsYetAddAccountToolStripMenuItem + // + this.noAccountsYetAddAccountToolStripMenuItem.Name = "noAccountsYetAddAccountToolStripMenuItem"; + this.noAccountsYetAddAccountToolStripMenuItem.Size = new System.Drawing.Size(247, 22); + this.noAccountsYetAddAccountToolStripMenuItem.Text = "No accounts yet. A&dd Account..."; + // // scanLibraryToolStripMenuItem // this.scanLibraryToolStripMenuItem.Name = "scanLibraryToolStripMenuItem"; @@ -169,6 +179,14 @@ this.beginPdfBackupsToolStripMenuItem.Text = "Begin &PDF Only Backups: {0}"; this.beginPdfBackupsToolStripMenuItem.Click += new System.EventHandler(this.beginPdfBackupsToolStripMenuItem_Click); // + // exportToolStripMenuItem + // + this.exportToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.exportLibraryToolStripMenuItem}); + this.exportToolStripMenuItem.Name = "exportToolStripMenuItem"; + this.exportToolStripMenuItem.Size = new System.Drawing.Size(53, 20); + this.exportToolStripMenuItem.Text = "E&xport"; + // // quickFiltersToolStripMenuItem // this.quickFiltersToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -283,6 +301,13 @@ this.noAccountsYetAddAccountToolStripMenuItem.Text = "No accounts yet. A&dd Account..."; this.noAccountsYetAddAccountToolStripMenuItem.Click += new System.EventHandler(this.noAccountsYetAddAccountToolStripMenuItem_Click); // + // exportLibraryToolStripMenuItem + // + this.exportLibraryToolStripMenuItem.Name = "exportLibraryToolStripMenuItem"; + this.exportLibraryToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.exportLibraryToolStripMenuItem.Text = "E&xport Library..."; + this.exportLibraryToolStripMenuItem.Click += new System.EventHandler(this.exportLibraryToolStripMenuItem_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -338,5 +363,7 @@ private System.Windows.Forms.ToolStripMenuItem scanLibraryOfAllAccountsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem scanLibraryOfSomeAccountsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem noAccountsYetAddAccountToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportLibraryToolStripMenuItem; } } diff --git a/LibationWinForms/UNTESTED/Form1.cs b/LibationWinForms/UNTESTED/Form1.cs index 59490918..7be82178 100644 --- a/LibationWinForms/UNTESTED/Form1.cs +++ b/LibationWinForms/UNTESTED/Form1.cs @@ -130,6 +130,9 @@ namespace LibationWinForms var downloadedOnly = results.Count(r => r == AudioFileState.aax); var noProgress = results.Count(r => r == AudioFileState.none); + // enable/disable export + exportLibraryToolStripMenuItem.Enabled = results.Any(); + // update bottom numbers var pending = noProgress + downloadedOnly; var statusStripText @@ -299,6 +302,38 @@ namespace LibationWinForms private void updateGridRow(object _, LibraryBook libraryBook) => currProductsGrid.RefreshRow(libraryBook.Book.AudibleProductId); #endregion + #region Export menu + private void exportLibraryToolStripMenuItem_Click(object sender, EventArgs e) + { + try + { + var saveFileDialog = new SaveFileDialog + { + Title = "Where to export Library", + Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*" + }; + + if (saveFileDialog.ShowDialog() != DialogResult.OK) + return; + + switch (saveFileDialog.FilterIndex) + { + case 1: // csv + default: + LibraryExporter.ToCsv(saveFileDialog.FileName); + break; + } + + MessageBox.Show("Library exported to:\r\n" + saveFileDialog.FileName); + } + catch (Exception ex) + { + Serilog.Log.Error(ex, "Error attempting to export library"); + MessageBox.Show("Error attempting to export your library. Error message:\r\n\r\n" + ex.Message, "Error exporting", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + #endregion + #region quick filters menu private void loadInitialQuickFilterState() { diff --git a/WinFormsDesigner/Form1.Designer.cs b/WinFormsDesigner/Form1.Designer.cs index b32f5df5..a6bd43d9 100644 --- a/WinFormsDesigner/Form1.Designer.cs +++ b/WinFormsDesigner/Form1.Designer.cs @@ -35,12 +35,14 @@ this.filterSearchTb = new System.Windows.Forms.TextBox(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.importToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.noAccountsYetAddAccountToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scanLibraryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scanLibraryOfAllAccountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scanLibraryOfSomeAccountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.liberateToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.beginBookBackupsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.beginPdfBackupsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.quickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.firstFilterIsDefaultToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.editQuickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -55,7 +57,7 @@ this.backupsCountsLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.pdfsCountsLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.addFilterBtn = new System.Windows.Forms.Button(); - this.noAccountsYetAddAccountToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exportLibraryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -103,6 +105,7 @@ this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.importToolStripMenuItem, this.liberateToolStripMenuItem, + this.exportToolStripMenuItem, this.quickFiltersToolStripMenuItem, this.settingsToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); @@ -122,6 +125,12 @@ this.importToolStripMenuItem.Size = new System.Drawing.Size(55, 20); this.importToolStripMenuItem.Text = "&Import"; // + // noAccountsYetAddAccountToolStripMenuItem + // + this.noAccountsYetAddAccountToolStripMenuItem.Name = "noAccountsYetAddAccountToolStripMenuItem"; + this.noAccountsYetAddAccountToolStripMenuItem.Size = new System.Drawing.Size(247, 22); + this.noAccountsYetAddAccountToolStripMenuItem.Text = "No accounts yet. A&dd Account..."; + // // scanLibraryToolStripMenuItem // this.scanLibraryToolStripMenuItem.Name = "scanLibraryToolStripMenuItem"; @@ -161,6 +170,14 @@ this.beginPdfBackupsToolStripMenuItem.Size = new System.Drawing.Size(248, 22); this.beginPdfBackupsToolStripMenuItem.Text = "Begin &PDF Only Backups: {0}"; // + // exportToolStripMenuItem + // + this.exportToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.exportLibraryToolStripMenuItem}); + this.exportToolStripMenuItem.Name = "exportToolStripMenuItem"; + this.exportToolStripMenuItem.Size = new System.Drawing.Size(53, 20); + this.exportToolStripMenuItem.Text = "E&xport"; + // // quickFiltersToolStripMenuItem // this.quickFiltersToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -262,11 +279,11 @@ this.addFilterBtn.Text = "Add To Quick Filters"; this.addFilterBtn.UseVisualStyleBackColor = true; // - // noAccountsYetAddAccountToolStripMenuItem + // exportLibraryToolStripMenuItem // - this.noAccountsYetAddAccountToolStripMenuItem.Name = "noAccountsYetAddAccountToolStripMenuItem"; - this.noAccountsYetAddAccountToolStripMenuItem.Size = new System.Drawing.Size(247, 22); - this.noAccountsYetAddAccountToolStripMenuItem.Text = "No accounts yet. A&dd Account..."; + this.exportLibraryToolStripMenuItem.Name = "exportLibraryToolStripMenuItem"; + this.exportLibraryToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.exportLibraryToolStripMenuItem.Text = "E&xport Library..."; // // Form1 // @@ -322,5 +339,7 @@ private System.Windows.Forms.ToolStripMenuItem scanLibraryOfAllAccountsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem scanLibraryOfSomeAccountsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem noAccountsYetAddAccountToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportLibraryToolStripMenuItem; } }