// Copyright (c) 2024 Synty Studios Limited. All rights reserved. // // Use of this software is subject to the terms and conditions of the Synty Studios End User Licence Agreement (EULA) // available at: https://syntystore.com/pages/end-user-licence-agreement // // For additional details, see the LICENSE.MD file bundled with this software. using SQLite; using System; using System.Collections.Generic; namespace Synty.SidekickCharacters.Database.DTO { [Table("sk_species")] public class SidekickSpecies { [PrimaryKey, AutoIncrement, Column("id")] public int ID { get; set; } [Column("name")] public string Name { get; set; } [Column("code")] public string Code { get; set; } /// /// Gets a list of all the Species in the database. /// /// The Database Manager to use. /// Whether to excludes species that have no active parts /// A list of all species in the database. public static List GetAll(DatabaseManager dbManager, bool excludeSpeciesWithNoParts = true) { List allSpecies = dbManager.GetCurrentDbConnection().Table().ToList(); List filteredSpecies = allSpecies; if (excludeSpeciesWithNoParts) { filteredSpecies = new List(); foreach (SidekickSpecies species in allSpecies) { List allParts = SidekickPart.GetAllForSpecies(dbManager, species); if (allParts.Count > 0 || species.Name.Equals("Unrestricted", StringComparison.OrdinalIgnoreCase)) { filteredSpecies.Add(species); } } } return filteredSpecies; } /// /// Gets a specific Species by its database ID. /// /// The Database Manager to use. /// The id of the required Species. /// The specific Species if it exists; otherwise null. public static SidekickSpecies GetByID(DatabaseManager dbManager, int id) { return dbManager.GetCurrentDbConnection().Get(id); } /// /// Gets a specific Species by its database ID. /// /// The Database Manager to use. /// The name of the required Species. /// The specific Species if it exists; otherwise null. public static SidekickSpecies GetByName(DatabaseManager dbManager, string name) { return dbManager.GetCurrentDbConnection().Table().FirstOrDefault(species => species.Name.ToLower() == name.ToLower()); } /// /// Updates or Inserts this item in the Database. /// /// The database manager to use. public int Save(DatabaseManager dbManager) { if (ID < 0) { dbManager.GetCurrentDbConnection().Insert(this); // in theory this could return a different ID, but in practice it's highly unlikely ID = (int) SQLite3.LastInsertRowid(dbManager.GetCurrentDbConnection().Handle); } dbManager.GetCurrentDbConnection().Update(this); return ID; } /// /// Deletes this item from the database /// /// The database manager to use. public void Delete(DatabaseManager dbManager) { dbManager.GetCurrentDbConnection().Delete(ID); } /// public override bool Equals(object obj) { SidekickSpecies species = (SidekickSpecies) obj; if (ID > 0 && species?.ID > 0) { return ID == species?.ID; } return Name.Equals(species?.Name); } /// public override int GetHashCode() { return HashCode.Combine(ID, Name, Code); } } }