Пример #1
0
        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;
        }
Пример #2
0
        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;
        }
Пример #3
0
        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();
                }
            }
        }
Пример #4
0
        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);
        }
Пример #8
0
        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;
        }
Пример #9
0
        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;
        }
Пример #10
0
        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;
        }
Пример #11
0
 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;
 }