From d8127630b329bf24a120181ca9d5a0c0736fb87e Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 28 Apr 2018 02:10:03 +0100 Subject: [PATCH] Update DB to version 14: Computers and consoles are now machines with different type. --- Cicm.Database/Operations/Computer.cs | 303 +----- Cicm.Database/Operations/Console.cs | 300 +----- Cicm.Database/Operations/Init.cs | 60 +- Cicm.Database/Operations/Machine.cs | 470 +++++++++ Cicm.Database/Operations/Operations.cs | 2 +- Cicm.Database/Operations/Update.cs | 281 ++++++ Cicm.Database/Schemas/Console.cs | 80 -- Cicm.Database/Schemas/Enums.cs | 10 + .../Schemas/{Computer.cs => Machine.cs} | 6 +- Cicm.Database/Schemas/Sql/V14.cs | 149 +++ cicm_web/Controllers/ComputerController.cs | 13 +- cicm_web/Controllers/ConsoleController.cs | 15 +- cicm_web/Controllers/MachineController.cs | 53 + cicm_web/Models/Company.cs | 4 +- cicm_web/Models/Computer.cs | 174 +--- cicm_web/Models/Console.cs | 146 +-- cicm_web/Models/Machine.cs | 232 +++++ cicm_web/Models/News.cs | 12 +- cicm_web/Models/OwnedComputer.cs | 4 +- cicm_web/Models/OwnedConsole.cs | 4 +- cicm_web/Views/Company/View.cshtml | 599 +++++------ cicm_web/Views/Computer/ByLetter.cshtml | 26 +- cicm_web/Views/Computer/ByYear.cshtml | 6 +- cicm_web/Views/Console/ByLetter.cshtml | 6 +- cicm_web/Views/Console/ByYear.cshtml | 6 +- cicm_web/Views/Console/View.cshtml | 938 ------------------ .../Views/{Computer => Machine}/View.cshtml | 2 +- cicm_web/cicm_web.csproj | 2 +- 28 files changed, 1694 insertions(+), 2209 deletions(-) create mode 100644 Cicm.Database/Operations/Machine.cs delete mode 100644 Cicm.Database/Schemas/Console.cs rename Cicm.Database/Schemas/{Computer.cs => Machine.cs} (97%) create mode 100644 Cicm.Database/Schemas/Sql/V14.cs create mode 100644 cicm_web/Controllers/MachineController.cs create mode 100644 cicm_web/Models/Machine.cs delete mode 100644 cicm_web/Views/Console/View.cshtml rename cicm_web/Views/{Computer => Machine}/View.cshtml (99%) diff --git a/Cicm.Database/Operations/Computer.cs b/Cicm.Database/Operations/Computer.cs index a3809664..d4bb45cb 100644 --- a/Cicm.Database/Operations/Computer.cs +++ b/Cicm.Database/Operations/Computer.cs @@ -32,7 +32,6 @@ using System; using System.Collections.Generic; using System.Data; using Cicm.Database.Schemas; -using Console = System.Console; namespace Cicm.Database { @@ -43,7 +42,7 @@ namespace Cicm.Database /// /// All computers /// true if is correct, false otherwise - public bool GetComputers(out List entries) + public bool GetComputers(out List entries) { #if DEBUG Console.WriteLine("Getting all computers..."); @@ -51,16 +50,16 @@ namespace Cicm.Database try { - const string SQL = "SELECT * from computers"; + string sql = $"SELECT * FROM machines WHERE type = '{(int)MachineType.Computer}'"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); - dbCmd.CommandText = SQL; + dbCmd.CommandText = sql; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); - entries = ComputersFromDataTable(dataSet.Tables[0]); + entries = MachinesFromDataTable(dataSet.Tables[0]); return true; } @@ -79,7 +78,7 @@ namespace Cicm.Database /// All computers /// Company id /// true if is correct, false otherwise - public bool GetComputers(out List entries, int company) + public bool GetComputers(out List entries, int company) { #if DEBUG Console.WriteLine("Getting all computers from company id {0}...", company); @@ -87,16 +86,16 @@ namespace Cicm.Database try { - const string SQL = "SELECT * from computers WHERE company = '{id}'"; + string sql = $"SELECT * FROM machines WHERE company = '{company}' AND type = '{(int)MachineType.Computer}'"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); - dbCmd.CommandText = SQL; + dbCmd.CommandText = sql; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); - entries = ComputersFromDataTable(dataSet.Tables[0]); + entries = MachinesFromDataTable(dataSet.Tables[0]); return true; } @@ -116,7 +115,7 @@ namespace Cicm.Database /// Start of query /// How many entries to retrieve /// true if is correct, false otherwise - public bool GetComputers(out List entries, ulong start, ulong count) + public bool GetComputers(out List entries, ulong start, ulong count) { #if DEBUG Console.WriteLine("Getting {0} computers from {1}...", count, start); @@ -124,7 +123,7 @@ namespace Cicm.Database try { - string sql = $"SELECT * FROM computers LIMIT {start}, {count}"; + string sql = $"SELECT * FROM machines LIMIT {start}, {count} WHERE type = '{(int)MachineType.Computer}'"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); @@ -133,7 +132,7 @@ namespace Cicm.Database dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); - entries = ComputersFromDataTable(dataSet.Tables[0]); + entries = MachinesFromDataTable(dataSet.Tables[0]); return true; } @@ -151,33 +150,9 @@ namespace Cicm.Database /// /// Id /// Computer with specified id, null if not found or error - public Computer GetComputer(int id) + public Machine GetComputer(int id) { - #if DEBUG - Console.WriteLine("Getting computer with id {0}...", id); - #endif - - try - { - string sql = $"SELECT * from computers WHERE id = '{id}'"; - - IDbCommand dbCmd = dbCon.CreateCommand(); - IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); - dbCmd.CommandText = sql; - DataSet dataSet = new DataSet(); - dataAdapter.SelectCommand = dbCmd; - dataAdapter.Fill(dataSet); - - List entries = ComputersFromDataTable(dataSet.Tables[0]); - - return entries == null || entries.Count == 0 ? null : entries[0]; - } - catch(Exception ex) - { - Console.WriteLine("Error getting computer."); - Console.WriteLine(ex); - return null; - } + return GetMachine(id); } /// @@ -191,7 +166,7 @@ namespace Cicm.Database #endif IDbCommand dbcmd = dbCon.CreateCommand(); - dbcmd.CommandText = "SELECT COUNT(*) FROM computers"; + dbcmd.CommandText = $"SELECT COUNT(*) FROM computers WHERE type = '{(int)MachineType.Computer}'"; object count = dbcmd.ExecuteScalar(); dbcmd.Dispose(); try { return Convert.ToInt64(count); } @@ -204,33 +179,10 @@ namespace Cicm.Database /// Entry to add /// ID of added entry /// true if added correctly, false otherwise - public bool AddComputer(Computer entry, out long id) + public bool AddComputer(Machine entry, out long id) { - #if DEBUG - Console.Write("Adding computer `{0}`...", entry.Model); - #endif - - IDbCommand dbcmd = GetCommandComputer(entry); - IDbTransaction trans = dbCon.BeginTransaction(); - dbcmd.Transaction = trans; - - const string SQL = - "INSERT INTO computers (company, year, model, cpu1, mhz1, cpu2, mhz2, bits, ram, rom, gpu, vram, colors, res, sound_synth, music_synth, sound_channels, music_channels, hdd1, hdd2, hdd3, disk1, cap1, disk2, cap2)" + - " VALUES (@company, @year, @model, @cpu1, @mhz1, @cpu2, @mhz2, @bits, @ram, @rom, @gpu, @vram, @colors, @res, @sound_synth, @music_synth, @sound_channels, @music_channels, @hdd1, @hdd2, @hdd3, @disk1, @cap1, @disk2, @cap2)"; - - dbcmd.CommandText = SQL; - - dbcmd.ExecuteNonQuery(); - trans.Commit(); - dbcmd.Dispose(); - - id = dbCore.LastInsertRowId; - - #if DEBUG - Console.WriteLine(" id {0}", id); - #endif - - return true; + entry.Type = MachineType.Computer; + return AddMachine(entry, out id); } /// @@ -238,29 +190,9 @@ namespace Cicm.Database /// /// Updated entry /// true if updated correctly, false otherwise - public bool UpdateComputer(Computer entry) + public bool UpdateComputer(Machine entry) { - #if DEBUG - Console.WriteLine("Updating computer `{0}`...", entry.Model); - #endif - - IDbCommand dbcmd = GetCommandComputer(entry); - IDbTransaction trans = dbCon.BeginTransaction(); - dbcmd.Transaction = trans; - - string sql = - "UPDATE computers SET company = @company, year = @year, model = @model, cpu1 = @cpu1, mhz1 = @mhz1, cpu2 = @cpu2, " + - "mhz2 = @mhz2, bits = @bits, ram = @ram, rom = @rom, gpu = @gpu, vram = @vram, colors = @colors, res = @res, sound_synth = @sound_synth, music_synth = @music_synth " + - "sound_channels = @sound_channels, music_channels = @music_channels, hdd1 = @hdd1, hdd2 = @hdd2, hdd3 = @hdd3, disk1 = @disk1, cap1 = @cap1, disk2 = @disk2, cap2 = @cap2 " + - $"WHERE id = {entry.Id}"; - - dbcmd.CommandText = sql; - - dbcmd.ExecuteNonQuery(); - trans.Commit(); - dbcmd.Dispose(); - - return true; + return UpdateMachine(entry); } /// @@ -270,202 +202,7 @@ namespace Cicm.Database /// true if removed correctly, false otherwise public bool RemoveComputer(long id) { - #if DEBUG - Console.WriteLine("Removing computer widh id `{0}`...", id); - #endif - - IDbCommand dbcmd = dbCon.CreateCommand(); - IDbTransaction trans = dbCon.BeginTransaction(); - dbcmd.Transaction = trans; - - string sql = $"DELETE FROM computers WHERE id = '{id}';"; - - dbcmd.CommandText = sql; - - dbcmd.ExecuteNonQuery(); - trans.Commit(); - dbcmd.Dispose(); - - return true; - } - - IDbCommand GetCommandComputer(Computer entry) - { - IDbCommand dbcmd = dbCon.CreateCommand(); - - IDbDataParameter param1 = dbcmd.CreateParameter(); - IDbDataParameter param2 = dbcmd.CreateParameter(); - IDbDataParameter param3 = dbcmd.CreateParameter(); - IDbDataParameter param4 = dbcmd.CreateParameter(); - IDbDataParameter param5 = dbcmd.CreateParameter(); - IDbDataParameter param6 = dbcmd.CreateParameter(); - IDbDataParameter param7 = dbcmd.CreateParameter(); - IDbDataParameter param8 = dbcmd.CreateParameter(); - IDbDataParameter param9 = dbcmd.CreateParameter(); - IDbDataParameter param10 = dbcmd.CreateParameter(); - IDbDataParameter param11 = dbcmd.CreateParameter(); - IDbDataParameter param12 = dbcmd.CreateParameter(); - IDbDataParameter param13 = dbcmd.CreateParameter(); - IDbDataParameter param14 = dbcmd.CreateParameter(); - IDbDataParameter param15 = dbcmd.CreateParameter(); - IDbDataParameter param16 = dbcmd.CreateParameter(); - IDbDataParameter param17 = dbcmd.CreateParameter(); - IDbDataParameter param18 = dbcmd.CreateParameter(); - IDbDataParameter param19 = dbcmd.CreateParameter(); - IDbDataParameter param20 = dbcmd.CreateParameter(); - IDbDataParameter param21 = dbcmd.CreateParameter(); - IDbDataParameter param22 = dbcmd.CreateParameter(); - IDbDataParameter param23 = dbcmd.CreateParameter(); - IDbDataParameter param24 = dbcmd.CreateParameter(); - IDbDataParameter param25 = dbcmd.CreateParameter(); - - param1.ParameterName = "@company"; - param2.ParameterName = "@year"; - param3.ParameterName = "@model"; - param4.ParameterName = "@cpu1"; - param5.ParameterName = "@mhz1"; - param6.ParameterName = "@cpu2"; - param7.ParameterName = "@mhz2"; - param8.ParameterName = "@bits"; - param9.ParameterName = "@ram"; - param10.ParameterName = "@rom"; - param11.ParameterName = "@gpu"; - param12.ParameterName = "@vram"; - param13.ParameterName = "@colors"; - param14.ParameterName = "@res"; - param15.ParameterName = "@sound_synth"; - param16.ParameterName = "@music_synth"; - param17.ParameterName = "@sound_channels"; - param18.ParameterName = "@music_channels"; - param19.ParameterName = "@hdd1"; - param20.ParameterName = "@hdd2"; - param21.ParameterName = "@hdd3"; - param22.ParameterName = "@disk1"; - param23.ParameterName = "@cap1"; - param24.ParameterName = "@disk2"; - param25.ParameterName = "@cap2"; - - param1.DbType = DbType.Int32; - param2.DbType = DbType.Int32; - param3.DbType = DbType.String; - param4.DbType = DbType.Int32; - param5.DbType = DbType.Double; - param6.DbType = DbType.Int32; - param7.DbType = DbType.Double; - param8.DbType = DbType.Int32; - param9.DbType = DbType.Int32; - param10.DbType = DbType.Int32; - param11.DbType = DbType.Int32; - param12.DbType = DbType.Int32; - param13.DbType = DbType.Int32; - param14.DbType = DbType.String; - param15.DbType = DbType.Int32; - param16.DbType = DbType.Int32; - param17.DbType = DbType.Int32; - param18.DbType = DbType.Int32; - param19.DbType = DbType.Int32; - param20.DbType = DbType.Int32; - param21.DbType = DbType.Int32; - param22.DbType = DbType.Int32; - param23.DbType = DbType.String; - param24.DbType = DbType.Int32; - param25.DbType = DbType.String; - - param1.Value = entry.Company; - param2.Value = entry.Year; - param3.Value = entry.Model; - param4.Value = entry.Cpu1; - param5.Value = entry.Mhz1; - param6.Value = entry.Cpu2; - param7.Value = entry.Mhz2; - param8.Value = entry.Bits; - param9.Value = entry.Ram; - param10.Value = entry.Rom; - param11.Value = entry.Gpu; - param12.Value = entry.Vram; - param13.Value = entry.Colors; - param14.Value = entry.Resolution; - param15.Value = entry.SoundSynth; - param16.Value = entry.MusicSynth; - param17.Value = entry.SoundChannels; - param18.Value = entry.MusicChannels; - param19.Value = entry.Hdd1; - param20.Value = entry.Hdd2; - param21.Value = entry.Hdd3; - param22.Value = entry.Disk1; - param23.Value = entry.Cap1; - param24.Value = entry.Disk2; - param25.Value = entry.Cap2; - - dbcmd.Parameters.Add(param1); - dbcmd.Parameters.Add(param2); - dbcmd.Parameters.Add(param3); - dbcmd.Parameters.Add(param4); - dbcmd.Parameters.Add(param5); - dbcmd.Parameters.Add(param6); - dbcmd.Parameters.Add(param7); - dbcmd.Parameters.Add(param8); - dbcmd.Parameters.Add(param9); - dbcmd.Parameters.Add(param10); - dbcmd.Parameters.Add(param11); - dbcmd.Parameters.Add(param12); - dbcmd.Parameters.Add(param13); - dbcmd.Parameters.Add(param14); - dbcmd.Parameters.Add(param15); - dbcmd.Parameters.Add(param16); - dbcmd.Parameters.Add(param17); - dbcmd.Parameters.Add(param18); - dbcmd.Parameters.Add(param19); - dbcmd.Parameters.Add(param20); - dbcmd.Parameters.Add(param21); - dbcmd.Parameters.Add(param22); - dbcmd.Parameters.Add(param23); - dbcmd.Parameters.Add(param24); - dbcmd.Parameters.Add(param25); - - return dbcmd; - } - - static List ComputersFromDataTable(DataTable dataTable) - { - List entries = new List(); - - foreach(DataRow dataRow in dataTable.Rows) - { - Computer entry = new Computer - { - Id = (int)dataRow["id"], - Company = (int)dataRow["company"], - Year = (int)dataRow["year"], - Model = (string)dataRow["model"], - Cpu1 = dataRow["cpu1"] == DBNull.Value ? 0 : (int)dataRow["cpu1"], - Mhz1 = dataRow["mhz1"] == DBNull.Value ? 0 : float.Parse(dataRow["mhz1"].ToString()), - Cpu2 = dataRow["cpu2"] == DBNull.Value ? 0 : (int)dataRow["cpu2"], - Mhz2 = dataRow["mhz2"] == DBNull.Value ? 0 : float.Parse(dataRow["mhz2"].ToString()), - Bits = (int)dataRow["bits"], - Ram = (int)dataRow["ram"], - Rom = (int)dataRow["rom"], - Gpu = dataRow["gpu"] == DBNull.Value ? 0 : (int)dataRow["gpu"], - Vram = (int)dataRow["vram"], - Colors = (int)dataRow["colors"], - Resolution = (string)dataRow["res"], - SoundSynth = (int)dataRow["sound_synth"], - MusicSynth = (int)dataRow["music_synth"], - SoundChannels = (int)dataRow["sound_channels"], - MusicChannels = (int)dataRow["music_channels"], - Hdd1 = (int)dataRow["hdd1"], - Hdd2 = dataRow["hdd2"] == DBNull.Value ? 0 : (int)dataRow["hdd2"], - Hdd3 = dataRow["hdd3"] == DBNull.Value ? 0 : (int)dataRow["hdd3"], - Disk1 = (int)dataRow["disk1"], - Cap1 = (string)dataRow["cap1"], - Disk2 = dataRow["disk2"] == DBNull.Value ? 0 : (int)dataRow["disk2"], - Cap2 = dataRow["cap2"] == DBNull.Value ? null : (string)dataRow["cap2"] - }; - - entries.Add(entry); - } - - return entries; + return RemoveMachine(id); } } } \ No newline at end of file diff --git a/Cicm.Database/Operations/Console.cs b/Cicm.Database/Operations/Console.cs index bcc5a198..aa608bc3 100644 --- a/Cicm.Database/Operations/Console.cs +++ b/Cicm.Database/Operations/Console.cs @@ -31,7 +31,7 @@ using System; using System.Collections.Generic; using System.Data; -using Console = Cicm.Database.Schemas.Console; +using Cicm.Database.Schemas; namespace Cicm.Database { @@ -42,31 +42,31 @@ namespace Cicm.Database /// /// All consoles /// true if is correct, false otherwise - public bool GetConsoles(out List entries) + public bool GetConsoles(out List entries) { #if DEBUG - System.Console.WriteLine("Getting all consoles..."); + Console.WriteLine("Getting all consoles..."); #endif try { - const string SQL = "SELECT * from consoles"; + string sql = $"SELECT * FROM machines WHERE type = '{(int)MachineType.Console}'"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); - dbCmd.CommandText = SQL; + dbCmd.CommandText = sql; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); - entries = ConsolesFromDataTable(dataSet.Tables[0]); + entries = MachinesFromDataTable(dataSet.Tables[0]); return true; } catch(Exception ex) { - System.Console.WriteLine("Error getting consoles."); - System.Console.WriteLine(ex); + Console.WriteLine("Error getting consoles."); + Console.WriteLine(ex); entries = null; return false; } @@ -78,31 +78,31 @@ namespace Cicm.Database /// All consoles /// Company id /// true if is correct, false otherwise - public bool GetConsoles(out List entries, int company) + public bool GetConsoles(out List entries, int company) { #if DEBUG - System.Console.WriteLine("Getting all consoles from company id {0}...", company); + Console.WriteLine("Getting all consoles from company id {0}...", company); #endif try { - const string SQL = "SELECT * from consoles WHERE company = '{id}'"; + string sql = $"SELECT * FROM machines WHERE company = '{company}' AND type = '{(int)MachineType.Console}'"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); - dbCmd.CommandText = SQL; + dbCmd.CommandText = sql; DataSet dataSet = new DataSet(); dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); - entries = ConsolesFromDataTable(dataSet.Tables[0]); + entries = MachinesFromDataTable(dataSet.Tables[0]); return true; } catch(Exception ex) { - System.Console.WriteLine("Error getting consoles."); - System.Console.WriteLine(ex); + Console.WriteLine("Error getting consoles."); + Console.WriteLine(ex); entries = null; return false; } @@ -115,15 +115,15 @@ namespace Cicm.Database /// Start of query /// How many entries to retrieve /// true if is correct, false otherwise - public bool GetConsoles(out List entries, ulong start, ulong count) + public bool GetConsoles(out List entries, ulong start, ulong count) { #if DEBUG - System.Console.WriteLine("Getting {0} consoles from {1}...", count, start); + Console.WriteLine("Getting {0} consoles from {1}...", count, start); #endif try { - string sql = $"SELECT * FROM consoles LIMIT {start}, {count}"; + string sql = $"SELECT * FROM machines WHERE type = '{(int)MachineType.Console}' LIMIT {start}, {count}"; IDbCommand dbCmd = dbCon.CreateCommand(); IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); @@ -132,14 +132,14 @@ namespace Cicm.Database dataAdapter.SelectCommand = dbCmd; dataAdapter.Fill(dataSet); - entries = ConsolesFromDataTable(dataSet.Tables[0]); + entries = MachinesFromDataTable(dataSet.Tables[0]); return true; } catch(Exception ex) { - System.Console.WriteLine("Error getting consoles."); - System.Console.WriteLine(ex); + Console.WriteLine("Error getting consoles."); + Console.WriteLine(ex); entries = null; return false; } @@ -150,33 +150,9 @@ namespace Cicm.Database /// /// Id /// Videogame console with specified id, null if not found or error - public Console GetConsole(int id) + public Machine GetConsole(int id) { - #if DEBUG - System.Console.WriteLine("Getting console with id {0}...", id); - #endif - - try - { - string sql = $"SELECT * from consoles WHERE id = '{id}'"; - - IDbCommand dbCmd = dbCon.CreateCommand(); - IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); - dbCmd.CommandText = sql; - DataSet dataSet = new DataSet(); - dataAdapter.SelectCommand = dbCmd; - dataAdapter.Fill(dataSet); - - List entries = ConsolesFromDataTable(dataSet.Tables[0]); - - return entries == null || entries.Count == 0 ? null : entries[0]; - } - catch(Exception ex) - { - System.Console.WriteLine("Error getting console."); - System.Console.WriteLine(ex); - return null; - } + return GetMachine(id); } /// @@ -186,11 +162,11 @@ namespace Cicm.Database public long CountConsoles() { #if DEBUG - System.Console.WriteLine("Counting consoles..."); + Console.WriteLine("Counting consoles..."); #endif IDbCommand dbcmd = dbCon.CreateCommand(); - dbcmd.CommandText = "SELECT COUNT(*) FROM consoles"; + dbcmd.CommandText = $"SELECT COUNT(*) FROM consoles WHERE type = '{(int)MachineType.Console}'"; object count = dbcmd.ExecuteScalar(); dbcmd.Dispose(); try { return Convert.ToInt64(count); } @@ -203,33 +179,10 @@ namespace Cicm.Database /// Entry to add /// ID of added entry /// true if added correctly, false otherwise - public bool AddConsole(Console entry, out long id) + public bool AddConsole(Machine entry, out long id) { - #if DEBUG - System.Console.Write("Adding console `{0}`...", entry.Model); - #endif - - IDbCommand dbcmd = GetCommandConsole(entry); - IDbTransaction trans = dbCon.BeginTransaction(); - dbcmd.Transaction = trans; - - const string SQL = - "INSERT INTO consoles (company, year, model, cpu1, mhz1, cpu2, mhz2, bits, ram, rom, gpu, vram, colors, res, sound_synth, music_synth, schannels, mchannels, palette, format, cap)" + - " VALUES (@company, @year, @model, @cpu1, @mhz1, @cpu2, @mhz2, @bits, @ram, @rom, @gpu, @vram, @colors, @res, @sound_synth, @music_synth, @schannels, @mchannels, @palette, @format, @cap)"; - - dbcmd.CommandText = SQL; - - dbcmd.ExecuteNonQuery(); - trans.Commit(); - dbcmd.Dispose(); - - id = dbCore.LastInsertRowId; - - #if DEBUG - System.Console.WriteLine(" id {0}", id); - #endif - - return true; + entry.Type = MachineType.Console; + return AddMachine(entry, out id); } /// @@ -237,29 +190,9 @@ namespace Cicm.Database /// /// Updated entry /// true if updated correctly, false otherwise - public bool UpdateConsole(Console entry) + public bool UpdateConsole(Machine entry) { - #if DEBUG - System.Console.WriteLine("Updating console `{0}`...", entry.Model); - #endif - - IDbCommand dbcmd = GetCommandConsole(entry); - IDbTransaction trans = dbCon.BeginTransaction(); - dbcmd.Transaction = trans; - - string sql = - "UPDATE consoles SET company = @company, year = @year, model = @model, cpu1 = @cpu1, mhz1 = @mhz1, cpu2 = @cpu2, " + - "mhz2 = @mhz2, bits = @bits, ram = @ram, rom = @rom, gpu = @gpu, vram = @vram, colors = @colors, res = @res, sound_synth = @sound_synth, music_synth = @music_synth " + - "schannels = @schannels, mchannels = @mchannels, palette = @palette, format = @format, cap = @cap " + - $"WHERE id = {entry.Id}"; - - dbcmd.CommandText = sql; - - dbcmd.ExecuteNonQuery(); - trans.Commit(); - dbcmd.Dispose(); - - return true; + return UpdateMachine(entry); } /// @@ -269,178 +202,7 @@ namespace Cicm.Database /// true if removed correctly, false otherwise public bool RemoveConsole(long id) { - #if DEBUG - System.Console.WriteLine("Removing console widh id `{0}`...", id); - #endif - - IDbCommand dbcmd = dbCon.CreateCommand(); - IDbTransaction trans = dbCon.BeginTransaction(); - dbcmd.Transaction = trans; - - string sql = $"DELETE FROM consoles WHERE id = '{id}';"; - - dbcmd.CommandText = sql; - - dbcmd.ExecuteNonQuery(); - trans.Commit(); - dbcmd.Dispose(); - - return true; - } - - IDbCommand GetCommandConsole(Console entry) - { - IDbCommand dbcmd = dbCon.CreateCommand(); - - IDbDataParameter param1 = dbcmd.CreateParameter(); - IDbDataParameter param2 = dbcmd.CreateParameter(); - IDbDataParameter param3 = dbcmd.CreateParameter(); - IDbDataParameter param4 = dbcmd.CreateParameter(); - IDbDataParameter param5 = dbcmd.CreateParameter(); - IDbDataParameter param6 = dbcmd.CreateParameter(); - IDbDataParameter param7 = dbcmd.CreateParameter(); - IDbDataParameter param8 = dbcmd.CreateParameter(); - IDbDataParameter param9 = dbcmd.CreateParameter(); - IDbDataParameter param10 = dbcmd.CreateParameter(); - IDbDataParameter param11 = dbcmd.CreateParameter(); - IDbDataParameter param12 = dbcmd.CreateParameter(); - IDbDataParameter param13 = dbcmd.CreateParameter(); - IDbDataParameter param14 = dbcmd.CreateParameter(); - IDbDataParameter param15 = dbcmd.CreateParameter(); - IDbDataParameter param16 = dbcmd.CreateParameter(); - IDbDataParameter param17 = dbcmd.CreateParameter(); - IDbDataParameter param18 = dbcmd.CreateParameter(); - IDbDataParameter param19 = dbcmd.CreateParameter(); - IDbDataParameter param20 = dbcmd.CreateParameter(); - IDbDataParameter param21 = dbcmd.CreateParameter(); - - param1.ParameterName = "@company"; - param2.ParameterName = "@year"; - param3.ParameterName = "@model"; - param4.ParameterName = "@cpu1"; - param5.ParameterName = "@mhz1"; - param6.ParameterName = "@cpu2"; - param7.ParameterName = "@mhz2"; - param8.ParameterName = "@bits"; - param9.ParameterName = "@ram"; - param10.ParameterName = "@rom"; - param11.ParameterName = "@gpu"; - param12.ParameterName = "@vram"; - param13.ParameterName = "@colors"; - param14.ParameterName = "@res"; - param15.ParameterName = "@sound_synth"; - param16.ParameterName = "@music_synth"; - param17.ParameterName = "@schannels"; - param18.ParameterName = "@mchannels"; - param19.ParameterName = "@palette"; - param20.ParameterName = "@format"; - param21.ParameterName = "@cap"; - - param1.DbType = DbType.Int32; - param2.DbType = DbType.Int32; - param3.DbType = DbType.String; - param4.DbType = DbType.Int32; - param5.DbType = DbType.Double; - param6.DbType = DbType.Int32; - param7.DbType = DbType.Double; - param8.DbType = DbType.Int32; - param9.DbType = DbType.Int32; - param10.DbType = DbType.Int32; - param11.DbType = DbType.Int32; - param12.DbType = DbType.Int32; - param13.DbType = DbType.Int32; - param14.DbType = DbType.String; - param15.DbType = DbType.Int32; - param16.DbType = DbType.Int32; - param17.DbType = DbType.Int32; - param18.DbType = DbType.Int32; - param19.DbType = DbType.Int32; - param20.DbType = DbType.Int32; - param21.DbType = DbType.Int32; - - param1.Value = entry.Company; - param2.Value = entry.Year; - param3.Value = entry.Model; - param4.Value = entry.Cpu1; - param5.Value = entry.Mhz1; - param6.Value = entry.Cpu2; - param7.Value = entry.Mhz2; - param8.Value = entry.Bits; - param9.Value = entry.Ram; - param10.Value = entry.Rom; - param11.Value = entry.Gpu; - param12.Value = entry.Vram; - param13.Value = entry.Colors; - param14.Value = entry.Resolution; - param15.Value = entry.SoundSynth; - param16.Value = entry.MusicSynth; - param17.Value = entry.SoundChannels; - param18.Value = entry.MusicChannels; - param19.Value = entry.Palette; - param20.Value = entry.Format; - param21.Value = entry.Cap; - - dbcmd.Parameters.Add(param1); - dbcmd.Parameters.Add(param2); - dbcmd.Parameters.Add(param3); - dbcmd.Parameters.Add(param4); - dbcmd.Parameters.Add(param5); - dbcmd.Parameters.Add(param6); - dbcmd.Parameters.Add(param7); - dbcmd.Parameters.Add(param8); - dbcmd.Parameters.Add(param9); - dbcmd.Parameters.Add(param10); - dbcmd.Parameters.Add(param11); - dbcmd.Parameters.Add(param12); - dbcmd.Parameters.Add(param13); - dbcmd.Parameters.Add(param14); - dbcmd.Parameters.Add(param15); - dbcmd.Parameters.Add(param16); - dbcmd.Parameters.Add(param17); - dbcmd.Parameters.Add(param18); - dbcmd.Parameters.Add(param19); - dbcmd.Parameters.Add(param20); - dbcmd.Parameters.Add(param21); - - return dbcmd; - } - - static List ConsolesFromDataTable(DataTable dataTable) - { - List entries = new List(); - - foreach(DataRow dataRow in dataTable.Rows) - { - Console entry = new Console - { - Id = (int)dataRow["id"], - Company = (int)dataRow["company"], - Year = (int)dataRow["year"], - Model = (string)dataRow["model"], - Cpu1 = dataRow["cpu1"] == DBNull.Value ? 0 : (int)dataRow["cpu1"], - Mhz1 = dataRow["mhz1"] == DBNull.Value ? 0 : float.Parse(dataRow["mhz1"].ToString()), - Cpu2 = dataRow["cpu2"] == DBNull.Value ? 0 : (int)dataRow["cpu2"], - Mhz2 = dataRow["mhz2"] == DBNull.Value ? 0 : float.Parse(dataRow["mhz2"].ToString()), - Bits = (int)dataRow["bits"], - Ram = (int)dataRow["ram"], - Rom = (int)dataRow["rom"], - Gpu = dataRow["gpu"] == DBNull.Value ? 0 : (int)dataRow["gpu"], - Vram = (int)dataRow["vram"], - Colors = (int)dataRow["colors"], - Resolution = (string)dataRow["res"], - SoundSynth = (int)dataRow["sound_synth"], - MusicSynth = (int)dataRow["music_synth"], - SoundChannels = (int)dataRow["schannels"], - MusicChannels = (int)dataRow["mchannels"], - Palette = (int)dataRow["palette"], - Format = (int)dataRow["format"], - Cap = (int)dataRow["cap"] - }; - - entries.Add(entry); - } - - return entries; + return RemoveMachine(id); } } } \ No newline at end of file diff --git a/Cicm.Database/Operations/Init.cs b/Cicm.Database/Operations/Init.cs index f3bd7c00..1dd806a3 100644 --- a/Cicm.Database/Operations/Init.cs +++ b/Cicm.Database/Operations/Init.cs @@ -49,107 +49,99 @@ namespace Cicm.Database IDbCommand dbCmd = dbCon.CreateCommand(); Console.WriteLine("Creating table `admins`"); - dbCmd.CommandText = V13.Admins; + dbCmd.CommandText = V14.Admins; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `browser_tests`"); - dbCmd.CommandText = V13.BrowserTests; + dbCmd.CommandText = V14.BrowserTests; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `cicm_db`"); - dbCmd.CommandText = V13.CicmDb; + dbCmd.CommandText = V14.CicmDb; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `companies`"); - dbCmd.CommandText = V13.Companies; + dbCmd.CommandText = V14.Companies; dbCmd.ExecuteNonQuery(); - Console.WriteLine("Creating table `computers`"); - dbCmd.CommandText = V13.Computers; - dbCmd.ExecuteNonQuery(); - - Console.WriteLine("Creating table `consoles`"); - dbCmd.CommandText = V13.Consoles; + Console.WriteLine("Creating table `machines`"); + dbCmd.CommandText = V14.Machines; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `disk_formats`"); - dbCmd.CommandText = V13.DiskFormats; + dbCmd.CommandText = V14.DiskFormats; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `forbidden`"); - dbCmd.CommandText = V13.Forbidden; + dbCmd.CommandText = V14.Forbidden; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `gpus`"); - dbCmd.CommandText = V13.Gpus; + dbCmd.CommandText = V14.Gpus; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `log`"); - dbCmd.CommandText = V13.Logs; + dbCmd.CommandText = V14.Logs; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `money_donations`"); - dbCmd.CommandText = V13.MoneyDonations; + dbCmd.CommandText = V14.MoneyDonations; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `news`"); - dbCmd.CommandText = V13.News; + dbCmd.CommandText = V14.News; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `owned_computers`"); - dbCmd.CommandText = V13.OwnedComputers; + dbCmd.CommandText = V14.OwnedComputers; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `owned_consoles`"); - dbCmd.CommandText = V13.OwnedConsoles; + dbCmd.CommandText = V14.OwnedConsoles; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `instruction_sets`"); - dbCmd.CommandText = V13.InstructionSets; + dbCmd.CommandText = V14.InstructionSets; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `instruction_set_extensions`"); - dbCmd.CommandText = V13.InstructionSetExtensions; + dbCmd.CommandText = V14.InstructionSetExtensions; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `processors`"); - dbCmd.CommandText = V13.Processors; + dbCmd.CommandText = V14.Processors; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `instruction_set_extensions_by_processor`"); - dbCmd.CommandText = V13.InstructionSetExtensionsByProcessor; + dbCmd.CommandText = V14.InstructionSetExtensionsByProcessor; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `sound_synths`"); - dbCmd.CommandText = V13.SoundSynths; + dbCmd.CommandText = V14.SoundSynths; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `iso3166_1_numeric`"); - dbCmd.CommandText = V13.Iso3166Numeric; + dbCmd.CommandText = V14.Iso3166Numeric; dbCmd.ExecuteNonQuery(); Console.WriteLine("Filling table `iso3166_1_numeric`"); - dbCmd.CommandText = V13.Iso3166NumericValues; + dbCmd.CommandText = V14.Iso3166NumericValues; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating foreign keys for table `companies`"); - dbCmd.CommandText = V13.CompaniesForeignKeys; + dbCmd.CommandText = V14.CompaniesForeignKeys; dbCmd.ExecuteNonQuery(); - Console.WriteLine("Creating foreign keys for table `computers`"); - dbCmd.CommandText = V13.ComputersForeignKeys; - dbCmd.ExecuteNonQuery(); - - Console.WriteLine("Creating foreign keys for table `consoles`"); - dbCmd.CommandText = V13.ConsolesForeignKeys; + Console.WriteLine("Creating foreign keys for table `machines`"); + dbCmd.CommandText = V14.MachinesForeignKeys; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `company_logos`"); - dbCmd.CommandText = V13.CompanyLogos; + dbCmd.CommandText = V14.CompanyLogos; dbCmd.ExecuteNonQuery(); Console.WriteLine("Creating table `company_descriptions`"); - dbCmd.CommandText = V13.CompanyDescriptions; + dbCmd.CommandText = V14.CompanyDescriptions; dbCmd.ExecuteNonQuery(); return true; diff --git a/Cicm.Database/Operations/Machine.cs b/Cicm.Database/Operations/Machine.cs new file mode 100644 index 00000000..01833661 --- /dev/null +++ b/Cicm.Database/Operations/Machine.cs @@ -0,0 +1,470 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : Machine.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Contains operations to manage machines. +// +// --[ 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 © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Data; +using Cicm.Database.Schemas; + +namespace Cicm.Database +{ + public partial class Operations + { + /// + /// Gets all machines + /// + /// All machines + /// true if is correct, false otherwise + public bool GetMachines(out List entries) + { + #if DEBUG + Console.WriteLine("Getting all machines..."); + #endif + + try + { + const string SQL = "SELECT * from machines"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = SQL; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = MachinesFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting machines."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + + /// + /// Gets all machines from specified company + /// + /// All machines + /// Company id + /// true if is correct, false otherwise + public bool GetMachines(out List entries, int company) + { + #if DEBUG + Console.WriteLine("Getting all machines from company id {0}...", company); + #endif + + try + { + const string SQL = "SELECT * from machines WHERE company = '{id}'"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = SQL; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = MachinesFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting machines."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + + /// + /// Gets the specified number of machines since the specified start + /// + /// List of machines + /// Start of query + /// How many entries to retrieve + /// true if is correct, false otherwise + public bool GetMachines(out List entries, ulong start, ulong count) + { + #if DEBUG + Console.WriteLine("Getting {0} machines from {1}...", count, start); + #endif + + try + { + string sql = $"SELECT * FROM machines LIMIT {start}, {count}"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = MachinesFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting machines."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + + /// + /// Gets machine by specified id + /// + /// Id + /// Machine with specified id, null if not found or error + public Machine GetMachine(int id) + { + #if DEBUG + Console.WriteLine("Getting machine with id {0}...", id); + #endif + + try + { + string sql = $"SELECT * from machines WHERE id = '{id}'"; + + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = sql; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + List entries = MachinesFromDataTable(dataSet.Tables[0]); + + return entries == null || entries.Count == 0 ? null : entries[0]; + } + catch(Exception ex) + { + Console.WriteLine("Error getting machine."); + Console.WriteLine(ex); + return null; + } + } + + /// + /// Counts the number of machines in the database + /// + /// Entries in database + public long CountMachines() + { + #if DEBUG + Console.WriteLine("Counting machines..."); + #endif + + IDbCommand dbcmd = dbCon.CreateCommand(); + dbcmd.CommandText = "SELECT COUNT(*) FROM machines"; + object count = dbcmd.ExecuteScalar(); + dbcmd.Dispose(); + try { return Convert.ToInt64(count); } + catch { return 0; } + } + + /// + /// Adds a new administrator to the database + /// + /// Entry to add + /// ID of added entry + /// true if added correctly, false otherwise + public bool AddMachine(Machine entry, out long id) + { + #if DEBUG + Console.Write("Adding machine `{0}`...", entry.Model); + #endif + + IDbCommand dbcmd = GetCommandMachine(entry); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + const string SQL = + "INSERT INTO machines (company, year, model, cpu1, mhz1, cpu2, mhz2, ram, rom, gpu, vram, colors, res, sound_synth, music_synth, sound_channels, music_channels, hdd1, hdd2, hdd3, disk1, cap1, disk2, cap2, type)" + + " VALUES (@company, @year, @model, @cpu1, @mhz1, @cpu2, @mhz2, @ram, @rom, @gpu, @vram, @colors, @res, @sound_synth, @music_synth, @sound_channels, @music_channels, @hdd1, @hdd2, @hdd3, @disk1, @cap1, @disk2, @cap2, @type)"; + + dbcmd.CommandText = SQL; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + id = dbCore.LastInsertRowId; + + #if DEBUG + Console.WriteLine(" id {0}", id); + #endif + + return true; + } + + /// + /// Updated a machine in the database + /// + /// Updated entry + /// true if updated correctly, false otherwise + public bool UpdateMachine(Machine entry) + { + #if DEBUG + Console.WriteLine("Updating machine `{0}`...", entry.Model); + #endif + + IDbCommand dbcmd = GetCommandMachine(entry); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = + "UPDATE machines SET company = @company, year = @year, model = @model, cpu1 = @cpu1, mhz1 = @mhz1, cpu2 = @cpu2, " + + "mhz2 = @mhz2, ram = @ram, rom = @rom, gpu = @gpu, vram = @vram, colors = @colors, res = @res, sound_synth = @sound_synth, music_synth = @music_synth " + + "sound_channels = @sound_channels, music_channels = @music_channels, hdd1 = @hdd1, hdd2 = @hdd2, hdd3 = @hdd3, disk1 = @disk1, cap1 = @cap1, disk2 = @disk2, cap2 = @cap2, type = @type " + + $"WHERE id = {entry.Id}"; + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } + + /// + /// Removes a machine from the database + /// + /// ID of entry to remove + /// true if removed correctly, false otherwise + public bool RemoveMachine(long id) + { + #if DEBUG + Console.WriteLine("Removing machine widh id `{0}`...", id); + #endif + + IDbCommand dbcmd = dbCon.CreateCommand(); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = $"DELETE FROM machines WHERE id = '{id}';"; + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } + + IDbCommand GetCommandMachine(Machine entry) + { + IDbCommand dbcmd = dbCon.CreateCommand(); + + IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param2 = dbcmd.CreateParameter(); + IDbDataParameter param3 = dbcmd.CreateParameter(); + IDbDataParameter param4 = dbcmd.CreateParameter(); + IDbDataParameter param5 = dbcmd.CreateParameter(); + IDbDataParameter param6 = dbcmd.CreateParameter(); + IDbDataParameter param7 = dbcmd.CreateParameter(); + IDbDataParameter param8 = dbcmd.CreateParameter(); + IDbDataParameter param9 = dbcmd.CreateParameter(); + IDbDataParameter param10 = dbcmd.CreateParameter(); + IDbDataParameter param11 = dbcmd.CreateParameter(); + IDbDataParameter param12 = dbcmd.CreateParameter(); + IDbDataParameter param13 = dbcmd.CreateParameter(); + IDbDataParameter param14 = dbcmd.CreateParameter(); + IDbDataParameter param15 = dbcmd.CreateParameter(); + IDbDataParameter param16 = dbcmd.CreateParameter(); + IDbDataParameter param17 = dbcmd.CreateParameter(); + IDbDataParameter param18 = dbcmd.CreateParameter(); + IDbDataParameter param19 = dbcmd.CreateParameter(); + IDbDataParameter param20 = dbcmd.CreateParameter(); + IDbDataParameter param21 = dbcmd.CreateParameter(); + IDbDataParameter param22 = dbcmd.CreateParameter(); + IDbDataParameter param23 = dbcmd.CreateParameter(); + IDbDataParameter param24 = dbcmd.CreateParameter(); + IDbDataParameter param25 = dbcmd.CreateParameter(); + + param1.ParameterName = "@company"; + param2.ParameterName = "@year"; + param3.ParameterName = "@model"; + param4.ParameterName = "@cpu1"; + param5.ParameterName = "@mhz1"; + param6.ParameterName = "@cpu2"; + param7.ParameterName = "@mhz2"; + param8.ParameterName = "@ram"; + param9.ParameterName = "@rom"; + param10.ParameterName = "@gpu"; + param11.ParameterName = "@vram"; + param12.ParameterName = "@colors"; + param13.ParameterName = "@res"; + param14.ParameterName = "@sound_synth"; + param15.ParameterName = "@music_synth"; + param16.ParameterName = "@sound_channels"; + param17.ParameterName = "@music_channels"; + param18.ParameterName = "@hdd1"; + param19.ParameterName = "@hdd2"; + param20.ParameterName = "@hdd3"; + param21.ParameterName = "@disk1"; + param22.ParameterName = "@cap1"; + param23.ParameterName = "@disk2"; + param24.ParameterName = "@cap2"; + param25.ParameterName = "@type"; + + param1.DbType = DbType.Int32; + param2.DbType = DbType.Int32; + param3.DbType = DbType.String; + param4.DbType = DbType.Int32; + param5.DbType = DbType.Double; + param6.DbType = DbType.Int32; + param7.DbType = DbType.Double; + param8.DbType = DbType.Int32; + param9.DbType = DbType.Int32; + param10.DbType = DbType.Int32; + param11.DbType = DbType.Int32; + param12.DbType = DbType.Int32; + param13.DbType = DbType.String; + param14.DbType = DbType.Int32; + param15.DbType = DbType.Int32; + param16.DbType = DbType.Int32; + param17.DbType = DbType.Int32; + param18.DbType = DbType.Int32; + param19.DbType = DbType.Int32; + param20.DbType = DbType.Int32; + param21.DbType = DbType.Int32; + param22.DbType = DbType.String; + param23.DbType = DbType.Int32; + param24.DbType = DbType.String; + param25.DbType = DbType.Int32; + + param1.Value = entry.Company; + param2.Value = entry.Year; + param3.Value = entry.Model; + param4.Value = entry.Cpu1; + param5.Value = entry.Mhz1; + param6.Value = entry.Cpu2; + param7.Value = entry.Mhz2; + param8.Value = entry.Ram; + param9.Value = entry.Rom; + param10.Value = entry.Gpu; + param11.Value = entry.Vram; + param12.Value = entry.Colors; + param13.Value = entry.Resolution; + param14.Value = entry.SoundSynth; + param15.Value = entry.MusicSynth; + param16.Value = entry.SoundChannels; + param17.Value = entry.MusicChannels; + param18.Value = entry.Hdd1; + param19.Value = entry.Hdd2; + param20.Value = entry.Hdd3; + param21.Value = entry.Disk1; + param22.Value = entry.Cap1; + param23.Value = entry.Disk2; + param24.Value = entry.Cap2; + param25.Value = entry.Type; + + dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + dbcmd.Parameters.Add(param4); + dbcmd.Parameters.Add(param5); + dbcmd.Parameters.Add(param6); + dbcmd.Parameters.Add(param7); + dbcmd.Parameters.Add(param8); + dbcmd.Parameters.Add(param9); + dbcmd.Parameters.Add(param10); + dbcmd.Parameters.Add(param11); + dbcmd.Parameters.Add(param12); + dbcmd.Parameters.Add(param13); + dbcmd.Parameters.Add(param14); + dbcmd.Parameters.Add(param15); + dbcmd.Parameters.Add(param16); + dbcmd.Parameters.Add(param17); + dbcmd.Parameters.Add(param18); + dbcmd.Parameters.Add(param19); + dbcmd.Parameters.Add(param20); + dbcmd.Parameters.Add(param21); + dbcmd.Parameters.Add(param22); + dbcmd.Parameters.Add(param23); + dbcmd.Parameters.Add(param24); + dbcmd.Parameters.Add(param25); + + return dbcmd; + } + + static List MachinesFromDataTable(DataTable dataTable) + { + List entries = new List(); + + foreach(DataRow dataRow in dataTable.Rows) + { + Machine entry = new Machine + { + Id = (int)dataRow["id"], + Company = (int)dataRow["company"], + Year = (int)dataRow["year"], + Model = (string)dataRow["model"], + Cpu1 = dataRow["cpu1"] == DBNull.Value ? 0 : (int)dataRow["cpu1"], + Mhz1 = dataRow["mhz1"] == DBNull.Value ? 0 : float.Parse(dataRow["mhz1"].ToString()), + Cpu2 = dataRow["cpu2"] == DBNull.Value ? 0 : (int)dataRow["cpu2"], + Mhz2 = dataRow["mhz2"] == DBNull.Value ? 0 : float.Parse(dataRow["mhz2"].ToString()), + Ram = (int)dataRow["ram"], + Rom = (int)dataRow["rom"], + Gpu = dataRow["gpu"] == DBNull.Value ? 0 : (int)dataRow["gpu"], + Vram = (int)dataRow["vram"], + Colors = (int)dataRow["colors"], + Resolution = (string)dataRow["res"], + SoundSynth = (int)dataRow["sound_synth"], + MusicSynth = (int)dataRow["music_synth"], + SoundChannels = (int)dataRow["sound_channels"], + MusicChannels = (int)dataRow["music_channels"], + Hdd1 = (int)dataRow["hdd1"], + Hdd2 = dataRow["hdd2"] == DBNull.Value ? 0 : (int)dataRow["hdd2"], + Hdd3 = dataRow["hdd3"] == DBNull.Value ? 0 : (int)dataRow["hdd3"], + Disk1 = (int)dataRow["disk1"], + Cap1 = (string)dataRow["cap1"], + Disk2 = dataRow["disk2"] == DBNull.Value ? 0 : (int)dataRow["disk2"], + Cap2 = dataRow["cap2"] == DBNull.Value ? null : (string)dataRow["cap2"], + Type = (MachineType)dataRow["type"] + }; + + entries.Add(entry); + } + + return entries; + } + } +} \ No newline at end of file diff --git a/Cicm.Database/Operations/Operations.cs b/Cicm.Database/Operations/Operations.cs index 8f0ba67c..7077e5f2 100644 --- a/Cicm.Database/Operations/Operations.cs +++ b/Cicm.Database/Operations/Operations.cs @@ -35,7 +35,7 @@ namespace Cicm.Database public partial class Operations { /// Last known database version - const int DB_VERSION = 13; + const int DB_VERSION = 14; /// The column with this value indicates there is no item of this type. public const int DB_NONE = -1; /// This value indicates there's no GPU, but a direct memory->display connection (a framebuffer). diff --git a/Cicm.Database/Operations/Update.cs b/Cicm.Database/Operations/Update.cs index 29fc7a35..ed9c790f 100644 --- a/Cicm.Database/Operations/Update.cs +++ b/Cicm.Database/Operations/Update.cs @@ -32,6 +32,7 @@ using System; using System.Collections.Generic; using System.Data; using System.IO; +using Cicm.Database.Schemas; using Cicm.Database.Schemas.Sql; namespace Cicm.Database @@ -133,6 +134,11 @@ namespace Cicm.Database UpdateDatabaseToV13(); break; } + case 13: + { + UpdateDatabaseToV14(); + break; + } } OptimizeDatabase(); @@ -1290,6 +1296,281 @@ namespace Cicm.Database dbCmd.Dispose(); } + void UpdateDatabaseToV14() + { + Console.WriteLine("Updating database to version 14"); + + Console.WriteLine("Renaming table `computers` to `machines`"); + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbTransaction trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = "ALTER TABLE `computers` RENAME TO `machines`;"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Removing column `bits` from table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = "ALTER TABLE `machines` DROP COLUMN `bits`;"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Creating column `type` in table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = + $"ALTER TABLE `machines` ADD COLUMN `type` INT NOT NULL DEFAULT '{(int)MachineType.Unknown}';"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Updating all entries in table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = $"UPDATE `machines` SET `type` = '{(int)MachineType.Computer}';"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Renaming all indexes on table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = + "ALTER TABLE `machines` DROP INDEX `idx_computers_company`, ADD INDEX `idx_machines_company` (`company`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_year`, ADD INDEX `idx_machines_year` (`year`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_model`, ADD INDEX `idx_machines_model` (`model`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_cpu1`, ADD INDEX `idx_machines_cpu1` (`cpu1`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_cpu2`, ADD INDEX `idx_machines_cpu2` (`cpu2`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_mhz1`, ADD INDEX `idx_machines_mhz1` (`mhz1`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_mhz2`, ADD INDEX `idx_machines_mhz2` (`mhz2`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_ram`, ADD INDEX `idx_machines_ram` (`ram`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_rom`, ADD INDEX `idx_machines_rom` (`rom`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_gpu`, ADD INDEX `idx_machines_gpu` (`gpu`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_vram`, ADD INDEX `idx_machines_vram` (`vram`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_colors`, ADD INDEX `idx_machines_colors` (`colors`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_res`, ADD INDEX `idx_machines_res` (`res`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_sound_synth`, ADD INDEX `idx_machines_sound_synth` (`sound_synth`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_music_synth`, ADD INDEX `idx_machines_music_synth` (`music_synth`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_hdd1`, ADD INDEX `idx_machines_hdd1` (`hdd1`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_hdd2`, ADD INDEX `idx_machines_hdd2` (`hdd2`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_hdd3`, ADD INDEX `idx_machines_hdd3` (`hdd3`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_disk1`, ADD INDEX `idx_machines_disk1` (`disk1`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_disk2`, ADD INDEX `idx_machines_disk2` (`disk2`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_cap1`, ADD INDEX `idx_machines_cap1` (`cap1`);\n" + + "ALTER TABLE `machines` DROP INDEX `idx_computers_cap2`, ADD INDEX `idx_machines_cap2` (`cap2`);"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Removing old foreign keys from table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_company`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_cpu1`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_cpu2`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_disk1`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_disk2`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_gpu`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_hdd1`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_hdd2`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_hdd3`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_music_synth`;\n" + + "ALTER TABLE `machines` DROP FOREIGN KEY `fk_computers_sound_synth`;"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Adding foreign keys in table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_company` (company) REFERENCES `companies` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_cpu1` (cpu1) REFERENCES `processors` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_cpu2` (cpu2) REFERENCES `processors` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_disk1` (disk1) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_disk2` (disk2) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_gpu` (gpu) REFERENCES `gpus` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_hdd1` (hdd1) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_hdd2` (hdd2) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_hdd3` (hdd3) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_music_synth` (music_synth) REFERENCES `sound_synths` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_sound_synth` (sound_synth) REFERENCES `sound_synths` (`id`) ON UPDATE CASCADE;"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Adding new index for `type` in table `machines`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = "CREATE INDEX `idx_machines_type` ON `machines` (`type`);"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Getting all items from `consoles`"); + + dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = "SELECT * from consoles"; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + foreach(DataRow dataRow in dataSet.Tables[0].Rows) + { + IDbCommand dbcmd = dbCon.CreateCommand(); + + IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param2 = dbcmd.CreateParameter(); + IDbDataParameter param3 = dbcmd.CreateParameter(); + IDbDataParameter param4 = dbcmd.CreateParameter(); + IDbDataParameter param5 = dbcmd.CreateParameter(); + IDbDataParameter param6 = dbcmd.CreateParameter(); + IDbDataParameter param7 = dbcmd.CreateParameter(); + IDbDataParameter param8 = dbcmd.CreateParameter(); + IDbDataParameter param9 = dbcmd.CreateParameter(); + IDbDataParameter param10 = dbcmd.CreateParameter(); + IDbDataParameter param11 = dbcmd.CreateParameter(); + IDbDataParameter param12 = dbcmd.CreateParameter(); + IDbDataParameter param13 = dbcmd.CreateParameter(); + IDbDataParameter param14 = dbcmd.CreateParameter(); + IDbDataParameter param15 = dbcmd.CreateParameter(); + IDbDataParameter param16 = dbcmd.CreateParameter(); + IDbDataParameter param17 = dbcmd.CreateParameter(); + IDbDataParameter param18 = dbcmd.CreateParameter(); + IDbDataParameter param19 = dbcmd.CreateParameter(); + IDbDataParameter param20 = dbcmd.CreateParameter(); + IDbDataParameter param21 = dbcmd.CreateParameter(); + + param1.ParameterName = "@company"; + param2.ParameterName = "@year"; + param3.ParameterName = "@model"; + param4.ParameterName = "@cpu1"; + param5.ParameterName = "@mhz1"; + param6.ParameterName = "@cpu2"; + param7.ParameterName = "@mhz2"; + param8.ParameterName = "@ram"; + param9.ParameterName = "@rom"; + param10.ParameterName = "@gpu"; + param11.ParameterName = "@vram"; + param12.ParameterName = "@colors"; + param13.ParameterName = "@res"; + param14.ParameterName = "@sound_synth"; + param15.ParameterName = "@music_synth"; + param16.ParameterName = "@sound_channels"; + param17.ParameterName = "@music_channels"; + param18.ParameterName = "@type"; + param19.ParameterName = "@hdd1"; + param20.ParameterName = "@disk1"; + param21.ParameterName = "@cap1"; + + param1.DbType = DbType.Int32; + param2.DbType = DbType.Int32; + param3.DbType = DbType.String; + param4.DbType = DbType.Int32; + param5.DbType = DbType.Double; + param6.DbType = DbType.Int32; + param7.DbType = DbType.Double; + param8.DbType = DbType.Int32; + param9.DbType = DbType.Int32; + param10.DbType = DbType.Int32; + param11.DbType = DbType.Int32; + param12.DbType = DbType.Int32; + param13.DbType = DbType.String; + param14.DbType = DbType.Int32; + param15.DbType = DbType.Int32; + param16.DbType = DbType.Int32; + param17.DbType = DbType.Int32; + param18.DbType = DbType.Int32; + param19.DbType = DbType.Int32; + param20.DbType = DbType.Int32; + param21.DbType = DbType.Int32; + + param1.Value = (int)dataRow["company"]; + param2.Value = (int)dataRow["year"]; + param3.Value = (string)dataRow["model"]; + param4.Value = dataRow["cpu1"] == DBNull.Value ? (object)null : (int)dataRow["cpu1"]; + param5.Value = dataRow["mhz1"] == DBNull.Value ? (object)null : float.Parse(dataRow["mhz1"].ToString()); + param6.Value = dataRow["cpu2"] == DBNull.Value ? (object)null : (int)dataRow["cpu2"]; + param7.Value = dataRow["mhz2"] == DBNull.Value ? (object)null : float.Parse(dataRow["mhz2"].ToString()); + param8.Value = (int)dataRow["ram"]; + param9.Value = (int)dataRow["rom"]; + param10.Value = dataRow["gpu"] == DBNull.Value ? (object)null : (int)dataRow["gpu"]; + param11.Value = (int)dataRow["vram"]; + param12.Value = (int)dataRow["colors"]; + param13.Value = (string)dataRow["res"]; + param14.Value = (int)dataRow["sound_synth"]; + param15.Value = (int)dataRow["music_synth"]; + param16.Value = (int)dataRow["schannels"]; + param17.Value = (int)dataRow["mchannels"]; + param18.Value = MachineType.Console; + param19.Value = 30; + param20.Value = 30; + param21.Value = 0; + + dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + dbcmd.Parameters.Add(param4); + dbcmd.Parameters.Add(param5); + dbcmd.Parameters.Add(param6); + dbcmd.Parameters.Add(param7); + dbcmd.Parameters.Add(param8); + dbcmd.Parameters.Add(param9); + dbcmd.Parameters.Add(param10); + dbcmd.Parameters.Add(param11); + dbcmd.Parameters.Add(param12); + dbcmd.Parameters.Add(param13); + dbcmd.Parameters.Add(param14); + dbcmd.Parameters.Add(param15); + dbcmd.Parameters.Add(param16); + dbcmd.Parameters.Add(param17); + dbcmd.Parameters.Add(param18); + dbcmd.Parameters.Add(param19); + dbcmd.Parameters.Add(param20); + dbcmd.Parameters.Add(param21); + + trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + Console.WriteLine("Converting console \"{0}\" to machine", (string)param3.Value); + + const string SQL = + "INSERT INTO machines (company, year, model, cpu1, mhz1, cpu2, mhz2, ram, rom, gpu, vram, colors, res, sound_synth, music_synth, sound_channels, music_channels, type, hdd1, disk1, cap1)" + + " VALUES (@company, @year, @model, @cpu1, @mhz1, @cpu2, @mhz2, @ram, @rom, @gpu, @vram, @colors, @res, @sound_synth, @music_synth, @sound_channels, @music_channels, @type, @hdd1, @disk1, @cap1)"; + + dbcmd.CommandText = SQL; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + } + + Console.WriteLine("Dropping table `consoles`"); + dbCmd = dbCon.CreateCommand(); + trans = dbCon.BeginTransaction(); + dbCmd.Transaction = trans; + dbCmd.CommandText = "DROP TABLE `consoles`;"; + dbCmd.ExecuteNonQuery(); + trans.Commit(); + dbCmd.Dispose(); + + Console.WriteLine("Setting new database version to 14..."); + dbCmd = dbCon.CreateCommand(); + dbCmd.CommandText = "INSERT INTO cicm_db (version) VALUES ('14')"; + dbCmd.ExecuteNonQuery(); + dbCmd.Dispose(); + } + void OptimizeDatabase() { IDbCommand dbCmd = dbCon.CreateCommand(); diff --git a/Cicm.Database/Schemas/Console.cs b/Cicm.Database/Schemas/Console.cs deleted file mode 100644 index abb2e757..00000000 --- a/Cicm.Database/Schemas/Console.cs +++ /dev/null @@ -1,80 +0,0 @@ -/****************************************************************************** -// Canary Islands Computer Museum Website -// ---------------------------------------------------------------------------- -// -// Filename : Console.cs -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// High level representation of a videogame console. -// -// --[ 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 © 2003-2018 Natalia Portillo -*******************************************************************************/ - -namespace Cicm.Database.Schemas -{ - public class Console - { - /// Bits of GPRs of primary CPU - public int Bits; - /// Capacity in kibibytes of storage format - public int Cap; - /// Maximum colors on screen - public int Colors; - /// Manufacturer's company ID - public int Company; - /// Primary CPU - public int Cpu1; - /// Secondary CPU - public int Cpu2; - /// ID of storage format - public int Format; - /// ID of GPU - public int Gpu; - /// ID - public int Id; - /// Frequency in MHz of primary CPU - public float Mhz1; - /// Frequency in MHz of secondary CPU - public float Mhz2; - /// Model name - public string Model; - /// Audio channels supported by the MPU - public int MusicChannels; - /// ID of MPU - public int MusicSynth; - /// Colors on palette - public int Palette; - /// Size in kibibytes of program RAM - public int Ram; - /// Resolution in WxH pixels - public string Resolution; - /// Size in kibibytes of firmware - public int Rom; - /// Audio channels supported by the DSP - public int SoundChannels; - /// ID of DSP - public int SoundSynth; - /// Size in kibibytes for video RAM - public int Vram; - /// Introduction date, 0 if unknown, 1000 if prototype - public int Year; - } -} \ No newline at end of file diff --git a/Cicm.Database/Schemas/Enums.cs b/Cicm.Database/Schemas/Enums.cs index a6da23ab..27271871 100644 --- a/Cicm.Database/Schemas/Enums.cs +++ b/Cicm.Database/Schemas/Enums.cs @@ -67,4 +67,14 @@ namespace Cicm.Database.Schemas /// Company renamed possibly with a change of intentions Renamed = 6 } + + public enum MachineType + { + /// Unknown machine type, should not happen + Unknown = 0, + /// Computer + Computer = 1, + /// Videogame console + Console = 2 + } } \ No newline at end of file diff --git a/Cicm.Database/Schemas/Computer.cs b/Cicm.Database/Schemas/Machine.cs similarity index 97% rename from Cicm.Database/Schemas/Computer.cs rename to Cicm.Database/Schemas/Machine.cs index a9ae1ccf..b9946db0 100644 --- a/Cicm.Database/Schemas/Computer.cs +++ b/Cicm.Database/Schemas/Machine.cs @@ -31,10 +31,8 @@ namespace Cicm.Database.Schemas { /// Computer - public class Computer + public class Machine { - /// Bits of GPRs of primary CPU - public int Bits; /// Capacity of first removable disk format public string Cap1; /// Capacity of second removable disk format @@ -81,6 +79,8 @@ namespace Cicm.Database.Schemas public int SoundChannels; /// ID of DSP public int SoundSynth; + /// Machine type + public MachineType Type; /// Size in kibibytes for video RAM public int Vram; /// Introduction date, 0 if unknown, 1000 if prototype diff --git a/Cicm.Database/Schemas/Sql/V14.cs b/Cicm.Database/Schemas/Sql/V14.cs new file mode 100644 index 00000000..8770e0f7 --- /dev/null +++ b/Cicm.Database/Schemas/Sql/V14.cs @@ -0,0 +1,149 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : V14.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Contains SQL queries to create the database version 7. +// +// --[ 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 © 2003-2018 Natalia Portillo +*******************************************************************************/ + +namespace Cicm.Database.Schemas.Sql +{ + public static class V14 + { + public static readonly string Admins = V13.Admins; + + public static readonly string BrowserTests = V13.BrowserTests; + + public static readonly string CicmDb = "CREATE TABLE `cicm_db` (\n" + + "`id` int(11) NOT NULL AUTO_INCREMENT,\n" + + "`version` int(11) NOT NULL,\n" + + "`updated` datetime DEFAULT CURRENT_TIMESTAMP,\n" + + "PRIMARY KEY (`id`)\n" + ");\n" + + "INSERT INTO cicm_db (version) VALUES ('14');"; + + public static readonly string Companies = V13.Companies; + + public static readonly string Machines = "CREATE TABLE `machines` (;\n" + + "`id` int(11) NOT NULL AUTO_INCREMENT,;\n" + + "`company` int(11) NOT NULL DEFAULT '0',;\n" + + "`year` int(11) NOT NULL DEFAULT '0',;\n" + + "`model` char(50) NOT NULL DEFAULT '',;\n" + + "`cpu1` int(11) DEFAULT NULL,;\n" + + "`mhz1` int(11) DEFAULT NULL,;\n" + + "`cpu2` int(11) DEFAULT NULL,;\n" + + "`mhz2` decimal(11,2) DEFAULT NULL,;\n" + + "`ram` int(11) NOT NULL DEFAULT '0',;\n" + + "`rom` int(11) NOT NULL DEFAULT '0',;\n" + + "`gpu` int(11) DEFAULT NULL,;\n" + + "`vram` int(11) NOT NULL DEFAULT '0',;\n" + + "`colors` int(11) NOT NULL DEFAULT '0',;\n" + + "`res` char(10) NOT NULL DEFAULT '',;\n" + + "`sound_synth` int(11) NOT NULL DEFAULT '0',;\n" + + "`music_synth` int(11) NOT NULL DEFAULT '0',;\n" + + "`sound_channels` int(11) NOT NULL DEFAULT '0',;\n" + + "`music_channels` int(11) NOT NULL DEFAULT '0',;\n" + + "`hdd1` int(11) NOT NULL DEFAULT '0',;\n" + + "`hdd2` int(11) DEFAULT NULL,;\n" + + "`hdd3` int(11) DEFAULT NULL,;\n" + + "`disk1` int(11) NOT NULL DEFAULT '0',;\n" + + "`cap1` char(25) NOT NULL DEFAULT '0',;\n" + + "`disk2` int(11) DEFAULT NULL,;\n" + + "`cap2` char(25) DEFAULT NULL,;\n" + + "`type` int(11) NOT NULL DEFAULT '0',;\n" + + "PRIMARY KEY (`id`),;\n" + + "KEY `idx_machines_company` (`company`),;\n" + + "KEY `idx_machines_year` (`year`),;\n" + + "KEY `idx_machines_model` (`model`),;\n" + + "KEY `idx_machines_cpu1` (`cpu1`),;\n" + + "KEY `idx_machines_cpu2` (`cpu2`),;\n" + + "KEY `idx_machines_mhz1` (`mhz1`),;\n" + + "KEY `idx_machines_mhz2` (`mhz2`),;\n" + + "KEY `idx_machines_ram` (`ram`),;\n" + + "KEY `idx_machines_rom` (`rom`),;\n" + + "KEY `idx_machines_gpu` (`gpu`),;\n" + + "KEY `idx_machines_vram` (`vram`),;\n" + + "KEY `idx_machines_colors` (`colors`),;\n" + + "KEY `idx_machines_res` (`res`),;\n" + + "KEY `idx_machines_sound_synth` (`sound_synth`),;\n" + + "KEY `idx_machines_music_synth` (`music_synth`),;\n" + + "KEY `idx_machines_hdd1` (`hdd1`),;\n" + + "KEY `idx_machines_hdd2` (`hdd2`),;\n" + + "KEY `idx_machines_hdd3` (`hdd3`),;\n" + + "KEY `idx_machines_disk1` (`disk1`),;\n" + + "KEY `idx_machines_disk2` (`disk2`),;\n" + + "KEY `idx_machines_cap1` (`cap1`),;\n" + + "KEY `idx_machines_cap2` (`cap2`),;\n" + + "KEY `idx_machines_type` (`type`));"; + + public static readonly string DiskFormats = V13.DiskFormats; + + public static readonly string Forbidden = V13.Forbidden; + + public static readonly string Gpus = V13.Gpus; + + public static readonly string Logs = V13.Logs; + + public static readonly string MoneyDonations = V13.MoneyDonations; + + public static readonly string News = V13.News; + + public static readonly string OwnedComputers = V13.OwnedComputers; + + public static readonly string OwnedConsoles = V13.OwnedConsoles; + + public static readonly string Processors = V13.Processors; + + public static readonly string SoundSynths = V13.SoundSynths; + + public static readonly string MachinesForeignKeys = V13.ComputersForeignKeys; + + public static readonly string Iso3166Numeric = V13.Iso3166Numeric; + + public static readonly string Iso3166NumericValues = V13.Iso3166NumericValues; + + public static readonly string CompaniesForeignKeys = + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_company` (company) REFERENCES `companies` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_cpu1` (cpu1) REFERENCES `processors` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_cpu2` (cpu2) REFERENCES `processors` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_disk1` (disk1) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_disk2` (disk2) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_gpu` (gpu) REFERENCES `gpus` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_hdd1` (hdd1) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_hdd2` (hdd2) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_hdd3` (hdd3) REFERENCES `disk_formats` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_music_synth` (music_synth) REFERENCES `sound_synths` (`id`) ON UPDATE CASCADE;\n" + + "ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_sound_synth` (sound_synth) REFERENCES `sound_synths` (`id`) ON UPDATE CASCADE;"; + + public static readonly string CompanyLogos = V13.CompanyLogos; + + public static readonly string CompanyDescriptions = V13.CompanyDescriptions; + + public static readonly string InstructionSets = V13.InstructionSets; + + public static readonly string InstructionSetExtensions = V13.InstructionSetExtensions; + + public static readonly string InstructionSetExtensionsByProcessor = V13.InstructionSetExtensionsByProcessor; + } +} \ No newline at end of file diff --git a/cicm_web/Controllers/ComputerController.cs b/cicm_web/Controllers/ComputerController.cs index 4657d80d..72556d8a 100644 --- a/cicm_web/Controllers/ComputerController.cs +++ b/cicm_web/Controllers/ComputerController.cs @@ -33,7 +33,7 @@ using System.Linq; using cicm_web.Models; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; -using Computer = Cicm.Database.Schemas.Computer; +using Machine = Cicm.Database.Schemas.Machine; namespace cicm_web.Controllers { @@ -48,7 +48,7 @@ namespace cicm_web.Controllers public IActionResult Index() { - Program.Database.Operations.GetComputers(out List computers); + Program.Database.Operations.GetComputers(out List computers); ViewBag.ItemCount = computers.Count; @@ -67,7 +67,7 @@ namespace cicm_web.Controllers ViewBag.Letter = id; - ComputerMini[] computers = + MachineMini[] computers = id == '\0' ? ComputerMini.GetAllItems() : ComputerMini.GetItemsStartingWithLetter(id); return View(computers); @@ -79,12 +79,5 @@ namespace cicm_web.Controllers return View(ComputerMini.GetItemsFromYear(id)); } - - public IActionResult View(int id) - { - ViewBag.WebRootPath = hostingEnvironment.WebRootPath; - - return View(Models.Computer.GetItem(id)); - } } } \ No newline at end of file diff --git a/cicm_web/Controllers/ConsoleController.cs b/cicm_web/Controllers/ConsoleController.cs index e3580f3b..d9efd180 100644 --- a/cicm_web/Controllers/ConsoleController.cs +++ b/cicm_web/Controllers/ConsoleController.cs @@ -7,7 +7,7 @@ // // --[ Description ] ---------------------------------------------------------- // -// Videograme console controller +// Videogame console controller // // --[ License ] -------------------------------------------------------------- // @@ -33,7 +33,7 @@ using System.Linq; using cicm_web.Models; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; -using Computer = Cicm.Database.Schemas.Computer; +using Machine = Cicm.Database.Schemas.Machine; namespace cicm_web.Controllers { @@ -48,7 +48,7 @@ namespace cicm_web.Controllers public IActionResult Index() { - Program.Database.Operations.GetConsoles(out List consoles); + Program.Database.Operations.GetConsoles(out List consoles); ViewBag.ItemCount = consoles.Count; @@ -67,7 +67,7 @@ namespace cicm_web.Controllers ViewBag.Letter = id; - ConsoleMini[] consoles = + MachineMini[] consoles = id == '\0' ? ConsoleMini.GetAllItems() : ConsoleMini.GetItemsStartingWithLetter(id); return View(consoles); @@ -79,12 +79,5 @@ namespace cicm_web.Controllers return View(ConsoleMini.GetItemsFromYear(id)); } - - public IActionResult View(int id) - { - ViewBag.WebRootPath = hostingEnvironment.WebRootPath; - - return View(Models.Console.GetItem(id)); - } } } \ No newline at end of file diff --git a/cicm_web/Controllers/MachineController.cs b/cicm_web/Controllers/MachineController.cs new file mode 100644 index 00000000..e4393a2f --- /dev/null +++ b/cicm_web/Controllers/MachineController.cs @@ -0,0 +1,53 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : ConsoleController.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Machine controller +// +// --[ 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 © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using cicm_web.Models; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; + +namespace cicm_web.Controllers +{ + public class MachineController : Controller + { + readonly IHostingEnvironment hostingEnvironment; + + public MachineController(IHostingEnvironment env) + { + hostingEnvironment = env; + } + + public IActionResult View(int id) + { + ViewBag.WebRootPath = hostingEnvironment.WebRootPath; + + return View(Machine.GetItem(id)); + } + } +} \ No newline at end of file diff --git a/cicm_web/Models/Company.cs b/cicm_web/Models/Company.cs index bd651f6e..b2b36636 100644 --- a/cicm_web/Models/Company.cs +++ b/cicm_web/Models/Company.cs @@ -40,8 +40,8 @@ namespace cicm_web.Models { public string Address; public string City; - public ComputerMini[] Computers; - public ConsoleMini[] Consoles; + public MachineMini[] Computers; + public MachineMini[] Consoles; public Iso3166 Country; public string Description; public string Facebook; diff --git a/cicm_web/Models/Computer.cs b/cicm_web/Models/Computer.cs index c5a59c8e..cff8a5b4 100644 --- a/cicm_web/Models/Computer.cs +++ b/cicm_web/Models/Computer.cs @@ -34,178 +34,88 @@ using System.Linq; namespace cicm_web.Models { - public class Computer + public static class Computer { - public int Bits; - public string Cap1; - public string Cap2; - public int Colors; - public Company Company; - public Processor Cpu1; - public Processor Cpu2; - public DiskFormat Disk1; - public DiskFormat Disk2; - public Gpu Gpu; - public DiskFormat Hdd1; - public DiskFormat Hdd2; - public DiskFormat Hdd3; - public int Id; - public float Mhz1; - public float Mhz2; - public string Model; - public int MusicChannels; - public SoundSynth MusicSynth; - public int Ram; - public string Resolution; - public int Rom; - public int SoundChannels; - public SoundSynth SoundSynth; - public int Vram; - public int Year; - - public static Computer[] GetAllItems() + public static Machine[] GetAllItems() { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); + List items = new List(); - return dbItems.Select(TransformItem).OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); + return dbItems.Select(Machine.TransformItem).OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static Computer[] GetItemsFromCompany(int id) + public static Machine[] GetItemsFromCompany(int id) { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; // TODO: Company chosen by DB - return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); + return dbItems.Where(t => t.Company == id).Select(Machine.TransformItem).OrderBy(t => t.Model).ToArray(); } - public static Computer GetItem(int id) + public static Machine GetItem(int id) { - Cicm.Database.Schemas.Computer dbItem = Program.Database?.Operations.GetComputer(id); - return dbItem == null ? null : TransformItem(dbItem); - } - - static Computer TransformItem(Cicm.Database.Schemas.Computer dbItem) - { - Computer item = new Computer - { - Bits = dbItem.Bits, - Colors = dbItem.Colors, - Company = Company.GetItem(dbItem.Company), - Gpu = Gpu.GetItem(dbItem.Gpu), - Hdd1 = DiskFormat.GetItem(dbItem.Hdd1), - Hdd2 = DiskFormat.GetItem(dbItem.Hdd2), - Hdd3 = DiskFormat.GetItem(dbItem.Hdd3), - Id = dbItem.Id, - Model = dbItem.Model, - Ram = dbItem.Ram, - Resolution = dbItem.Resolution, - Rom = dbItem.Rom, - Vram = dbItem.Vram, - Year = dbItem.Year - }; - - if(dbItem.Disk1 > 0) - { - item.Cap1 = dbItem.Cap1; - item.Disk1 = DiskFormat.GetItem(dbItem.Disk1); - } - - if(dbItem.Disk2 > 0) - { - item.Cap2 = dbItem.Cap2; - item.Disk2 = DiskFormat.GetItem(dbItem.Disk2); - } - - if(dbItem.Cpu1 > 0) - { - item.Cpu1 = Processor.GetItem(dbItem.Cpu1); - item.Mhz1 = dbItem.Mhz1; - } - - if(dbItem.Cpu2 > 0) - { - item.Cpu2 = Processor.GetItem(dbItem.Cpu2); - item.Mhz2 = dbItem.Mhz2; - } - - if(dbItem.MusicSynth > 0) - { - item.MusicSynth = SoundSynth.GetItem(dbItem.MusicSynth); - item.MusicChannels = dbItem.MusicChannels; - } - - if(dbItem.SoundSynth > 0) - { - item.SoundSynth = SoundSynth.GetItem(dbItem.SoundSynth); - item.SoundChannels = dbItem.SoundChannels; - } - - return item; + Cicm.Database.Schemas.Machine dbItem = Program.Database?.Operations.GetComputer(id); + return dbItem == null ? null : Machine.TransformItem(dbItem); } } - public class ComputerMini + public static class ComputerMini { - public Company Company; - public int Id; - public string Model; - - public static ComputerMini[] GetAllItems() + public static MachineMini[] GetAllItems() { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); - foreach(Cicm.Database.Schemas.Computer dbItem in dbItems) items.Add(TransformItem(dbItem)); + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) items.Add(MachineMini.TransformItem(dbItem)); return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static ComputerMini[] GetItemsStartingWithLetter(char letter) + public static MachineMini[] GetItemsStartingWithLetter(char letter) { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); - foreach(Cicm.Database.Schemas.Computer dbItem in dbItems) + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) if(dbItem.Model.StartsWith(new string(letter, 1), StringComparison.InvariantCultureIgnoreCase)) - items.Add(TransformItem(dbItem)); + items.Add(MachineMini.TransformItem(dbItem)); return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static ComputerMini[] GetItemsFromYear(int year) + public static MachineMini[] GetItemsFromYear(int year) { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); - foreach(Cicm.Database.Schemas.Computer dbItem in dbItems) + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) if(dbItem.Year == year) - items.Add(TransformItem(dbItem)); + items.Add(MachineMini.TransformItem(dbItem)); return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static ComputerMini[] GetItemsWithCompany(int id, string companyName) + public static MachineMini[] GetItemsWithCompany(int id, string companyName) { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; // TODO: Company chosen by DB return dbItems.Where(t => t.Company == id) - .Select(t => new ComputerMini + .Select(t => new MachineMini { Company = new Company {Id = id, Name = companyName}, Id = t.Id, @@ -213,19 +123,15 @@ namespace cicm_web.Models }).OrderBy(t => t.Model).ToArray(); } - public static ComputerMini[] GetItemsFromCompany(int id) + public static MachineMini[] GetItemsFromCompany(int id) { - List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems); + List dbItems = null; + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; // TODO: Company chosen by DB - return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); - } - - static ComputerMini TransformItem(Cicm.Database.Schemas.Computer dbItem) - { - return new ComputerMini {Company = Company.GetItem(dbItem.Company), Id = dbItem.Id, Model = dbItem.Model}; + return dbItems.Where(t => t.Company == id).Select(MachineMini.TransformItem).OrderBy(t => t.Model) + .ToArray(); } } } \ No newline at end of file diff --git a/cicm_web/Models/Console.cs b/cicm_web/Models/Console.cs index 0eaebd6f..73974bb8 100644 --- a/cicm_web/Models/Console.cs +++ b/cicm_web/Models/Console.cs @@ -34,164 +34,86 @@ using System.Linq; namespace cicm_web.Models { - public class Console + public static class Console { - public int Bits; - public int Cap; - public int Colors; - public Company Company; - public Processor Cpu1; - public Processor Cpu2; - public DiskFormat Format; - public Gpu Gpu; - public int Id; - public float Mhz1; - public float Mhz2; - public string Model; - public int MusicChannels; - public SoundSynth MusicSynth; - public int Palette; - public int Ram; - public string Resolution; - public int Rom; - public int SoundChannels; - public SoundSynth SoundSynth; - public int Vram; - public int Year; - - public static Console[] GetAllItems() + public static Machine[] GetAllItems() { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - return dbItems.OrderByDescending(i => i.Id).Select(TransformItem) as Console[]; + return dbItems.OrderByDescending(i => i.Id).Select(Machine.TransformItem) as Machine[]; } - public static Console[] GetItemsFromCompany(int id) + public static Machine[] GetItemsFromCompany(int id) { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; // TODO: Company chosen by DB - return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); + return dbItems.Where(t => t.Company == id).Select(Machine.TransformItem).OrderBy(t => t.Model).ToArray(); } - public static Console GetItem(int id) + public static Machine GetItem(int id) { - Cicm.Database.Schemas.Console dbItem = Program.Database?.Operations.GetConsole(id); - return dbItem == null ? null : TransformItem(dbItem); - } - - static Console TransformItem(Cicm.Database.Schemas.Console dbItem) - { - Console item = new Console - { - Bits = dbItem.Bits, - Colors = dbItem.Colors, - Company = Company.GetItem(dbItem.Company), - Gpu = Gpu.GetItem(dbItem.Gpu), - Id = dbItem.Id, - Model = dbItem.Model, - Palette = dbItem.Palette, - Ram = dbItem.Ram, - Resolution = dbItem.Resolution, - Rom = dbItem.Rom, - Vram = dbItem.Vram, - Year = dbItem.Year - }; - - if(dbItem.Format > 0) - { - item.Cap = dbItem.Cap; - item.Format = DiskFormat.GetItem(dbItem.Format); - } - - if(dbItem.Cpu1 > 0) - { - item.Cpu1 = Processor.GetItem(dbItem.Cpu1); - item.Mhz1 = dbItem.Mhz1; - } - - if(dbItem.Cpu2 > 0) - { - item.Cpu2 = Processor.GetItem(dbItem.Cpu2); - item.Mhz2 = dbItem.Mhz2; - } - - if(dbItem.MusicSynth > 0) - { - item.MusicSynth = SoundSynth.GetItem(dbItem.MusicSynth); - item.MusicChannels = dbItem.MusicChannels; - } - - if(dbItem.SoundSynth > 0) - { - item.SoundSynth = SoundSynth.GetItem(dbItem.SoundSynth); - item.SoundChannels = dbItem.SoundChannels; - } - - return item; + Cicm.Database.Schemas.Machine dbItem = Program.Database?.Operations.GetConsole(id); + return dbItem == null ? null : Machine.TransformItem(dbItem); } } - public class ConsoleMini + public static class ConsoleMini { - public Company Company; - public int Id; - public string Model; - - public static ConsoleMini[] GetAllItems() + public static MachineMini[] GetAllItems() { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); - foreach(Cicm.Database.Schemas.Console dbItem in dbItems) items.Add(TransformItem(dbItem)); + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) items.Add(MachineMini.TransformItem(dbItem)); return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static ConsoleMini[] GetItemsStartingWithLetter(char letter) + public static MachineMini[] GetItemsStartingWithLetter(char letter) { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); - foreach(Cicm.Database.Schemas.Console dbItem in dbItems) + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) if(dbItem.Model.StartsWith(new string(letter, 1), StringComparison.InvariantCultureIgnoreCase)) - items.Add(TransformItem(dbItem)); + items.Add(MachineMini.TransformItem(dbItem)); return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static ConsoleMini[] GetItemsFromYear(int year) + public static MachineMini[] GetItemsFromYear(int year) { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); - foreach(Cicm.Database.Schemas.Console dbItem in dbItems) + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) if(dbItem.Year == year) - items.Add(TransformItem(dbItem)); + items.Add(MachineMini.TransformItem(dbItem)); return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } - public static ConsoleMini[] GetItemsWithCompany(int id, string companyName) + public static MachineMini[] GetItemsWithCompany(int id, string companyName) { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; // TODO: Company chosen by DB return dbItems.Where(t => t.Company == id) - .Select(t => new ConsoleMini + .Select(t => new MachineMini { Company = new Company {Id = id, Name = companyName}, Id = t.Id, @@ -199,19 +121,15 @@ namespace cicm_web.Models }).OrderBy(t => t.Model).ToArray(); } - public static ConsoleMini[] GetItemsFromCompany(int id) + public static MachineMini[] GetItemsFromCompany(int id) { - List dbItems = null; + List dbItems = null; bool? result = Program.Database?.Operations.GetConsoles(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; // TODO: Company chosen by DB - return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); - } - - static ConsoleMini TransformItem(Cicm.Database.Schemas.Console dbItem) - { - return new ConsoleMini {Company = Company.GetItem(dbItem.Company), Id = dbItem.Id, Model = dbItem.Model}; + return dbItems.Where(t => t.Company == id).Select(MachineMini.TransformItem).OrderBy(t => t.Model) + .ToArray(); } } } \ No newline at end of file diff --git a/cicm_web/Models/Machine.cs b/cicm_web/Models/Machine.cs new file mode 100644 index 00000000..5d7a38ca --- /dev/null +++ b/cicm_web/Models/Machine.cs @@ -0,0 +1,232 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : Machine.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Machine model +// +// --[ 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 © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Linq; +using Cicm.Database.Schemas; + +namespace cicm_web.Models +{ + public class Machine + { + public string Cap1; + public string Cap2; + public int Colors; + public Company Company; + public Processor Cpu1; + public Processor Cpu2; + public DiskFormat Disk1; + public DiskFormat Disk2; + public Gpu Gpu; + public DiskFormat Hdd1; + public DiskFormat Hdd2; + public DiskFormat Hdd3; + public int Id; + public float Mhz1; + public float Mhz2; + public string Model; + public int MusicChannels; + public SoundSynth MusicSynth; + public int Ram; + public string Resolution; + public int Rom; + public int SoundChannels; + public SoundSynth SoundSynth; + public MachineType Type; + public int Vram; + public int Year; + + public static Machine[] GetAllItems() + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + List items = new List(); + + return dbItems.Select(TransformItem).OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); + } + + public static Machine[] GetItemsFromCompany(int id) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + // TODO: Company chosen by DB + return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); + } + + public static Machine GetItem(int id) + { + Cicm.Database.Schemas.Machine dbItem = Program.Database?.Operations.GetMachine(id); + return dbItem == null ? null : TransformItem(dbItem); + } + + internal static Machine TransformItem(Cicm.Database.Schemas.Machine dbItem) + { + Machine item = new Machine + { + Colors = dbItem.Colors, + Company = Company.GetItem(dbItem.Company), + Gpu = Gpu.GetItem(dbItem.Gpu), + Hdd1 = DiskFormat.GetItem(dbItem.Hdd1), + Hdd2 = DiskFormat.GetItem(dbItem.Hdd2), + Hdd3 = DiskFormat.GetItem(dbItem.Hdd3), + Id = dbItem.Id, + Model = dbItem.Model, + Ram = dbItem.Ram, + Resolution = dbItem.Resolution, + Rom = dbItem.Rom, + Vram = dbItem.Vram, + Year = dbItem.Year, + Type = dbItem.Type + }; + + if(dbItem.Disk1 > 0) + { + item.Cap1 = dbItem.Cap1; + item.Disk1 = DiskFormat.GetItem(dbItem.Disk1); + } + + if(dbItem.Disk2 > 0) + { + item.Cap2 = dbItem.Cap2; + item.Disk2 = DiskFormat.GetItem(dbItem.Disk2); + } + + if(dbItem.Cpu1 > 0) + { + item.Cpu1 = Processor.GetItem(dbItem.Cpu1); + item.Mhz1 = dbItem.Mhz1; + } + + if(dbItem.Cpu2 > 0) + { + item.Cpu2 = Processor.GetItem(dbItem.Cpu2); + item.Mhz2 = dbItem.Mhz2; + } + + if(dbItem.MusicSynth > 0) + { + item.MusicSynth = SoundSynth.GetItem(dbItem.MusicSynth); + item.MusicChannels = dbItem.MusicChannels; + } + + if(dbItem.SoundSynth > 0) + { + item.SoundSynth = SoundSynth.GetItem(dbItem.SoundSynth); + item.SoundChannels = dbItem.SoundChannels; + } + + return item; + } + } + + public class MachineMini + { + public Company Company; + public int Id; + public string Model; + + public static MachineMini[] GetAllItems() + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + + if(result == null || result.Value == false || dbItems == null) return null; + + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) items.Add(TransformItem(dbItem)); + + return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); + } + + public static MachineMini[] GetItemsStartingWithLetter(char letter) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) + if(dbItem.Model.StartsWith(new string(letter, 1), StringComparison.InvariantCultureIgnoreCase)) + items.Add(TransformItem(dbItem)); + + return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); + } + + public static MachineMini[] GetItemsFromYear(int year) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + List items = new List(); + foreach(Cicm.Database.Schemas.Machine dbItem in dbItems) + if(dbItem.Year == year) + items.Add(TransformItem(dbItem)); + + return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); + } + + public static MachineMini[] GetItemsWithCompany(int id, string companyName) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + // TODO: Company chosen by DB + return dbItems.Where(t => t.Company == id) + .Select(t => new MachineMini + { + Company = new Company {Id = id, Name = companyName}, + Id = t.Id, + Model = t.Model + }).OrderBy(t => t.Model).ToArray(); + } + + public static MachineMini[] GetItemsFromCompany(int id) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetMachines(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + // TODO: Company chosen by DB + return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); + } + + internal static MachineMini TransformItem(Cicm.Database.Schemas.Machine dbItem) + { + return new MachineMini {Company = Company.GetItem(dbItem.Company), Id = dbItem.Id, Model = dbItem.Model}; + } + } +} \ No newline at end of file diff --git a/cicm_web/Models/News.cs b/cicm_web/Models/News.cs index 23f34e8f..c67b56e7 100644 --- a/cicm_web/Models/News.cs +++ b/cicm_web/Models/News.cs @@ -71,13 +71,13 @@ namespace cicm_web.Models static News TransformItem(Cicm.Database.Schemas.News dbItem) { - string imageUrl; - string text; - string targetView; - string subtext; - Computer computer; + string imageUrl; + string text; + string targetView; + string subtext; + Machine computer; OwnedComputer owncomputer; - Console console; + Machine console; OwnedConsole ownconsole; switch(dbItem.Type) diff --git a/cicm_web/Models/OwnedComputer.cs b/cicm_web/Models/OwnedComputer.cs index c3b5a341..75543b3e 100644 --- a/cicm_web/Models/OwnedComputer.cs +++ b/cicm_web/Models/OwnedComputer.cs @@ -42,7 +42,7 @@ namespace cicm_web.Models public bool Boxed; public int Cap1; public int Cap2; - public Computer Computer; + public Machine Computer; public Processor Cpu1; public Processor Cpu2; public DiskFormat Disk1; @@ -75,7 +75,7 @@ namespace cicm_web.Models static OwnedComputer TransformItem(Cicm.Database.Schemas.OwnedComputer dbItem) { - Computer computer = Computer.GetItem(dbItem.ComputerId); + Machine computer = Machine.GetItem(dbItem.ComputerId); OwnedComputer item = new OwnedComputer { diff --git a/cicm_web/Models/OwnedConsole.cs b/cicm_web/Models/OwnedConsole.cs index 5a02ca64..3b281437 100644 --- a/cicm_web/Models/OwnedConsole.cs +++ b/cicm_web/Models/OwnedConsole.cs @@ -40,7 +40,7 @@ namespace cicm_web.Models { public DateTime Acquired; public bool Boxed; - public Console Console; + public Machine Console; public int Id; public bool Manuals; public StatusType Status; @@ -64,7 +64,7 @@ namespace cicm_web.Models static OwnedConsole TransformItem(Cicm.Database.Schemas.OwnedConsole dbItem) { - Console console = Console.GetItem(dbItem.ConsoleId); + Machine console = Machine.GetItem(dbItem.ConsoleId); return console == null ? null diff --git a/cicm_web/Views/Company/View.cshtml b/cicm_web/Views/Company/View.cshtml index 4fb2a823..7292024f 100644 --- a/cicm_web/Views/Company/View.cshtml +++ b/cicm_web/Views/Company/View.cshtml @@ -33,314 +33,315 @@ } @using System.IO @using Cicm.Database.Schemas -@using Markdig @model CompanyWithItems @if(Model != null) {
-

- @if(Model.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", Model.LastLogo.Guid + ".svg"))) - { - - - + + - - - } -

-
- @{ - string carrouselActive = "active"; - } - @if(Model.Logos != null && Model.Logos.Length > 1) - { -
-