public async Task <IActionResult> UpdateAvatar(int accountId, [FromForm] PhotoDTO photoDTO) { int userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (userId != accountId) { return(Unauthorized()); } var accountFromDb = await _repo.GetAccountDetail(accountId); if (accountFromDb == null) { return(NotFound()); } if (!string.IsNullOrEmpty(accountFromDb.PublicId)) { _cloud.DeleteImage(accountFromDb.PublicId); } var result = _cloud.UploadImage(photoDTO.File); accountFromDb.PhotoUrl = result.PublicUrl; accountFromDb.PublicId = result.PublicId; if (await _repo.SaveAll()) { return(Ok()); } return(StatusCode(500)); }
public async Task <IActionResult> UpdateQuestion(int id, [FromForm] QuestionCreateDTO questionCreateDTO) { try { var questionFromDb = await _repo.GetOneWithConditionTracking <Question>(q => q.Id == id); if (questionFromDb == null) { return(NotFound(new { Error = "Không tìm thấy câu hỏi" })); } _mapper.Map(questionCreateDTO, questionFromDb); if (questionCreateDTO.File != null) { if (!string.IsNullOrEmpty(questionFromDb.PublicId)) { var deleteResult = _helper.DeleteImage(questionFromDb.PublicId); if (deleteResult) { var uploadResult = _helper.UploadImage(questionCreateDTO.File); if (uploadResult != null) { questionFromDb.PublicId = uploadResult.PublicId; questionFromDb.PhotoUrl = uploadResult.PublicUrl; } } } } if (questionCreateDTO.Audio != null) { var uploadResult = await _dropBox.UploadFile(questionCreateDTO.Audio, "/Engrisk"); if (uploadResult != null) { questionFromDb.Audio = uploadResult.SharedUrl; } } 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> EditSection(int id, [FromForm] SectionUpdateDTO section) { var sectionFromDb = await _repo.GetOneWithConditionTracking <Section>(section => section.Id == id); if (sectionFromDb == null) { return(NotFound()); } _mapper.Map(section, sectionFromDb); if (section.File != null) { if (!string.IsNullOrEmpty(sectionFromDb.PublicId)) { var deleteResult = _cloud.DeleteImage(sectionFromDb.PublicId); if (deleteResult) { var uploadResult = _cloud.UploadImage(section.File); if (uploadResult != null) { sectionFromDb.PublicId = uploadResult.PublicId; sectionFromDb.PhotoUrl = uploadResult.PublicUrl; } } } } if (await _repo.SaveAll()) { return(Ok()); } return(StatusCode(500)); }
public async Task <IActionResult> UpdateWord(int wordId, [FromForm] WordCreateDTO word) { try { var wordFromDb = await _repo.GetOneWithConditionTracking <Word>(word => word.Id == wordId); if (wordFromDb == null) { return(NotFound()); } _mapper.Map(word, wordFromDb); if (word.File != null) { if (!string.IsNullOrEmpty(wordFromDb.PublicId)) { var deleteResult = _cloud.DeleteImage(wordFromDb.PublicId); if (deleteResult) { var uploadResult = _cloud.UploadImage(word.File); if (uploadResult != null) { wordFromDb.WordImg = uploadResult.PublicUrl; wordFromDb.PublicId = uploadResult.PublicId; } } } } if (word.Audio != null) { var audioUploadResult = await _dropbox.UploadFile(word.Audio, "/Engrisk"); if (audioUploadResult != null) { wordFromDb.WordVoice = audioUploadResult.SharedUrl; } } if (await _repo.SaveAll()) { return(Ok()); } return(NoContent()); } catch (System.Exception e) { throw e; } }
public async Task <IActionResult> DeleteBanner(int id) { var bannerFromDb = await _repo.GetOneWithCondition <Banner>(banner => banner.Id == id); if (bannerFromDb == null) { return(NotFound()); } if (_cloudHelper.DeleteImage(bannerFromDb.PublicId)) { _repo.Delete(bannerFromDb); if (await _repo.SaveAll()) { return(NoContent()); } return(StatusCode(500)); } return(StatusCode(500)); }