public Recipe GetRecipeById(int id) { Recipe r = new Recipe(); SqlConnection con = CreateConnection(); try { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "GetRecipeById"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", id); SqlDataReader res = cmd.ExecuteReader(); res.Read(); r = MapResultFromDatabaseToRecipeObject(res); } catch (SqlException ex) { throw /*ex*/; } finally { if (con != null) { con.Close(); } } return r; }
public List<Comment> GetCommentsByRecipe(Recipe r) { SqlConnection con = CreateConnection(); List<Comment> cl = new List<Comment>(); try { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "GetCommentByRecipe"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@recipeID", r.Id); SqlDataReader res = cmd.ExecuteReader(); cl = MakeCommentObjectIntoCommentList(res); } catch (SqlException ex) { throw /*ex*/; } finally { if (con != null) { con.Close(); } } return cl; }
public void AddRatingToRecipe(Rating rate, Recipe r, User u) { SqlConnection con = CreateConnection(); try { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "AddRatingToRecipe"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@recipeID", r.Id); cmd.Parameters.AddWithValue("@userID", u.Id); cmd.Parameters.AddWithValue("@rating", rate); cmd.ExecuteNonQuery(); } catch (SqlException ex) { throw /*ex*/; } finally { if (con != null) { con.Close(); } } }
public void Add(Recipe r) { SqlConnection con = CreateConnection(); try { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "AddRecipe"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@name", r.Name); cmd.Parameters.AddWithValue("@content", r.Content); cmd.ExecuteNonQuery(); } catch (SqlException ex) { throw /*ex*/; } finally { if (con != null) { con.Close(); } } }
public void GetRecipeById() { SqlRecipeManager srm = new SqlRecipeManager(); Recipe r = new Recipe(); int id=1; r = srm.GetRecipeById(id); Assert.AreEqual(id, r.Id); }
public void GetCommentsByRecipe() { SqlCommentManager scm = new SqlCommentManager(); SqlRecipeManager srm = new SqlRecipeManager(); Recipe r = new Recipe(); r = srm.GetRecipeById(1); List<Comment> cl = scm.GetCommentsByRecipe(r); Assert.AreEqual(1, cl.Count); }
public void AddTestRecipe() { SqlRecipeManager srm = new SqlRecipeManager(); Recipe r = new Recipe(); r.Id = 123124; r.AverageRating = 0; r.Content = "RECIPIE!"; string tempName = DateTime.Now.ToString(); r.Name = tempName; r.UserId = 1; srm.Add(r); Recipe found = srm.GetRecipeByName(tempName); Assert.AreEqual(tempName, found.Name); }
public Recipe MapDbResultToRecipe(SqlDataReader res) { res.Read(); User u = new User(); Recipe r = new Recipe(); r.Name = res["name"].ToString(); r.Username = res["username"].ToString(); r.Content = res["content"].ToString(); return r; }
public Recipe GetRecipe(string name) { Recipe r = new Recipe(); SqlConnection con = CreateConnection(); try { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "GetRecipe"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@name", name); cmd.ExecuteReader(); SqlDataReader res = cmd.ExecuteReader(); r = MapDbResultToRecipe(res); } catch (SqlException ex) { throw; } finally { if (con != null) { con.Close(); } } return r; }
public Recipe MapDbResultToRecipe(SqlDataReader res, bool isSingle=true) { if (isSingle) { res.Read(); } Recipe r = new Recipe(); if (res.HasRows) { r.Id = (int)res["Id"]; r.Name = res["name"].ToString(); r.Username = res["username"].ToString(); r.Content = res["content"].ToString(); if (res["rating"] != DBNull.Value) { r.Rating = (decimal)res["rating"]; } else { r.Rating = 0.0M; } } return r; }
private Recipe MapResultFromDatabaseToRecipeObject(SqlDataReader res) { Recipe r = new Recipe(); r.Id = (int)res["id"]; r.UserId = (int)res["userId"]; r.Name = res["name"].ToString(); r.Content = res["content"].ToString(); r.AverageRating = 0; return r; }