From 9323b48e43be25ff2cd8cc5d9f5ffaf449517f1d Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 22 Aug 2020 14:20:59 +0100 Subject: [PATCH] Add machines (games) to database. --- RomRepoMgr.Core/Workers/DatImporter.cs | 55 ++++++ RomRepoMgr.Database/Context.cs | 12 +- .../20200822130751_AddMachine.Designer.cs | 169 ++++++++++++++++++ .../Migrations/20200822130751_AddMachine.cs | 57 ++++++ .../Migrations/ContextModelSnapshot.cs | 27 +++ RomRepoMgr.Database/Models/Machine.cs | 37 ++++ RomRepoMgr.Database/Models/RomSet.cs | 3 + 7 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 RomRepoMgr.Database/Migrations/20200822130751_AddMachine.Designer.cs create mode 100644 RomRepoMgr.Database/Migrations/20200822130751_AddMachine.cs create mode 100644 RomRepoMgr.Database/Models/Machine.cs diff --git a/RomRepoMgr.Core/Workers/DatImporter.cs b/RomRepoMgr.Core/Workers/DatImporter.cs index 32e8520..2af3356 100644 --- a/RomRepoMgr.Core/Workers/DatImporter.cs +++ b/RomRepoMgr.Core/Workers/DatImporter.cs @@ -24,8 +24,10 @@ *******************************************************************************/ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using Aaru.Checksums; using RomRepoMgr.Core.EventArgs; using RomRepoMgr.Core.Models; @@ -139,6 +141,59 @@ namespace RomRepoMgr.Core.Workers datCompress.SetProgressBounds += SetProgressBounds; datCompress.CompressFile(_datPath, compressedDatPath); + SetMessage?.Invoke(this, new MessageEventArgs + { + Message = "Getting machine (game) names..." + }); + + List machineNames = + (from value in datFile.Items.Values from item in value select item.Machine.Name).Distinct(). + ToList(); + + SetMessage?.Invoke(this, new MessageEventArgs + { + Message = "Adding machines (games)..." + }); + + SetProgressBounds?.Invoke(this, new ProgressBoundsEventArgs + { + Minimum = 0, + Maximum = machineNames.Count + }); + + int position = 0; + Dictionary machines = new Dictionary(); + + foreach(string name in machineNames) + { + SetProgress?.Invoke(this, new ProgressEventArgs + { + Value = position + }); + + var machine = new Machine + { + Name = name, + RomSet = romSet, + CreatedOn = DateTime.UtcNow, + UpdatedOn = DateTime.UtcNow + }; + + machines[name] = machine; + + Context.Singleton.Machines.Add(machine); + position++; + } + + SetMessage?.Invoke(this, new MessageEventArgs + { + Message = "Saving changes to database..." + }); + + SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty); + + Context.Singleton.SaveChanges(); + WorkFinished?.Invoke(this, System.EventArgs.Empty); } catch(Exception e) diff --git a/RomRepoMgr.Database/Context.cs b/RomRepoMgr.Database/Context.cs index cb2b2ff..1ce632d 100644 --- a/RomRepoMgr.Database/Context.cs +++ b/RomRepoMgr.Database/Context.cs @@ -52,8 +52,9 @@ namespace RomRepoMgr.Database } } - public DbSet Files { get; set; } - public DbSet RomSets { get; set; } + public DbSet Files { get; set; } + public DbSet RomSets { get; set; } + public DbSet Machines { get; set; } public static Context Create(string dbPath) { @@ -98,6 +99,13 @@ namespace RomRepoMgr.Database entity.HasIndex(e => e.Sha384); }); + + modelBuilder.Entity(entity => + { + entity.HasIndex(e => e.Name); + + entity.HasOne(e => e.RomSet).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade); + }); } } } \ No newline at end of file diff --git a/RomRepoMgr.Database/Migrations/20200822130751_AddMachine.Designer.cs b/RomRepoMgr.Database/Migrations/20200822130751_AddMachine.Designer.cs new file mode 100644 index 0000000..406c190 --- /dev/null +++ b/RomRepoMgr.Database/Migrations/20200822130751_AddMachine.Designer.cs @@ -0,0 +1,169 @@ +// +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("20200822130751_AddMachine")] + partial class AddMachine + { + 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.Machine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedOn") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("RomSetId") + .HasColumnType("INTEGER"); + + b.Property("UpdatedOn") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.HasIndex("RomSetId"); + + b.ToTable("Machines"); + }); + + 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"); + }); + + modelBuilder.Entity("RomRepoMgr.Database.Models.Machine", b => + { + b.HasOne("RomRepoMgr.Database.Models.RomSet", "RomSet") + .WithMany("Machines") + .HasForeignKey("RomSetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RomRepoMgr.Database/Migrations/20200822130751_AddMachine.cs b/RomRepoMgr.Database/Migrations/20200822130751_AddMachine.cs new file mode 100644 index 0000000..e8c3d00 --- /dev/null +++ b/RomRepoMgr.Database/Migrations/20200822130751_AddMachine.cs @@ -0,0 +1,57 @@ +/****************************************************************************** +// 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 AddMachine : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable("Machines", table => new + { + Id = table.Column(nullable: false).Annotation("Sqlite:Autoincrement", true), + CreatedOn = table.Column(nullable: false), + UpdatedOn = table.Column(nullable: false), + Name = table.Column(nullable: false), + RomSetId = table.Column(nullable: false) + }, constraints: table => + { + table.PrimaryKey("PK_Machines", x => x.Id); + + table.ForeignKey("FK_Machines_RomSets_RomSetId", x => x.RomSetId, "RomSets", "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex("IX_Machines_Name", "Machines", "Name"); + + migrationBuilder.CreateIndex("IX_Machines_RomSetId", "Machines", "RomSetId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) => migrationBuilder.DropTable("Machines"); + } +} \ No newline at end of file diff --git a/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs b/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs index 538e2cb..fac3534 100644 --- a/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs +++ b/RomRepoMgr.Database/Migrations/ContextModelSnapshot.cs @@ -45,6 +45,27 @@ namespace RomRepoMgr.Database.Migrations b.ToTable("Files"); }); + modelBuilder.Entity("RomRepoMgr.Database.Models.Machine", b => + { + b.Property("Id").ValueGeneratedOnAdd().HasColumnType("INTEGER"); + + b.Property("CreatedOn").HasColumnType("TEXT"); + + b.Property("Name").IsRequired().HasColumnType("TEXT"); + + b.Property("RomSetId").HasColumnType("INTEGER"); + + b.Property("UpdatedOn").HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.HasIndex("RomSetId"); + + b.ToTable("Machines"); + }); + modelBuilder.Entity("RomRepoMgr.Database.Models.RomSet", b => { b.Property("Id").ValueGeneratedOnAdd().HasColumnType("INTEGER"); @@ -93,6 +114,12 @@ namespace RomRepoMgr.Database.Migrations b.ToTable("RomSets"); }); + + modelBuilder.Entity("RomRepoMgr.Database.Models.Machine", b => + { + b.HasOne("RomRepoMgr.Database.Models.RomSet", "RomSet").WithMany("Machines").HasForeignKey("RomSetId"). + OnDelete(DeleteBehavior.Cascade).IsRequired(); + }); #pragma warning restore 612, 618 } } diff --git a/RomRepoMgr.Database/Models/Machine.cs b/RomRepoMgr.Database/Models/Machine.cs new file mode 100644 index 0000000..93c5c9f --- /dev/null +++ b/RomRepoMgr.Database/Models/Machine.cs @@ -0,0 +1,37 @@ +/****************************************************************************** +// 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 Machine : BaseModel + { + [Required] + public string Name { get; set; } + [Required] + public virtual RomSet RomSet { get; set; } + } +} \ No newline at end of file diff --git a/RomRepoMgr.Database/Models/RomSet.cs b/RomRepoMgr.Database/Models/RomSet.cs index 3578ab6..e478af2 100644 --- a/RomRepoMgr.Database/Models/RomSet.cs +++ b/RomRepoMgr.Database/Models/RomSet.cs @@ -23,6 +23,7 @@ // Copyright © 2020 Natalia Portillo *******************************************************************************/ +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace RomRepoMgr.Database.Models @@ -40,5 +41,7 @@ namespace RomRepoMgr.Database.Models public string Filename { get; set; } [Required, StringLength(96, MinimumLength = 96)] public string Sha384 { get; set; } + + public virtual ICollection Machines { get; set; } } } \ No newline at end of file