コード例 #1
0
        private void buttonAddToFavourites_Click(object sender, EventArgs e)
        {
            Favourite currentSuite = new Favourite
            {
                UserId  = userId,
                SuiteId = selectedSuiteId
            };


            dbAccess.OpenConnection();
            inFavourties = dbAccess.CheckInFavourites(new Favourite
            {
                UserId  = userId,
                SuiteId = selectedSuiteId
            });
            dbAccess.CloseConnection();

            if (inFavourties)
            {
                dbAccess.OpenConnection();
                dbAccess.RemoveFromFavourites(currentSuite);
                ChangeFavouriteIcon(false);
                dbAccess.CloseConnection();
            }
            else
            {
                dbAccess.OpenConnection();
                dbAccess.AddToFavourites(currentSuite);
                ChangeFavouriteIcon(true);
                dbAccess.CloseConnection();
            }
        }
コード例 #2
0
        /// <summary>
        /// Checks existence of a favourite in the db.
        /// </summary>
        /// <param name="f">Favourite Object</param>
        /// <returns>Boolean</returns>
        public bool CheckInFavourites(Favourite f)
        {
            bool existsInFavourites = false;

            string sql = "Select * From Favourites where " +
                         $"Favourites.UserId = {f.UserId} and Favourites.SuiteId = {f.SuiteId}";

            using (SqlCommand command = new SqlCommand(sql, dbConnection))
            {
                try
                {
                    SqlDataReader dataReader = command.ExecuteReader();
                    if (dataReader.HasRows)
                    {
                        existsInFavourites = true;
                    }
                }
                catch (SqlException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }

            return(existsInFavourites);
        }
コード例 #3
0
        /// <summary>
        /// Removes the given favourite from the database
        /// </summary>
        /// <param name="f">Favourite Object</param>
        public void RemoveFromFavourites(Favourite f)
        {
            string sql = "Delete from Favourites where " +
                         $"Favourites.UserId = {f.UserId} and Favourites.SuiteId = {f.SuiteId}";

            using (SqlCommand command = new SqlCommand(sql, dbConnection))
            {
                try
                {
                    command.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Inserts a new favourite to the database
        /// </summary>
        /// <param name="f">Favourite Object</param>
        public void AddToFavourites(Favourite f)
        {
            string sql = "Insert Into Favourites (UserId, SuiteId) Values " +
                         $"('{f.UserId}', '{f.SuiteId}')";

            using (SqlCommand command = new SqlCommand(sql, dbConnection))
            {
                try
                {
                    command.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
        }