public async Task <ActionResult> RunSolution([FromBody] SolutionRunRequest solutionToRun) { // check if student exists var student = await _context.Students.FindAsync(solutionToRun.StudentId); if (student == null) { return(NotFound()); } // check if problem exists var courseProblem = await _context.CourseProblems .Include(cp => cp.Problem) .ThenInclude(p => p.TestCases) .FirstOrDefaultAsync(p => p.Id == solutionToRun.CourseProblemId); if (courseProblem == null) { return(NotFound()); } // solution doesnt exist, create. else update source code var solution = await _context.Solutions.FindAsync(solutionToRun.SolutionId); if (solution == null) { solution = _mapper.Map <Solution> (solutionToRun); _context.Solutions.Add(solution); } else { solution.SourceCode = solutionToRun.SourceCode; } await _context.SaveChangesAsync(); var codeExecutionRequest = new CodeExecutionRequest() { SolutionId = solution.Id, SourceCode = solutionToRun.SourceCode, TestCases = _mapper.Map <List <TestCaseRequest> > (courseProblem.Problem.TestCases) }; var results = await _codeExecutionService.ExecuteAsync(codeExecutionRequest); return(Ok(results)); }
public async Task <IActionResult> CreateCourse( [FromBody] CourseCreateRequest courseToCreate, [FromServices] IOptions <ApiBehaviorOptions> apiBehaviorOptions ) { // find the instructor that wants to create the course var instructor = await _context.Instructors.FindAsync(courseToCreate.InstructorId); if (instructor == null) { return(NotFound()); } // instructor can't create course of the same course name to their own courses. but 2 different instructors, can have same course name var duplicateCourse = await _context.Courses.AnyAsync(c => c.InstructorId == instructor.Id && c.CourseName.ToUpper() == courseToCreate.CourseName.ToUpper() && c.Term.ToUpper() == courseToCreate.Term.ToUpper() && c.Section.ToUpper() == courseToCreate.Section.ToUpper() ); if (duplicateCourse) { ModelState.AddModelError(nameof(courseToCreate.CourseName), $"Error! Duplicate Course Exists..."); return(apiBehaviorOptions.Value.InvalidModelStateResponseFactory(ControllerContext)); } // map request to entity var course = _mapper.Map <Course> (courseToCreate); // save the course _context.Courses.Add(course); await _context.SaveChangesAsync(); // return response at location var courseToReturn = _mapper.Map <CourseResponse> (course); return(CreatedAtAction(nameof(GetCourse), new { courseId = courseToReturn.Id }, courseToReturn)); }
public async Task <ActionResult <StudentCourseResponse> > CreateStudentCourse([FromBody] StudentCourseCreateRequest studentCourseToCreate) { // find the course w/ courseId var course = await _context.Courses.FindAsync(studentCourseToCreate.CourseId); if (course == null) { return(NotFound()); } // find the student w/ studentId var student = await _context.Students.FindAsync(studentCourseToCreate.StudentId); if (student == null) { return(NotFound()); } // find join code var joinCode = await _context.JoinCodes.FirstOrDefaultAsync(jc => jc.Code == studentCourseToCreate.Code); if (joinCode == null) { return(NotFound()); } // TODO: check if join code expired // ... // valid join code // create StudentCourse // if we map it to a student course entity, and it successfully saved to db and gets // student and course info, no need to find course and student. just check if it exist var studentCourse = _mapper.Map <StudentCourse> (studentCourseToCreate); // save the studentCourse _context.StudentCourses.Add(studentCourse); await _context.SaveChangesAsync(); // return response var studentCourseToReturn = _mapper.Map <StudentCourseResponse> (studentCourse); return(CreatedAtAction(nameof(GetStudentCourse), new { studentId = studentCourse.StudentId, courseId = studentCourse.CourseId }, studentCourseToReturn )); }
public async Task <ActionResult <ProblemResponse> > CreateProblem([FromBody] ProblemCreateRequest problemToCreate) { // verify author exists var author = await _context.Accounts.FindAsync(problemToCreate.AuthorId); if (author == null) { return(NotFound()); } // verify CourseIds exist var courses = await _context.Courses.Where(c => problemToCreate.CourseIds.Contains(c.Id)).ToListAsync(); if (courses == null) { return(NotFound()); } // map request to entity var problem = _mapper.Map <Problem> (problemToCreate); problem.Author = author; problem.CourseProblems = courses.Select(course => new CourseProblem() { Course = course, Problem = problem } ).ToList(); // save problem _context.Problems.Add(problem); await _context.SaveChangesAsync(); // map entity to response var problemResponse = _mapper.Map <ProblemResponse> (problem); return(CreatedAtAction(nameof(GetProblem), new { problemId = problemResponse.Id }, problemResponse)); }
public async Task <ActionResult <TopicResponse> > CreateTopic([FromRoute] Guid courseId, [FromBody] TopicCreateRequest topicToCreate) { // find course this topic is assigned var course = await _context.Courses.FindAsync(courseId); if (course == null) { return(NotFound()); } // verify ProblemIds exists var problems = await _context.Problems.Where(p => topicToCreate.ProblemIds.Contains(p.Id)).ToListAsync(); if (problems == null) { return(NotFound()); } // map request to entity var topic = _mapper.Map <Topic> (topicToCreate); topic.Course = course; topic.TopicProblems = problems.Select(problem => new TopicProblem() { Topic = topic, Problem = problem } ).ToList(); // save topic _context.Topics.Add(topic); await _context.SaveChangesAsync(); // map entity to response var topicResponse = _mapper.Map <TopicResponse> (topic); return(CreatedAtAction(nameof(GetTopic), new { courseId = courseId, topicId = topicResponse.Id }, topicResponse)); }
public async Task <ActionResult <JoinCodeResponse> > GenerateNewCode([FromRoute] Guid courseId) { var course = await _context.Courses.Include(c => c.JoinCode).FirstOrDefaultAsync(c => c.Id == courseId); if (course == null) { return(NotFound()); } var newCode = JoinCodeGenerator.Generate(6); // TODO: Handle duplicate join code if (course.JoinCode == null) { course.JoinCode = new JoinCode(); } course.JoinCode.Code = newCode; await _context.SaveChangesAsync(); return(Ok(_mapper.Map <JoinCodeResponse> (course.JoinCode))); }
public async Task <ActionResult <AccountResponse> > CreateAccount([FromBody] AccountCreateRequest accountToCreate) { // map request to entity var account = _mapper.Map <Account> (accountToCreate); // Role must be student/instructor only if (account.AccountRole == Role.Student) { account.Student = new Student() { Account = account } } ; else if (account.AccountRole == Role.Instructor) { account.Instructor = new Instructor() { Account = account } } ; else { return(BadRequest()); } // save _context.Accounts.Add(account); await _context.SaveChangesAsync(); // return response at location var accountResponse = _mapper.Map <AccountResponse> (account); return(CreatedAtAction(nameof(GetAccount), new { accountId = accountResponse.Id }, accountResponse)); }