//Create public void CreateGame(Game game, HttpPostedFileBase file) { if (game.Lifes < 1 || game.Lifes > 5) { throw new Exception("De levens moet tussen de 1 en de 5 zijn!"); } if (game.BonusTime < 1 || game.BonusTime > 10) { throw new Exception("De bonustijd moet tussen de 1 en de 10 zijn!"); } if (game.Name.Length < 3 || game.Name.Length > 25) { throw new Exception("De naam moet tussen de 3 en 25 characters bevatten!"); } Models.DataModels.Game NewGame = new Models.DataModels.Game() { bonusTime = game.BonusTime, lifes = game.Lifes, name = game.Name, Online = game.Online }; string Image = ""; if (file != null) { Image = UploadHandler.UploadImage(file, UploadHandler.GetImageSize(PreDefImageSize.GameImage), PreDefImageLocation.GameImage).Name; string img = UploadHandler.GetImageUrlPath(PreDefImageLocation.GameImage) + Image; //SQL.Insert("INSERT INTO `chartgroup`(`ID`) VALUES(NULL);INSERT INTO `game` (`ID`, `name`, `lifes`, `bonusTime`,`online`, `ChartGroupID`, `img`) VALUES (NULL, '" + game.Name + "', '" + game.Lifes + "', '" + game.BonusTime + "', '1',( SELECT MAX(`chartgroup`.`ID`) FROM `chartgroup`), '" + img + "');"); NewGame.img = img; } dbContext.ChartGroup.Add(new Models.DataModels.ChartGroup()); dbContext.SaveChanges(); NewGame.ChartGroupID = dbContext.ChartGroup.OrderByDescending(o => o.ID).FirstOrDefault().ID; dbContext.Game.Add(NewGame); dbContext.SaveChanges(); }
//update public void UpdateImageQuestion(ImageQuestion question, HttpPostedFileBase file) { if (dbContext.ImageQuestion.Where(q => q.ID == question.ID).Count() == 0) { throw new Exception("Er is geen vraag gevonden met het ID : `" + question.ID.ToString() + "`"); } if (question.Question.Length < 5 || question.Question.Length > 50) { throw new Exception("Een vraag moet tussen de 5 en 50 characters bevatten!"); } if (question.Question.Split(' ').Count() < 3) { throw new Exception("Een vraag moet uit meer dan 3 woorden bestaan!"); } if (question.X1 == 0 || question.Y1 == 0) { throw new Exception("Er moet een antwoord gegeven worden!"); } if (file != null) { string img = UploadHandler.UploadImage(file, UploadHandler.GetImageSize(PreDefImageSize.QuestionImage), PreDefImageLocation.QuestionImage).Name; question.img = "/" + UploadHandler.GetImageUrlPath(PreDefImageLocation.QuestionImage) + img; } Models.DataModels.ImageQuestion iq = dbContext.ImageQuestion.Where(q => q.ID == question.ID).First(); iq.answerX1 = question.X1; iq.answerX2 = question.X2; iq.answerY1 = question.Y1; iq.answerY2 = question.Y2; iq.answerRatio = question.Radius; iq.Question = question.Question; iq.img = question.img; dbContext.SaveChanges(); }
//Uploads the provided UploadHandler class private static UploadHandler Upload(UploadHandler uploadFile) { if (uploadFile.Type == FileType.Image) { if (uploadFile.Name == null) { uploadFile.Name = DateTime.Now.ToString("yyyMMddHHmm"); } uploadFile.Folder = Path.Combine(uploadFile.PathToRoot, UPLOAD_PATH, uploadFile.Folder); uploadFile.Folder += uploadFile.ImageFolder; Directory.CreateDirectory(uploadFile.Folder); uploadFile.Url = Path.Combine(uploadFile.PathToRoot, uploadFile.Folder, uploadFile.Name); int count = 0; while (System.IO.File.Exists(uploadFile.Url)) { count++; uploadFile.Url = Path.Combine(uploadFile.PathToRoot, uploadFile.Folder, uploadFile.Name + "(" + count.ToString() + ")"); } if (count > 0) { uploadFile.Name += "(" + count.ToString() + ").jpeg"; } else { uploadFile.Name += ".jpeg"; } uploadFile.Url += ".jpeg"; uploadFile.Extention = ".jpeg"; Image image = Image.FromStream(uploadFile.File.InputStream, true, true); Point newImageSize = new Point(image.Width, image.Height); if (uploadFile.isCrop) { int widthDiff = newImageSize.X - uploadFile.Size.X; int heigthDiff = newImageSize.Y - uploadFile.Size.Y; int ratio = 0; if (widthDiff > heigthDiff || widthDiff == heigthDiff) { ratio = uploadFile.Size.Y / newImageSize.Y; } else { ratio = uploadFile.Size.X / newImageSize.X; } newImageSize.X = uploadFile.Size.X * ratio; newImageSize.Y = uploadFile.Size.Y * ratio; } else if (uploadFile.Size.X > 0 && uploadFile.Size.Y > 0) { newImageSize = uploadFile.Size; } var newImage = new Bitmap(newImageSize.X, newImageSize.Y); using (var g = Graphics.FromImage(newImage)) { g.DrawImage(image, uploadFile.Crop.X, uploadFile.Crop.Y, newImageSize.X, newImageSize.Y); } Image uploadImage = newImage; uploadImage.Save(uploadFile.Url, ImageFormat.Jpeg); } return(uploadFile); }
//Create public void CreateImageQuestion(ImageQuestion question, HttpPostedFileBase file, int GameID) { if (dbContext.Game.Where(g => g.ID == GameID).Count() == 0) { throw new Exception("Er is geen game gevonden met het ID : `" + GameID.ToString() + "`"); } if (question.Question.Length < 5 || question.Question.Length > 50) { throw new Exception("Een vraag moet tussen de 5 en 50 characters bevatten!"); } if (question.Question.Split(' ').Count() < 3) { throw new Exception("Een vraag moet uit meer dan 3 woorden bestaan!"); } if (question.X1 == 0 || question.Y1 == 0) { throw new Exception("Er moet een antwoord gegeven worden!"); } if (file == null) { throw new Exception("Je moet een afbeelding uploaden!"); } string img = UploadHandler.UploadImage(file, UploadHandler.GetImageSize(PreDefImageSize.QuestionImage), PreDefImageLocation.QuestionImage).Name; question.img = "/" + UploadHandler.GetImageUrlPath(PreDefImageLocation.QuestionImage) + img; Models.DataModels.ImageQuestion imageQuestion = new Models.DataModels.ImageQuestion() { answerRatio = question.Radius, answerX1 = question.X1, answerX2 = question.X2, answerY1 = question.Y1, answerY2 = question.Y2, Question = question.Question, img = question.img, ID = question.ID }; dbContext.ImageQuestion.Add(imageQuestion); dbContext.SaveChanges(); //string qry = "INSERT INTO `imagequestion` (`ID`, `img`, `question`, `answerX1`, `answerX2`, `answerY1`, `answerY2`, `answerRatio`) VALUES (NULL, '"+question.img+"', '"+question.Question+"', '"+question.X1+"', '"+question.X2+"', '"+question.Y1+"', '"+question.Y2+"', '"+question.Radius+"')"; //SQL.Insert(qry); int questionID = dbContext.ImageQuestion.OrderByDescending(o => o.ID).FirstOrDefault().ID; dbContext.ChartGroup.Add(new Models.DataModels.ChartGroup()); dbContext.SaveChanges(); int chartGroupID = dbContext.ChartGroup.OrderByDescending(o => o.ID).FirstOrDefault().ID; Models.DataModels.Question q = new Models.DataModels.Question() { ChartGroupID = chartGroupID, GameID = GameID, QuestionType = Models.DataModels.QuestionType.ImageQuestion, QuestionTypeID = questionID }; dbContext.Question.Add(q); dbContext.SaveChanges(); //string QuestionID = SQL.Select("SELECT `ID` FROM `imagequestion` WHERE `question` = '"+question.Question+ "' AND `img` = '"+question.img+"'").Last(); //SQL.Insert("INSERT INTO `chartgroup`(`ID`) VALUES(NULL); INSERT INTO `question` (`ID`, `gameID`, `Type`, `QuestionTypeID`, `ChartGroupID`) VALUES (NULL, '" + GameID.ToString()+"', '0', '"+QuestionID+ "', ( SELECT MAX(`chartgroup`.`ID`) FROM `chartgroup`))"); }
//Uploads the provided UploadHandler class private static UploadHandler Upload(UploadHandler UploadFile) { if (UploadFile.Type == FileType.Image) { if (UploadFile.Name == null) { UploadFile.Name = DateTime.Now.ToString("yyyMMddHHmm"); } UploadFile.Folder = Path.Combine(UploadFile.PathToRoot, UploadPath, UploadFile.Folder); UploadFile.Folder += UploadFile.ImageFolder; Directory.CreateDirectory(UploadFile.Folder); UploadFile.Url = Path.Combine(UploadFile.PathToRoot, UploadFile.Folder, UploadFile.Name); int count = 0; while (System.IO.File.Exists(UploadFile.Url)) { count++; UploadFile.Url = Path.Combine(UploadFile.PathToRoot, UploadFile.Folder, UploadFile.Name + "(" + count.ToString() + ")"); } if (count > 0) { UploadFile.Name += "(" + count.ToString() + ").jpeg"; } else { UploadFile.Name += ".jpeg"; } UploadFile.Url += ".jpeg"; UploadFile.Extention = ".jpeg"; Image Image = Image.FromStream(UploadFile.File.InputStream); Point NewImageSize = new Point(Image.Width, Image.Height); if (UploadFile.isCrop) { int widthDiff = NewImageSize.X - UploadFile.Size.X; int heigthDiff = NewImageSize.Y - UploadFile.Size.Y; int ratio = 0; if (widthDiff > heigthDiff || widthDiff == heigthDiff) { ratio = UploadFile.Size.Y / NewImageSize.Y; } else { ratio = UploadFile.Size.X / NewImageSize.X; } NewImageSize.X = UploadFile.Size.X * ratio; NewImageSize.Y = UploadFile.Size.Y * ratio; } else if (UploadFile.Size.X > 0 && UploadFile.Size.Y > 0) { NewImageSize = UploadFile.Size; } var newImage = new Bitmap(NewImageSize.X, NewImageSize.Y); using (var g = Graphics.FromImage(newImage)) { g.DrawImage(Image, UploadFile.Crop.X, UploadFile.Crop.Y, NewImageSize.X, NewImageSize.Y); } Image uploadImage = newImage; uploadImage.Save(UploadFile.Url, ImageFormat.Jpeg); } return(UploadFile); }