diff --git a/RomRepoMgr.Core/Workers/DatImporter.cs b/RomRepoMgr.Core/Workers/DatImporter.cs index b6355d5..67d5bcf 100644 --- a/RomRepoMgr.Core/Workers/DatImporter.cs +++ b/RomRepoMgr.Core/Workers/DatImporter.cs @@ -28,6 +28,8 @@ using System.Diagnostics; using System.IO; using Aaru.Checksums; using RomRepoMgr.Core.EventArgs; +using RomRepoMgr.Database; +using RomRepoMgr.Database.Models; using SabreTools.Library.DatFiles; using ErrorEventArgs = RomRepoMgr.Core.EventArgs.ErrorEventArgs; @@ -83,6 +85,31 @@ namespace RomRepoMgr.Core.Workers return; } + SetMessage?.Invoke(this, new MessageEventArgs + { + Message = "Adding DAT to database..." + }); + + // TODO: Check if there is a has in database but not in repo + + var romSet = new RomSet + { + Author = datFile.Header.Author, + Comment = datFile.Header.Comment, + Date = datFile.Header.Date, + Description = datFile.Header.Description, + Filename = Path.GetFileName(_datPath), + Homepage = datFile.Header.Homepage, + Name = datFile.Header.Name, + Sha384 = datHash, + Version = datFile.Header.Version, + CreatedOn = DateTime.UtcNow, + UpdatedOn = DateTime.UtcNow + }; + + Context.Singleton.RomSets.Add(romSet); + Context.Singleton.SaveChanges(); + SetMessage?.Invoke(this, new MessageEventArgs { Message = "Compressing DAT file..." diff --git a/RomRepoMgr.Database/Context.cs b/RomRepoMgr.Database/Context.cs index d3848fd..cb2b2ff 100644 --- a/RomRepoMgr.Database/Context.cs +++ b/RomRepoMgr.Database/Context.cs @@ -52,7 +52,8 @@ namespace RomRepoMgr.Database } } - public DbSet Files { get; set; } + public DbSet Files { get; set; } + public DbSet RomSets { get; set; } public static Context Create(string dbPath) { @@ -76,6 +77,27 @@ namespace RomRepoMgr.Database entity.HasIndex(e => e.Size); }); + + modelBuilder.Entity(entity => + { + entity.HasIndex(e => e.Author); + + entity.HasIndex(e => e.Comment); + + entity.HasIndex(e => e.Date); + + entity.HasIndex(e => e.Description); + + entity.HasIndex(e => e.Homepage); + + entity.HasIndex(e => e.Name); + + entity.HasIndex(e => e.Version); + + entity.HasIndex(e => e.Filename); + + entity.HasIndex(e => e.Sha384); + }); } } } \ No newline at end of file diff --git a/RomRepoMgr.Database/Migrations/20200821223117_AddFiles.Designer.cs b/RomRepoMgr.Database/Migrations/20200821223117_AddFiles.Designer.cs index d052e3a..697da4c 100644 --- a/RomRepoMgr.Database/Migrations/20200821223117_AddFiles.Designer.cs +++ b/RomRepoMgr.Database/Migrations/20200821223117_AddFiles.Designer.cs @@ -29,7 +29,6 @@ namespace RomRepoMgr.Database.Migrations .HasMaxLength(8); b.Property("CreatedOn") - .ValueGeneratedOnAdd() .HasColumnType("TEXT"); b.Property("Md5") @@ -48,7 +47,6 @@ namespace RomRepoMgr.Database.Migrations .HasColumnType("INTEGER"); b.Property("UpdatedOn") - .ValueGeneratedOnAddOrUpdate() .HasColumnType("TEXT"); b.HasKey("Id"); diff --git a/RomRepoMgr.Database/Migrations/20200822042211_AddRomSets.Designer.cs b/RomRepoMgr.Database/Migrations/20200822042211_AddRomSets.Designer.cs new file mode 100644 index 0000000..39f68b6 --- /dev/null +++ b/RomRepoMgr.Database/Migrations/20200822042211_AddRomSets.Designer.cs @@ -0,0 +1,132 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using RomRepoMgr.Database; + +namespace RomRepoMgr.Database.Migrations +{ + [DbContext(typeof(Context))] + [Migration("20200822042211_AddRomSets")] + partial class AddRomSets + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.7"); + + modelBuilder.Entity("RomRepoMgr.Database.Models.DbFile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Crc32") + .HasColumnType("TEXT") + .HasMaxLength(8); + + b.Property("CreatedOn") + .HasColumnType("TEXT"); + + b.Property("Md5") + .HasColumnType("TEXT") + .HasMaxLength(32); + + b.Property("Sha1") + .HasColumnType("TEXT") + .HasMaxLength(40); + + b.Property("Sha256") + .HasColumnType("TEXT") + .HasMaxLength(64); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("UpdatedOn") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Crc32"); + + b.HasIndex("Sha1"); + + b.HasIndex("Sha256"); + + b.HasIndex("Size"); + + b.ToTable("Files"); + }); + + modelBuilder.Entity("RomRepoMgr.Database.Models.RomSet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Author") + .HasColumnType("TEXT"); + + b.Property("Comment") + .HasColumnType("TEXT"); + + b.Property("CreatedOn") + .HasColumnType("TEXT"); + + b.Property("Date") + .HasColumnType("TEXT"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("Filename") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Homepage") + .HasColumnType("TEXT"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("Sha384") + .IsRequired() + .HasColumnType("TEXT") + .HasMaxLength(96); + + b.Property("UpdatedOn") + .HasColumnType("TEXT"); + + b.Property("Version") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Author"); + + b.HasIndex("Comment"); + + b.HasIndex("Date"); + + b.HasIndex("Description"); + + b.HasIndex("Filename"); + + b.HasIndex("Homepage"); + + b.HasIndex("Name"); + + b.HasIndex("Sha384"); + + b.HasIndex("Version"); + + b.ToTable("RomSets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RomRepoMgr.Database/Migrations/20200822042211_AddRomSets.cs b/RomRepoMgr.Database/Migrations/20200822042211_AddRomSets.cs new file mode 100644 index 0000000..23af3bf --- /dev/null +++ b/RomRepoMgr.Database/Migrations/20200822042211_AddRomSets.cs @@ -0,0 +1,75 @@ +/****************************************************************************** +// RomRepoMgr - ROM repository manager +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2020 Natalia Portillo +*******************************************************************************/ + +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace RomRepoMgr.Database.Migrations +{ + public partial class AddRomSets : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable("RomSets", table => new + { + Id = table.Column(nullable: false).Annotation("Sqlite:Autoincrement", true), + CreatedOn = table.Column(nullable: false), + UpdatedOn = table.Column(nullable: false), + Author = table.Column(nullable: true), + Comment = table.Column(nullable: true), + Date = table.Column(nullable: true), + Description = table.Column(nullable: true), + Homepage = table.Column(nullable: true), + Name = table.Column(nullable: true), + Version = table.Column(nullable: true), + Filename = table.Column(nullable: false), + Sha384 = table.Column(maxLength: 96, nullable: false) + }, constraints: table => + { + table.PrimaryKey("PK_RomSets", x => x.Id); + }); + + migrationBuilder.CreateIndex("IX_RomSets_Author", "RomSets", "Author"); + + migrationBuilder.CreateIndex("IX_RomSets_Comment", "RomSets", "Comment"); + + migrationBuilder.CreateIndex("IX_RomSets_Date", "RomSets", "Date"); + + migrationBuilder.CreateIndex("IX_RomSets_Description", "RomSets", "Description"); + + migrationBuilder.CreateIndex("IX_RomSets_Filename", "RomSets", "Filename"); + + migrationBuilder.CreateIndex("IX_RomSets_Homepage", "RomSets", "Homepage"); + + migrationBuilder.CreateIndex("IX_RomSets_Name", "RomSets", "Name"); + + migrationBuilder.CreateIndex("IX_RomSets_Sha384", "RomSets", "Sha384"); + + migrationBuilder.CreateIndex("IX_RomSets_Version", "RomSets", "Version"); + } + + protected override void Down(MigrationBuilder migrationBuilder) => migrationBuilder.DropTable("RomSets"); + } +} \ No newline at end of file diff --git a/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs b/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs index 5ca1f59..538e2cb 100644 --- a/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs +++ b/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs @@ -20,7 +20,7 @@ namespace RomRepoMgr.Database.Migrations b.Property("Crc32").HasColumnType("TEXT").HasMaxLength(8); - b.Property("CreatedOn").ValueGeneratedOnAdd().HasColumnType("TEXT"); + b.Property("CreatedOn").HasColumnType("TEXT"); b.Property("Md5").HasColumnType("TEXT").HasMaxLength(32); @@ -30,7 +30,7 @@ namespace RomRepoMgr.Database.Migrations b.Property("Size").HasColumnType("INTEGER"); - b.Property("UpdatedOn").ValueGeneratedOnAddOrUpdate().HasColumnType("TEXT"); + b.Property("UpdatedOn").HasColumnType("TEXT"); b.HasKey("Id"); @@ -44,6 +44,55 @@ namespace RomRepoMgr.Database.Migrations b.ToTable("Files"); }); + + modelBuilder.Entity("RomRepoMgr.Database.Models.RomSet", b => + { + b.Property("Id").ValueGeneratedOnAdd().HasColumnType("INTEGER"); + + b.Property("Author").HasColumnType("TEXT"); + + b.Property("Comment").HasColumnType("TEXT"); + + b.Property("CreatedOn").HasColumnType("TEXT"); + + b.Property("Date").HasColumnType("TEXT"); + + b.Property("Description").HasColumnType("TEXT"); + + b.Property("Filename").IsRequired().HasColumnType("TEXT"); + + b.Property("Homepage").HasColumnType("TEXT"); + + b.Property("Name").HasColumnType("TEXT"); + + b.Property("Sha384").IsRequired().HasColumnType("TEXT").HasMaxLength(96); + + b.Property("UpdatedOn").HasColumnType("TEXT"); + + b.Property("Version").HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Author"); + + b.HasIndex("Comment"); + + b.HasIndex("Date"); + + b.HasIndex("Description"); + + b.HasIndex("Filename"); + + b.HasIndex("Homepage"); + + b.HasIndex("Name"); + + b.HasIndex("Sha384"); + + b.HasIndex("Version"); + + b.ToTable("RomSets"); + }); #pragma warning restore 612, 618 } } diff --git a/RomRepoMgr.Database/Models/BaseModel.cs b/RomRepoMgr.Database/Models/BaseModel.cs index bbd840d..8fe5ad0 100644 --- a/RomRepoMgr.Database/Models/BaseModel.cs +++ b/RomRepoMgr.Database/Models/BaseModel.cs @@ -24,7 +24,6 @@ *******************************************************************************/ using System; -using System.ComponentModel.DataAnnotations.Schema; namespace RomRepoMgr.Database.Models { @@ -32,9 +31,7 @@ namespace RomRepoMgr.Database.Models { public TKey Id { get; set; } - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public DateTime CreatedOn { get; set; } - [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime UpdatedOn { get; set; } } } \ No newline at end of file diff --git a/RomRepoMgr.Database/Models/RomSet.cs b/RomRepoMgr.Database/Models/RomSet.cs new file mode 100644 index 0000000..3578ab6 --- /dev/null +++ b/RomRepoMgr.Database/Models/RomSet.cs @@ -0,0 +1,44 @@ +/****************************************************************************** +// RomRepoMgr - ROM repository manager +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2020 Natalia Portillo +*******************************************************************************/ + +using System.ComponentModel.DataAnnotations; + +namespace RomRepoMgr.Database.Models +{ + public class RomSet : BaseModel + { + public string Author { get; set; } + public string Comment { get; set; } + public string Date { get; set; } + public string Description { get; set; } + public string Homepage { get; set; } + public string Name { get; set; } + public string Version { get; set; } + [Required] + public string Filename { get; set; } + [Required, StringLength(96, MinimumLength = 96)] + public string Sha384 { get; set; } + } +} \ No newline at end of file