Example #1
0
        public static ItemPO MapDOtoPO(IItemDO iFrom)
        {
            ItemPO oTo = new ItemPO(); //creating a new instance

            //PO            //DO
            oTo.ItemID      = iFrom.ItemID;
            oTo.UserID      = iFrom.UserID;
            oTo.ItemName    = iFrom.ItemName;
            oTo.Used        = iFrom.Used;
            oTo.Description = iFrom.Description;

            return(oTo); //return PO
        }
        public void UpdateItem(IItemDO iItem)
        {
            try
            {
                using (SqlConnection connectionToSql = new SqlConnection(_ConnectionString))
                {
                    using (SqlCommand command = new SqlCommand("UPDATE_ITEMS", connectionToSql))
                    {
                        try
                        {
                            command.CommandType    = CommandType.StoredProcedure;
                            command.CommandTimeout = 30;

                            //Passing parameters in from Sql
                            command.Parameters.AddWithValue("@ItemID", iItem.ItemID);
                            command.Parameters.AddWithValue("@UserID", iItem.UserID);
                            command.Parameters.AddWithValue("@ItemName", iItem.ItemName);
                            command.Parameters.AddWithValue("@Used", iItem.Used);
                            command.Parameters.AddWithValue("@Description", iItem.Description);

                            connectionToSql.Open();
                            command.ExecuteNonQuery();
                        }
                        catch (Exception e)
                        {
                            throw (e); //throw to outside try catch
                        }
                        finally
                        {
                            connectionToSql.Close(); //Saftey
                            connectionToSql.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw (e);  //throw to controller
            }
            finally
            {
                //Onshore standards
            }
        }
        public void CreateItem(IItemDO iItem)
        {
            try
            {
                using (SqlConnection connectionToSql = new SqlConnection(_ConnectionString))
                {
                    using (SqlCommand command = new SqlCommand("CREATE_ITEMS", connectionToSql))
                    {
                        try
                        {
                            command.CommandType    = CommandType.StoredProcedure;
                            command.CommandTimeout = 30;


                            command.Parameters.AddWithValue("@UserID", iItem.UserID);
                            command.Parameters.AddWithValue("@ItemName", iItem.ItemName);
                            command.Parameters.AddWithValue("@Used", iItem.Used);
                            command.Parameters.AddWithValue("@Description", iItem.Description);
                            connectionToSql.Open();
                            command.ExecuteNonQuery();
                        }
                        catch (Exception e)
                        {
                            throw (e); //We are going to log and write the error once it gets to the controller
                        }
                        finally
                        {
                            connectionToSql.Close();
                            connectionToSql.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                //Onshore Standards
            }
        }
        public ActionResult UpdateItem(ItemVM iItem)
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null || (Int16)Session["Role"] == 3)
            {
                //Guest & User
                oResponse = RedirectToAction("Index", "Home");
            }
            else
            {
                if (ModelState.IsValid) //if correct info
                {
                    try
                    {
                        //give mapping a variable to be used
                        IItemDO update = ItemMap.MapPOtoDO(iItem.Item);
                        //Call the method to be used
                        ItemAccess.UpdateItem(update);
                        //Return to the view to see changes
                        oResponse = RedirectToAction("ViewItems", "Item");
                    }
                    catch (Exception e)
                    {           //for the errors thrown below
                        iItem.ErrorMessage = "Sorry your request cannot be processed";
                        ErrorLog.LogError(e);
                        oResponse = RedirectToAction("UpdateItem", "Item");
                    }
                    finally
                    {
                        //Onshore standards
                    }
                }
                else //if it isn't valid information
                {
                    oResponse = View(iItem); //Return to the Update page to fill out info again
                }
            }

            return(oResponse);
        }
        public ActionResult UpdateItem(long ItemID)
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null || (Int16)Session["Role"] == 3)
            {    //Guest & User
                oResponse = RedirectToAction("Index", "Home");
            }
            else
            {
                //Create a new instance of the object
                ItemVM newVM = new ItemVM();
                //set the method to a variable to be used
                IItemDO item = ItemAccess.ViewItemsByID(ItemID);
                //set mapping to a variable
                newVM.Item = ItemMap.MapDOtoPO(item);
                //return view
                oResponse = View(newVM);
            }
            return(oResponse);
        }
        public ActionResult CreateItem(ItemVM iItem)
        {
            ActionResult oResponse = null; //set to null to determine if valid or not

            if (Session["Username"] == null)
            {
                oResponse = RedirectToAction("Index", "Home");
            }
            else
            {
                if (ModelState.IsValid)
                {  //if info correct
                    try
                    {
                        //Mapping into a variable
                        IItemDO Itemform = ItemMap.MapPOtoDO(iItem.Item);
                        //Using the mehtod in DAL to create item
                        ItemAccess.CreateItem(Itemform);
                        //Return the view
                        oResponse = RedirectToAction("ViewItemsByUserID", "User", new { UserID = (long)Session["UserID"] });
                    }
                    catch (Exception e)
                    {
                        iItem.ErrorMessage = "We are sorry, we can not process your request";
                        ErrorLog.LogError(e);    //Log error in file
                        oResponse = View(iItem); //return view
                    }

                    finally
                    {
                        //Onshore Standards
                    }
                }
                else //if info incorrect
                {
                    oResponse = View(iItem); //return to view once on
                }
            }
            return(oResponse); //return view
        }