Ejemplo n.º 1
0
        internal bool CreateCategory(Category category)
        {
            //Maken van een categorie
            using (NpgsqlCommand cmd = new NpgsqlCommand())
            {
                cmd.Connection = _connection;
                _transaction = cmd.Connection.BeginTransaction();
                cmd.CommandText = "INSERT INTO category(name) VALUES(@name);";

                cmd.Parameters.AddWithValue("name", category.Name);

                bool success = parseNonqueryResult(cmd.ExecuteNonQuery());
                if (success)
                {
                    _transaction.Commit();
                    _transaction.Dispose();
                    return success; //Commit als het sucessvol is
                }
                _transaction.Rollback();
                _transaction.Dispose();
                return success;
            }
        }
Ejemplo n.º 2
0
 public ActionResult UpdateCategory(Category category)
 {
     try
     {
         using (DatabaseQuery query = new DatabaseQuery())
         {
             if (ModelState.IsValid) //check of de modelstate goed is
             {
                 query.UpdateCategory(category);
                 return RedirectToAction("CategoryOverview");
             }
         }
     }
     catch (Exception e)
     {
         ViewBag.Error = "Er is iets fout gegaan met het updaten van de categorie: " + e;
         return RedirectToAction("ChangeCategory", category);
     }
     return RedirectToAction("category");
 }
Ejemplo n.º 3
0
 public ActionResult AddCategory()
 {
     Category model = new Category();
     return View(model); //Geef pagina weer
 }
Ejemplo n.º 4
0
        public ActionResult NewCategory(Category category)
        {
            try
            {
                if (ModelState.IsValid) //check of de modelstate goed is
                {
                    using (DatabaseQuery query = new DatabaseQuery())
                    {

                        query.CreateCategory(category);
                        return RedirectToAction("index"); //Laat de Admin zien als het goed gaat
                    }
                }
            }
            catch (Exception error)
            {
                ModelState.AddModelError("Product is niet toegevoegd aan de Database", error);
                //Adds a model error to the errors collection for the model-state dictionary.
            }
            return RedirectToAction("AddCategory", category);
        }
Ejemplo n.º 5
0
        internal bool UpdateCategory(Category category)
        {
            using (NpgsqlCommand cmd = new NpgsqlCommand())
            {
                cmd.Connection = this._connection;
                _transaction = cmd.Connection.BeginTransaction();

                cmd.CommandText = "UPDATE category SET name=@name where id = @id;";

                cmd.Parameters.AddWithValue("id", (long)category.Id);
                cmd.Parameters.AddWithValue("name", category.Name);

                //Parameters
                bool success = parseNonqueryResult(cmd.ExecuteNonQuery());
                if (success)
                {
                    _transaction.Commit();
                    _transaction.Dispose();
                }
                return success;
            }
        }
Ejemplo n.º 6
0
        internal Category GetCategory(ulong categoryId)
        {
            using (NpgsqlCommand cmd = new NpgsqlCommand())
            {
                cmd.Connection = _connection;
                cmd.CommandText = "SELECT * FROM category where id = @categoryId";
                cmd.Parameters.AddWithValue("categoryId", (long)categoryId);

                NpgsqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    Category category = new Category()
                    {
                        Id = (ulong)categoryId,
                        Name = reader.GetString(reader.GetOrdinal("name"))
                    };
                    return category;
                }
                reader.Close();
                return null;
            }
        }
Ejemplo n.º 7
0
        internal List<Category> GetCategories()
        {
            using (NpgsqlCommand cmd = new NpgsqlCommand())
            {
                cmd.Connection = this._connection;
                cmd.CommandText = "SELECT * from category;";

                NpgsqlDataReader reader = cmd.ExecuteReader();
                List<Category> categories = new List<Category>();

                while (reader.Read())
                {
                    Category c = new Category();
                    c.Id = (ulong)reader.GetInt32(reader.GetOrdinal("id"));
                    c.Name = reader.GetString(reader.GetOrdinal("name"));
                    categories.Add(c);
                }
                reader.Close();
                return categories;
            }
        }
Ejemplo n.º 8
0
        internal List<Category> GetCategories(string pattern = null)
        {
            //Krijg een lijst met categorien
            using (NpgsqlCommand cmd = new NpgsqlCommand())
            {
                cmd.Connection = this._connection;

                    cmd.CommandText = "SELECT * FROM category;";

                NpgsqlDataReader reader = cmd.ExecuteReader(); //intialiseren
                List<Category> categories = new List<Category>();

                while (reader.Read())
                {
                    Category category = new Category();
                    category.Id = (ulong)reader.GetInt32(reader.GetOrdinal("id"));
                    category.Name = reader.GetString(reader.GetOrdinal("name"));

                    categories.Add(category); //Blijf categorien toeveogen
                }

                reader.Close();
                return categories; //return alle categorien
            }
        }