//Create
 public int?Add(GodDTO entity)
 {
     try
     {
         using (MySqlConnection conn = _con.GetConnection())
         {
             conn.Open();
             MySqlCommand cmd = new MySqlCommand("INSERT INTO god (GodIDGodName,GodTitle,GodLore,GodClass,GodIcon,GodCardArt) VALUES(?GodID,?GodName," +
                                                 "?GodTitle,?GodLore,?GodClass,?GodIcon,?GodCardArt)", conn);
             //values
             cmd.Parameters.AddWithValue("GodID", entity.GodID ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodName", entity.GodName ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodTitle", entity.GodTitle ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodLore", entity.GodLore ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodClass", entity.GodClass ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodIcon", entity.GodIcon ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodCardArt", entity.GodCardArt ?? (object)DBNull.Value);
             //execute command
             int rowsAffected = cmd.ExecuteNonQuery();
             //should return if a row is affected or not
             return(rowsAffected);
         }
     }
     catch (Exception ex)
     {
         throw new ContextErrorException(ex);
     }
 }
        public GodDTO GetByID(int?id)
        {
            try
            {
                GodDTO god = new GodDTO();

                using (MySqlConnection conn = _con.GetConnection())
                {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand("SELECT GodID,GodName,GodTitle,GodLore,GodClass,GodIcon,GodCardArt FROM god WHERE GodID = ?id", conn);
                    cmd.Parameters.AddWithValue("id", id);

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            god.GodID      = reader[0] as int? ?? default;
                            god.GodName    = reader[1] as string ?? default;
                            god.GodTitle   = reader[2] as string ?? default;
                            god.GodLore    = reader[3] as string ?? default;
                            god.GodClass   = reader[4] as string ?? default;
                            god.GodIcon    = reader[5] as byte[] ?? default;
                            god.GodCardArt = reader[6] as byte[] ?? default;
                        }
                    }
                }
                return(god);
            }
            catch (Exception ex)
            {
                throw new ContextErrorException(ex);
            }
        }
 //Update
 public int?Update(GodDTO entity)
 {
     try
     {
         using (MySqlConnection conn = _con.GetConnection())
         {
             conn.Open();
             MySqlCommand cmd = new MySqlCommand("UPDATE god SET GodName = ?GodName,GodTitle = ?GodTitle, GodLore = ?GodLore, " +
                                                 "GodClass = ?GodClass, GodIcon = ?GodIcon, GodCardArt = ?GodCardArt WHERE GodID = ?id", conn);
             //where id is
             cmd.Parameters.AddWithValue("id", entity.GodID);
             //values
             cmd.Parameters.AddWithValue("GodName", entity.GodName ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodTitle", entity.GodTitle ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodLore", entity.GodLore ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodClass", entity.GodClass ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodIcon", entity.GodIcon ?? (object)DBNull.Value);
             cmd.Parameters.AddWithValue("GodCardArt", entity.GodCardArt ?? (object)DBNull.Value);
             //execute command
             int rowsAffected = cmd.ExecuteNonQuery();
             //should return if a row is affected or not
             return(rowsAffected);
         }
     }
     catch (Exception ex)
     {
         throw new ContextErrorException(ex);
     }
 }
Ejemplo n.º 4
0
        public async Task <int?> UpdateGods()
        {
            IEnumerable <ApiGod> apiGods = await _hirezApi.GetAllGods();

            int countNewAdditions = 0;

            foreach (ApiGod god in apiGods)
            {
                var    GodInDB = _godContext.GetByID((int)god.Id);
                GodDTO gDTO    = new GodDTO
                {
                    GodID    = (int?)god.Id,
                    GodName  = god.Name,
                    GodTitle = god.Title,
                    GodClass = god.Classes.ToString(),
                    GodLore  = god.Lore,
                };
                using (var webClient = new WebClient())
                {
                    try
                    {
                        byte[] icon    = webClient.DownloadData(god.GodIcon_URL);
                        byte[] CardArt = webClient.DownloadData(god.GodCard_Url);

                        gDTO.GodIcon    = icon;
                        gDTO.GodCardArt = CardArt;
                    }
                    catch
                    {
                        //"Couldn't update god because the api returned a bad gateway, image / data is likely not implemented yet";
                    }
                }

                if (GodInDB.GodName == null)
                {
                    //create
                    _godContext.Add(gDTO);
                    countNewAdditions++;
                }
                else
                {
                    //update
                    _godContext.Update(gDTO);
                }
            }

            return(countNewAdditions);
        }
 //Delete
 public int?Remove(GodDTO entity)
 {
     try
     {
         using (MySqlConnection conn = _con.GetConnection())
         {
             conn.Open();
             MySqlCommand cmd = new MySqlCommand("DELETE FROM god WHERE GodID = ?id", conn);
             //where id =
             cmd.Parameters.AddWithValue("id", entity.GodID);
             //execute command
             int rowsAffected = cmd.ExecuteNonQuery();
             //should return if a row is affected or not
             return(rowsAffected);
         }
     }
     catch (Exception ex)
     {
         throw new ContextErrorException(ex);
     }
 }