public async Task <IActionResult> CreateQuiz([FromForm] QuizCreateDTO quizCreateDTO) { try { var quiz = _mapper.Map <Quiz>(quizCreateDTO); if (quizCreateDTO.File != null) { var result = _cloud.UploadImage(quizCreateDTO.File); if (result != null) { quiz.PublicId = result.PublicId; quiz.QuizPhoto = result.PublicUrl; } ; } _repo.Create(quiz); if (await _repo.SaveAll()) { return(Ok()); } return(NoContent()); } catch (System.Exception e) { throw e; } }
public async Task <IActionResult> UpdateQuiz(int id, [FromForm] QuizCreateDTO quiz) { var quizFromDb = await _repo.GetOneWithConditionTracking <Quiz>(u => u.Id == id); if (quizFromDb == null) { return(NotFound(new { Error = "Không tìm thấy quiz" })); } _mapper.Map(quiz, quizFromDb); if (quiz.File != null) { if (!string.IsNullOrEmpty(quizFromDb.PublicId)) { var deleteResult = _cloud.DeleteImage(quizFromDb.PublicId); if (deleteResult) { var uploadResult = _cloud.UploadImage(quiz.File); if (uploadResult != null) { quizFromDb.PublicId = uploadResult.PublicId; quizFromDb.QuizPhoto = uploadResult.PublicUrl; } } } } if (await _repo.SaveAll()) { return(Ok()); } throw new Exception("Error on updating quiz"); }
public async Task <IActionResult> CreateQuiz(int id, [FromForm] QuizCreateDTO quizDTO) { try { var sectionFromDb = await _repo.GetOneWithCondition <Section>(section => section.Id == id); if (sectionFromDb == null) { return(NotFound()); } Quiz quiz = new Quiz(); quiz = _mapper.Map(quizDTO, quiz); var result = _cloud.UploadImage(quizDTO.File); if (result != null) { quiz.PublicId = result.PublicId; quiz.QuizPhoto = result.PublicUrl; } ; _repo.Create(quiz); quiz.Section = sectionFromDb; if (await _repo.SaveAll()) { return(Ok()); } throw new Exception("Error on create new quiz to section " + sectionFromDb.Id); } catch (System.Exception e) { throw e; } }