Esempio n. 1
0
        public PokeTradeDetail(TPoke pkm, PokeTradeTrainerInfo info, GiveawayPoolEntry poolEntry, IPokeTradeNotifier <TPoke> notifier, PokeTradeType type, int code, bool favored = false)
        {
            Code      = code;
            TradeData = pkm;
            PoolEntry = poolEntry;
            Trainer   = info;
            Notifier  = notifier;
            Type      = type;
            Time      = DateTime.Now;
            IsFavored = favored;

            ID = Interlocked.Increment(ref CreatedCount) % 3000;
        }
        public int NewEntry(GiveawayPoolEntry entry)
        {
            SQLiteConnection conn = NewConnection();;
            int result            = -1;
            int entryID           = 0;

            try
            {
                using SQLiteCommand cmd = new SQLiteCommand(conn);
                cmd.CommandText         = "INSERT INTO " + entry.Pool + " (Name, Pokemon, Status, PK8, Tag, Uploader)" +
                                          "VALUES (@name, @pokemon, @status, @pk8, @tag, @uploader)";
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@name", entry.Name);
                cmd.Parameters.AddWithValue("@pokemon", entry.Pokemon);
                cmd.Parameters.AddWithValue("@status", entry.Status);
                cmd.Parameters.AddWithValue("@pk8", Utils.PK8ToB64(entry.PK8));
                cmd.Parameters.AddWithValue("@tag", entry.Tag);
                cmd.Parameters.AddWithValue("@uploader", entry.Uploader);

                result = cmd.ExecuteNonQuery();
            }
            catch (SQLiteException e)
            {
                LogUtil.LogInfo("Error inserting new entry: " + e.Message, nameof(GiveawayPool));
                return(0);
            }

            try
            {
                using SQLiteCommand getRow = new SQLiteCommand(conn);
                getRow.CommandText         = "SELECT last_insert_rowid()";
                var lastRow = getRow.ExecuteScalar();
                Int32.TryParse(lastRow.ToString(), out entryID);
            }
            catch (SQLiteException e)
            {
                LogUtil.LogInfo("Error getting new entry id: " + e.Message, nameof(GiveawayPool));
                entryID = 0;
            }
            finally
            {
                conn.Close();
            }

            return(entryID);
        }
        public List <GiveawayPoolEntry> SearchPool(string pool, string search)
        {
            List <GiveawayPoolEntry> entries = new List <GiveawayPoolEntry>();
            SQLiteConnection         conn    = NewConnection();
            var name = search;
            var tag  = search;

            try
            {
                using SQLiteCommand cmd = new SQLiteCommand(conn);

                cmd.CommandText = "SELECT * FROM " + pool + " WHERE Name like @name OR Tag like @tag";
                cmd.Prepare();
                LogUtil.LogInfo(cmd.CommandText, "SQL");
                LogUtil.LogInfo("Pool: " + pool + " Search: " + name + "/" + tag, "SQL");
                cmd.Parameters.AddWithValue("name", name);
                cmd.Parameters.AddWithValue("tag", tag);

                using SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    GiveawayPoolEntry entry = new GiveawayPoolEntry();

                    Int32.TryParse(reader["Id"].ToString(), out var id);
                    entry.Id = id;
#pragma warning disable CS8601 // Possible null reference assignment.
                    if (reader["Name"] != null)
                    {
                        entry.Name = reader["Name"].ToString();
                    }
                    if (reader["Tag"] != null)
                    {
                        entry.Tag = reader["Tag"].ToString();
                    }
                    if (reader["Status"] != null)
                    {
                        entry.Status = reader["Status"].ToString();
                    }
                    if (reader["PK8"] != null)
                    {
                        entry.PK8 = Utils.B64ToPK8(reader["PK8"].ToString());
                    }
                    if (reader["Pokemon"] != null)
                    {
                        entry.Pokemon = reader["Pokemon"].ToString();
                    }
                    if (reader["Description"] != null)
                    {
                        entry.Description = reader["Description"].ToString();
                    }
                    if (reader["Uploader"] != null)
                    {
                        entry.Uploader = reader["Uploader"].ToString();
                    }
#pragma warning restore CS8601 // Possible null reference assignment.
                    entries.Add(entry);
                }
            }
            catch (SQLiteException e)
            {
                LogUtil.LogInfo("Error querying database: " + e.Message, nameof(GiveawayPool));
            }
            finally
            {
                conn.Close();
            }
            return(entries);
        }