/****************************************************************************** // Canary Islands Computer Museum Website // ---------------------------------------------------------------------------- // // Filename : Console.cs // Author(s) : Natalia Portillo // // --[ Description ] ---------------------------------------------------------- // // Contains operations to manage videogame consoles. // // --[ 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 consoles /// /// All consoles /// true if is correct, false otherwise public bool GetConsoles(out List entries) { #if DEBUG Console.WriteLine("Getting all consoles..."); #endif try { string sql = $"SELECT * FROM machines WHERE type = '{(int)MachineType.Console}'"; 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 consoles."); Console.WriteLine(ex); entries = null; return false; } } /// /// Gets all consoles from specified company /// /// All consoles /// Company id /// true if is correct, false otherwise public bool GetConsoles(out List entries, int company) { #if DEBUG Console.WriteLine("Getting all consoles from company id {0}...", company); #endif try { string sql = $"SELECT * FROM machines WHERE company = '{company}' AND type = '{(int)MachineType.Console}'"; 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 consoles."); Console.WriteLine(ex); entries = null; return false; } } /// /// Gets the specified number of videogame consoles since the specified start /// /// List of videogame consoles /// Start of query /// How many entries to retrieve /// true if is correct, false otherwise public bool GetConsoles(out List entries, ulong start, ulong count) { #if DEBUG Console.WriteLine("Getting {0} consoles from {1}...", count, start); #endif try { string sql = $"SELECT * FROM machines WHERE type = '{(int)MachineType.Console}' 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 consoles."); Console.WriteLine(ex); entries = null; return false; } } /// /// Gets videogame console by specified id /// /// Id /// Videogame console with specified id, null if not found or error public Machine GetConsole(int id) { return GetMachine(id); } /// /// Counts the number of videogame consoles in the database /// /// Entries in database public long CountConsoles() { #if DEBUG Console.WriteLine("Counting consoles..."); #endif IDbCommand dbcmd = dbCon.CreateCommand(); dbcmd.CommandText = $"SELECT COUNT(*) FROM consoles WHERE type = '{(int)MachineType.Console}'"; object count = dbcmd.ExecuteScalar(); dbcmd.Dispose(); try { return Convert.ToInt64(count); } catch { return 0; } } /// /// Adds a new videogame console to the database /// /// Entry to add /// ID of added entry /// true if added correctly, false otherwise public bool AddConsole(Machine entry, out long id) { entry.Type = MachineType.Console; return AddMachine(entry, out id); } /// /// Updated an videogame console in the database /// /// Updated entry /// true if updated correctly, false otherwise public bool UpdateConsole(Machine entry) { return UpdateMachine(entry); } /// /// Removes a videogame console from the database /// /// ID of entry to remove /// true if removed correctly, false otherwise public bool RemoveConsole(long id) { return RemoveMachine(id); } } }