Esempio n. 1
0
        public void CreateNewItem(ItemDO item)
        {
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(connectionString))
                    using (SqlCommand sqlCMD = new SqlCommand("ITEMS_CREATE", sqlCon))
                    {
                        sqlCMD.CommandType = System.Data.CommandType.StoredProcedure;

                        sqlCMD.Parameters.AddWithValue("ItemName", item.Name);
                        sqlCMD.Parameters.AddWithValue("Description", item.Description);
                        sqlCMD.Parameters.AddWithValue("Image", item.ImagePath);
                        sqlCMD.Parameters.AddWithValue("Purchasable", item.Purchasable);
                        sqlCMD.Parameters.AddWithValue("Validate", item.Validated);

                        sqlCon.Open();
                        sqlCMD.ExecuteNonQuery();
                    }
            }
            catch (SqlException sqlEx)
            {
                Logger.LogSqlException(sqlEx);
                sqlEx.Data["Logged"] = true;
                throw sqlEx;
            }
        }
        public List <IItemDO> ViewAllItems()
        {
            //Creating new instance of a list
            List <IItemDO> viewItems = new List <IItemDO>();

            try
            {
                using (SqlConnection connectionToSql = new SqlConnection(_ConnectionString))
                {
                    using (SqlCommand storedCommand = new SqlCommand("VIEW_ITEMS", connectionToSql))
                    {
                        try
                        {
                            storedCommand.CommandType    = CommandType.StoredProcedure;
                            storedCommand.CommandTimeout = 30;

                            connectionToSql.Open();
                            using (SqlDataReader commandReader = storedCommand.ExecuteReader())
                            {
                                while (commandReader.Read())
                                {
                                    IItemDO item = new ItemDO() //new instance of and object
                                    {                           //properties
                                        ItemID      = commandReader.GetInt64(0),
                                        UserID      = commandReader.GetInt64(1),
                                        ItemName    = commandReader.GetString(2),
                                        Used        = commandReader.GetBoolean(3),
                                        Description = commandReader.GetString(4)
                                    };
                                    viewItems.Add(item); //Add each Item one by one with reader
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw (e); //throw to outer try catch
                        }
                        finally
                        {
                            connectionToSql.Close();     //Saftey
                            connectionToSql.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw (e); //throw exception to the student controller
            }
            finally
            {
                //Onshore standards
            }
            return(viewItems); //return the list viewItems
        }
Esempio n. 3
0
        public PartialViewResult ViewDropsFrom(int id)
        {
            PartialViewResult response = new PartialViewResult();

            //try to connect to the database via multiple DAO's
            try
            {
                //creating and populating a list, describing Enemy and Item Relationship based on a JunctionTable
                List <EnemyItemDO> doList = linkDAO.ViewByItemID(id);

                //gathering selected items information for View Model
                ItemDO tempItem = iDAO.ViewItemSingle(id);
                ItemPO viewItem = Mapper.Mapper.ItemDOtoPO(tempItem);

                //gathering enemy information using the EnemyID's found in out Enemy Item Relationship list
                List <EnemyDO> tempEnemyList = new List <EnemyDO>();
                foreach (EnemyItemDO item in doList)
                {
                    //adding each Enemy to our EnemyDO list
                    tempEnemyList.Add(eDAO.ViewSingleEnemy(item.EnemyID));
                }

                //converting our EnemyDO List to an EnemyPO List
                List <EnemyPO> enemyList = Mapper.Mapper.EnemyDOListToPO(tempEnemyList);

                //instantiatin our View Model
                ItemDropsVM itemVM = new ItemDropsVM();

                //settingg View Model values to our collected information
                //---Item information
                //---EnemyPO List
                itemVM.item    = viewItem;
                itemVM.enemies = enemyList;

                //setting our response to our target Partial View, passing in our View Model
                response = PartialView("_ViewDropsFrom", itemVM);
            }
            //catch and log any unlogged sqlExceptions
            catch (SqlException sqlEx)
            {
                if (!((bool)sqlEx.Data["Logged"] == true) || !sqlEx.Data.Contains("Logged"))
                {
                    Logger.LogSqlException(sqlEx);
                }
            }
            catch (Exception ex)
            {
                if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false)
                {
                    Logger.LogException(ex);
                }
            }

            return(response);
        }
        public IItemDO ViewItemsByID(long ItemID)
        {
            IItemDO viewItems = new ItemDO();

            try
            {
                using (SqlConnection connectionToSql = new SqlConnection(_ConnectionString))
                {
                    using (SqlCommand command = new SqlCommand("VIEW_ITEMS_BY_ID", connectionToSql))
                    {
                        try
                        {
                            command.CommandType    = CommandType.StoredProcedure;
                            command.CommandTimeout = 30;
                            command.Parameters.AddWithValue("@ItemID", ItemID);

                            connectionToSql.Open();

                            using (SqlDataReader commandReader = command.ExecuteReader())
                            {
                                while (commandReader.Read())
                                {
                                    viewItems.ItemID      = commandReader.GetInt64(0);
                                    viewItems.UserID      = commandReader.GetInt64(1);
                                    viewItems.ItemName    = commandReader.GetString(2);
                                    viewItems.Used        = commandReader.GetBoolean(3);
                                    viewItems.Description = commandReader.GetString(4);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw (e);
                        }
                        finally
                        {
                            connectionToSql.Close();
                            connectionToSql.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                //Onshore standards
            }
            return(viewItems);
        }
Esempio n. 5
0
        public static ItemPO ItemDOtoPO(ItemDO from)
        {
            ItemPO to = new ItemPO();

            to.ItemID      = from.ItemID;
            to.Name        = from.Name;
            to.Description = from.Description;
            to.ImagePath   = from.ImagePath;
            to.Purchasable = from.Purchasable;
            to.Validated   = from.Validated;

            return(to);
        }
Esempio n. 6
0
        public static IItemDO MapPOtoDO(ItemPO iFrom)
        {
            IItemDO oTo = new ItemDO();//creating new instance

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

            return(oTo); //return DO
        }
Esempio n. 7
0
        //mapper to map a single itemms data from the server to an ItemDO object
        public static ItemDO MapSingleItem(SqlDataReader from)
        {
            ItemDO to = new ItemDO();

            to.ItemID      = (int)from["ItemID"];
            to.Name        = from["ItemName"] as string;
            to.Description = from["Description"] as string;
            to.ImagePath   = from["Image"] as string;
            to.Purchasable = (bool)from["Purchasable"];
            to.Validated   = (bool)from["Validate"];

            return(to);
        }
Esempio n. 8
0
        public static QuizDO QuizFixture_1()
        {
            var quiz = new QuizDO();

            quiz.AnswerTime = 3000;

            var questionLabel = new Label {
                Text = "What is the highest mountain of the world?"
            };
            var questionImage = new Image {
                ImagePath = "cdn://dksjdksjkd.com/image.png"
            };
            var questionContent = new ContentDO();

            questionContent.AddControl(questionLabel);
            questionContent.AddControl(questionImage);

            var question = new QuestionDO();

            question.Content = questionContent;

            var quizItem = new ItemDO();

            quizItem.SetQuestion(question);

            for (int i = 0; i < 4; ++i)
            {
                var answerLabel = new Label {
                    Text = "Everest"
                };
                var answerImage = new Image {
                    ImagePath = "cdn://dkhfjdkl.io/everest.png"
                };
                var answerContent = new ContentDO();
                answerContent.AddControl(answerLabel);
                answerContent.AddControl(answerImage);

                var answer = new AnswerDO();
                answer.Content = answerContent;

                quizItem.AddPossibleAnswer(answer);
            }

            quizItem.SetCorrectAnswer(new SingleChoiceAnswer {
                Value = 1
            });

            quiz.AddQuizItem(quizItem);

            return(quiz);
        }
Esempio n. 9
0
        public ItemDO ViewItemByName(string itemName)
        {
            ItemDO item = new ItemDO();

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                    using (SqlCommand ViewByName = new SqlCommand("ITEMS_VIEW_BY_NAME", sqlConnection))
                    {
                        ViewByName.CommandType = System.Data.CommandType.StoredProcedure;
                        ViewByName.Parameters.AddWithValue("Name", itemName);

                        sqlConnection.Open();
                        using (SqlDataReader reader = ViewByName.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                item = Mapper.MapSingleItem(reader);
                            }
                        }
                    }
            }
            catch (SqlException sqlEx)
            {
                Logger.LogSqlException(sqlEx);
                sqlEx.Data["Logged"] = true;
                throw sqlEx;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                ex.Data["Logged"] = true;
                throw ex;
            }

            return(item);
        }
Esempio n. 10
0
        public ItemDO ViewItemSingle(int id)
        {
            ItemDO item = new ItemDO();

            using (SqlConnection sqlCon = new SqlConnection(connectionString))
                using (SqlCommand sqlCMD = new SqlCommand("ITEMS_VIEW_SINGLE", sqlCon))
                {
                    try
                    {
                        sqlCMD.CommandType = System.Data.CommandType.StoredProcedure;
                        sqlCMD.Parameters.AddWithValue("ItemID", id);

                        sqlCon.Open();

                        using (SqlDataReader reader = sqlCMD.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                item = Mapper.MapSingleItem(reader);
                            }
                        }
                    }
                    catch (SqlException sqlEx)
                    {
                        Logger.LogSqlException(sqlEx);
                        sqlEx.Data["Logged"] = true;
                        throw sqlEx;
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex);
                        ex.Data["Logged"] = true;
                        throw ex;
                    }
                }
            return(item);
        }
Esempio n. 11
0
        public List <IItemDO> ViewItemsbyUserID(long UserID)
        {
            List <IItemDO> userItems = new List <IItemDO>(); //Create new instance of list

            try                                              //Catch Exception
            {
                //Create connection
                using (SqlConnection connectionToSql = new SqlConnection(_ConnectionString))
                {       //Create command
                    using (SqlCommand command = new SqlCommand("VIEW_ITEMS_BY_USER_ID", connectionToSql))
                    {
                        try
                        {       //command Properties
                            command.CommandType    = CommandType.StoredProcedure;
                            command.CommandTimeout = 30;
                            //View that one users info
                            command.Parameters.AddWithValue("@UserID", UserID);

                            connectionToSql.Open();

                            using (SqlDataReader commandReader = command.ExecuteReader())
                            {
                                while (commandReader.Read())
                                {
                                    //Read each property
                                    #region Properties
                                    ItemDO item = new ItemDO()//Create a new instance
                                    {
                                        ItemID      = commandReader.GetInt64(0),
                                        UserID      = commandReader.GetInt64(1),
                                        ItemName    = commandReader.GetString(2),
                                        Used        = commandReader.GetBoolean(3),
                                        Description = commandReader.GetString(4)
                                    };
                                    userItems.Add(item);
                                    #endregion
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw (e); //throw to outer try catch
                        }
                        finally
                        {
                            connectionToSql.Close(); //Saftey
                            connectionToSql.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogError.Log(e);
                throw (e); //throw to the controller
            }
            finally
            {
                //Onshore standards
            }
            return(userItems); //return List
        }
Esempio n. 12
0
        public ActionResult CreateItem(ItemVM form)
        {
            ActionResult response;

            //Makes sure everything required was entered in the view
            if (ModelState.IsValid)
            {
                ItemDO itemCheck = iDAO.ViewItemByName(form.Item.Name);

                if (itemCheck.ItemID == 0)
                {
                    //try to connect to the server and create a new item
                    try
                    {
                        form.Item.Validated = false;

                        form.Item.ImagePath = "~/Images/Items/" + form.Item.Name + ".png";

                        if (System.IO.File.Exists(Server.MapPath(form.Item.ImagePath)))
                        {
                            System.IO.File.Delete(Server.MapPath(form.Item.ImagePath));
                        }

                        if (form.File != null && form.File.ContentLength > 0)
                        {
                            string newImage = Server.MapPath(form.Item.ImagePath);
                            if (System.IO.File.Exists(newImage))
                            {
                                System.IO.File.Delete(newImage);
                            }

                            string path = Server.MapPath(form.Item.ImagePath);
                            form.File.SaveAs(path);
                        }
                        else
                        {
                            string oldImage = Server.MapPath("~/Images/Items/Item.png");

                            string newImage = Server.MapPath(form.Item.ImagePath);

                            System.IO.File.Copy(oldImage, newImage);
                        }

                        iDAO.CreateNewItem(Mapper.Mapper.ItemPOtoDO(form.Item));

                        response = RedirectToAction("Index", "Item");
                    }
                    //catch and log any unloged sqlExceptions encountered during db call
                    catch (SqlException sqlEx)
                    {
                        if (!((bool)sqlEx.Data["Logged"] == true) || !sqlEx.Data.Contains("Logged"))
                        {
                            Logger.LogSqlException(sqlEx);
                        }
                        response = RedirectToAction("Index", "Item");
                    }
                    catch (Exception ex)
                    {
                        if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false)
                        {
                            Logger.LogException(ex);
                        }
                        response = RedirectToAction("Index", "Item");
                    }
                }
                else
                {
                    ModelState.AddModelError("Item.Name", "Item already exists!");
                    response = View(form);
                }
            }
            //returns to view if modelstate was false
            else
            {
                ModelState.AddModelError("Item.Description", "Missing information, please fill in all fields!");
                response = View(form);
            }

            return(response);
        }