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