コード例 #1
0
        public async Task <IActionResult> StartCourse([FromBody] CourseStartOptions startOptions, [FromQuery] string userId)
        {
            if (startOptions == null)
            {
                return(BadRequest("CourseStartOptions is null"));
            }

            if (!User.IsInRole("Client") || string.IsNullOrEmpty(userId))
            {
                userId = User.GetId();
            }

            if (!IdIsValid(userId))
            {
                return(Forbid($"The user has no rights or the id = {userId} is invalid"));
            }

            var course = await courseRepository.GetAsync(startOptions.CourseId);

            if (course == null)
            {
                return(BadRequest($"Invalid course id = {startOptions.CourseId}"));
            }

            if (await progressRepository.ContainsAsync(userId, course.Id))
            {
                return(BadRequest($"User id = {userId} hasn't course with id = {startOptions.CourseId}"));
            }

            var courseProgress = course.CreateProgress(userId);

            courseProgress.Id = await progressRepository.InsertAsync(courseProgress);

            courseProgress.SetUpLinks();

            var uri = Request.GetUri();

            return(Created($"{uri}/{courseProgress.CourceId}", courseProgress));
        }