Ejemplo n.º 1
0
        public async Task <ActionResult <LessonDto> > CreateLesson(
            [FromRoute] Guid classroomId,
            [FromBody] LessonDto lessonDto
            )
        {
            var user = (ApplicationUser)HttpContext.Items["ApplicationUser"];

            Debug.Assert(user != null, nameof(user) + " != null");

            try
            {
                var classroom = await _classroomService.FindAsync(classroomId);

                var authorization = await _authorizationService.AuthorizeAsync(User, classroom, "IsOwner");

                if (!authorization.Succeeded)
                {
                    return(Forbid());
                }

                var lesson = lessonDto.ToLesson();
                lesson = await _lessonService.CreateAsync(lesson);

                await _classroomService.AddLessonAsync(classroom, lesson);

                lesson = await _lessonService.LoadTeachersAsync(lesson);

                await _lessonService.AddTeacherAsync(lesson, user);

                return(Ok(lesson.ToDto()));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(BadRequest());
            }
        }