public async Task <IActionResult> UpdateCourse(int id, CourseForUpdate courseForUpdate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var accountId = _userRepository.GetUserAccountId(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)); if (!await _courseRepository.CanEdit(id, accountId)) { return(Unauthorized()); } try { if (await _courseRepository.Update(id, courseForUpdate)) { return(Ok()); } return(StatusCode(500, new ErrorResponse { ErrorMessage = "An error occurred during update course. Try again later." })); } catch (CourseNotFoundException) { return(BadRequest(new ErrorResponse { ErrorMessage = "Course has not been found." })); } }
public async Task <IActionResult> CreateFlashcard(int courseId, int lessonId, FlashcardForCreate flashcardForCreate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var accountId = _userRepository.GetUserAccountId(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)); if (!await _courseRepository.CanEdit(courseId, accountId)) { return(Unauthorized()); } var flashcardEntity = _mapper.Map <Flashcard>(flashcardForCreate); try { var flashcardToReturnRepo = _flashcardsRepository.Create(lessonId, flashcardEntity).Result; if (flashcardToReturnRepo != null) { var flashcardToReturnMapped = _mapper.Map <FlashcardForDetail>(flashcardToReturnRepo); return(Ok(flashcardToReturnMapped)); } return(StatusCode(500, new ErrorResponse { ErrorMessage = "An error occurred during create flashcard. Try again later." })); } catch (LessonNotFoundException) { return(NotFound(new ErrorResponse { ErrorMessage = "Lesson has not been found." })); } }
public async Task <IActionResult> CreateLesson(int courseId, LessonForCreate lessonForCreate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var accountId = _userRepository.GetUserAccountId(userId); if (!await _courseRepository.CanEdit(courseId, accountId)) { return(Unauthorized()); } var lessonEntity = _mapper.Map <Lesson>(lessonForCreate); try { if (await _lessonRepository.Create(courseId, lessonEntity)) { var lessonToReturn = _mapper.Map <LessonForDetail>(lessonEntity); return(Ok(lessonToReturn)); } return(StatusCode(500, new ErrorResponse { ErrorMessage = "An error occurred during create new lesson. Try again later." })); } catch (CourseNotFoundException) { return(NotFound(new ErrorResponse { ErrorMessage = "Course has not been found." })); } }