public IActionResult Create([FromBody] Frostings frosting) { bool result = false; if (cakeOptionsDao.AddFrostingOption(frosting)) { result = true; } return(Ok(result)); }
public IActionResult UpdateFrostingAvailability([FromBody] Frostings frosting) { bool result = false; if (cakeOptionsDao.UpdateFrostingAvailability(frosting)) { result = true; } return(Ok(result)); }
public bool UpdateFrostingAvailability(Frostings updatedFrosting) { try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("UPDATE frostings SET available = @available WHERE id = @id;", conn); cmd.Parameters.AddWithValue("@id", updatedFrosting.id); cmd.Parameters.AddWithValue("@available", updatedFrosting.isAvailable); cmd.ExecuteNonQuery(); } } catch (SqlException ex) { return(false); } return(true); }
public bool AddFrostingOption(Frostings newFrosting) { try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO frostings (available, frosting) VALUES (@available, @frosting);", conn); cmd.Parameters.AddWithValue("@available", newFrosting.isAvailable); cmd.Parameters.AddWithValue("@frosting", newFrosting.frosting); cmd.ExecuteNonQuery(); } } catch (SqlException ex) { return(false); } return(true); }
/// <summary> /// Following 3 methods are used for getting, adding, updating frosting options /// </summary> /// <returns></returns> public List <Frostings> GetAllCakeFrostings() { List <Frostings> listOfFrostings = new List <Frostings>(); try { // Create a new connection object using (SqlConnection conn = new SqlConnection(connectionString)) { // Open the connection conn.Open(); string sql = $"SELECT * FROM frostings"; SqlCommand cmd = new SqlCommand(sql, conn); // Execute the command SqlDataReader reader = cmd.ExecuteReader(); // Loop through each row while (reader.Read()) { Frostings frosting = new Frostings(); frosting.id = Convert.ToInt32(reader["id"]); frosting.frosting = Convert.ToString(reader["frosting"]); frosting.isAvailable = Convert.ToBoolean(reader["available"]); listOfFrostings.Add(frosting); } } } catch (SqlException ex) { throw; } return(listOfFrostings); }