private IActionResult FileValidityChecker(NewQuizViewModel quizBodyData) { IActionResult statusCodeResult = Ok(); if (quizBodyData.Files.Count == 0) { statusCodeResult = BadRequest("An image file need to be provided."); } else if (quizBodyData.Files.Count > 1) { statusCodeResult = BadRequest("Only one file can be uploaded."); } long size = quizBodyData.Files.Sum(f => f.Length); if (size <= 0) { statusCodeResult = BadRequest("File must not be empty."); } var contentType = quizBodyData.Files[0].ContentType; if (!(contentType == "image/gif" || contentType == "image/png" || contentType == "image/jpeg")) { statusCodeResult = BadRequest("Only images of the following formats are allowed: .png, .jpeg or .gif"); } return(statusCodeResult); }
public IActionResult CreateQuiz([FromForm] NewQuizViewModel quizData) { var song = songService.GetSongById(quizData.SongId); quizService.CreateNewQuiz(song, quizData.Answer1, quizData.Answer2, quizData.RightAnswer); return(RedirectToAction("Index")); }
public IActionResult CreateQuiz() { NewQuizViewModel quiz = new NewQuizViewModel() { SongList = GetSongList() }; return(PartialView("_NewQuizPartial", quiz)); }
public async Task <ActionResult> NewQuiz(NewQuizViewModel model) { //check if Model is valid if (!ModelState.IsValid) { model.PageInfo = new PageInfo() { PageTitle = "Create New Quiz", PageDescription = "Create new quiz." }; model.QuizTypes = new List <QuizType>() { QuizType.Text, QuizType.Image }; return(View(model)); } //populate new Quiz from Model var quiz = new Quiz(); var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); quiz.OwnerID = userId; quiz.Name = model.Name; quiz.Description = model.Description; quiz.QuizType = model.SelectedQuizType; quiz.EnableQuizTimer = model.EnableQuizTimer; if (quiz.EnableQuizTimer) { quiz.TimeDuration = new TimeSpan(model.Hours, model.Minutes, 0); quiz.EnableQuestionTimer = model.EnableQuestionTimer; } else { quiz.TimeDuration = new TimeSpan(0, 0, 0); quiz.EnableQuestionTimer = false; } quiz.ModifiedOn = DateTime.Now; if (await QuizzesService.Instance.SaveNewQuiz(quiz)) { //now redirect to New Question screen for this new Quiz return(RedirectToAction("NewQuestion", "Question", new { quizID = quiz.ID })); } else { return(StatusCode(500)); } }
public async Task <IActionResult> Put(NewQuizViewModel quizBodyData, int id) { int currentUserId = User.Claims.GetUserId(); IActionResult imageHttpResponse = FileValidityChecker(quizBodyData); if (IActionResult.Equals(imageHttpResponse, Ok())) { return(imageHttpResponse); } string fileUrl; AzureBlobService blobService; try { blobService = new AzureBlobService(); await blobService.InitializeBlob(); var previousQuizState = quizRepository.GetQuizById(id); blobService.DeletePhoto(previousQuizState.PictureUrl); fileUrl = await blobService.UploadPhoto(quizBodyData.Files[0]); } catch (Exception) { return(BadRequest("Image could not be uploaded.")); } Quiz updatedQuiz = quizRepository.UpdateQuiz(new Quiz { Title = quizBodyData.Title, PictureUrl = fileUrl, UserId = currentUserId, Id = id }); if (updatedQuiz == null) { return(BadRequest("Quiz does not exist.")); } QuizViewModel quizVm = entityToVmMapper.Map(updatedQuiz); quizVm.PictureUrl = blobService.urlPath.AbsoluteUri.ToString() + "users/" + fileUrl; return(Ok(new { quizVm.Id, quizVm.Title, quizVm.PictureUrl })); }
public ActionResult NewQuiz() { NewQuizViewModel model = new NewQuizViewModel(); model.PageInfo = new PageInfo() { PageTitle = "Create New Quiz", PageDescription = "Create new quiz." }; model.EnableQuizTimer = true; //set Default Quiz Timer to true model.QuizTypes = new List <QuizType>() { QuizType.Text, QuizType.Image }; model.SelectedQuizType = QuizType.Text; return(View(model)); }
public IActionResult OnPost(NewQuizViewModel model) { //check if Model is valid if (!ModelState.IsValid) { QuizViewModel = model; return(Page()); } //populate new Quiz from Model var quiz = new Quiz(); quiz.OwnerID = User.Claims.ToList().FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; quiz.Name = model.Name; quiz.Description = model.Description; quiz.IsActive = true; quiz.EnableQuizTimer = model.EnableQuizTimer; if (quiz.EnableQuizTimer) { quiz.TimeDuration = new TimeSpan(model.Hours, model.Minutes, 0); quiz.EnableQuestionTimer = model.EnableQuestionTimer; } else { quiz.TimeDuration = new TimeSpan(0, 0, 0); quiz.EnableQuestionTimer = false; } _context.Quizzes.Add(quiz); if (_context.SaveChanges() > 0) { return(Redirect("~/admin/question?quizId=" + quiz.Id)); } else { return(StatusCode(500)); } }
public void OnGet() { QuizViewModel = new NewQuizViewModel(); QuizViewModel.EnableQuizTimer = true; //set Default Quiz Timer to true }