|
15 | 15 | */ |
16 | 16 | // [START classroom_create_course] |
17 | 17 | /** |
18 | | - * Creates 10th Grade Biology Course. |
| 18 | + * Creates a 10th Grade Biology course. |
| 19 | + * @return {string} The ID of the created course. |
19 | 20 | * @see https://developers.google.com/classroom/reference/rest/v1/courses/create |
20 | | - * return {string} Id of created course |
21 | 21 | */ |
22 | 22 | function createCourse() { |
23 | | - let course = { |
| 23 | + if (!Classroom || !Classroom.Courses) { |
| 24 | + throw new Error('Enable the Classroom API advanced service.'); |
| 25 | + } |
| 26 | + const course = { |
24 | 27 | name: '10th Grade Biology', |
25 | 28 | section: 'Period 2', |
26 | 29 | descriptionHeading: 'Welcome to 10th Grade Biology', |
27 | | - description: 'We\'ll be learning about the structure of living creatures from a combination ' + |
28 | | - 'of textbooks, guest lectures, and lab work. Expect to be excited!', |
| 30 | + description: |
| 31 | + 'We\'ll be learning about the structure of living creatures from a ' + |
| 32 | + 'combination of textbooks, guest lectures, and lab work. Expect to be ' + |
| 33 | + 'excited!', |
29 | 34 | room: '301', |
30 | 35 | ownerId: 'me', |
31 | | - courseState: 'PROVISIONED' |
| 36 | + courseState: 'PROVISIONED', |
32 | 37 | }; |
33 | | - try { |
34 | | - // Create the course using course details. |
35 | | - course = Classroom.Courses.create(course); |
36 | | - console.log('Course created: %s (%s)', course.name, course.id); |
37 | | - return course.id; |
38 | | - } catch (err) { |
39 | | - // TODO (developer) - Handle Courses.create() exception |
40 | | - console.log('Failed to create course %s with an error %s', course.name, err.message); |
| 38 | + const newCourse = Classroom.Courses.create(course); |
| 39 | + console.log('Course created: %s (%s)', newCourse.name, newCourse.id); |
| 40 | + if (!newCourse.id) { |
| 41 | + throw new Error('Course created but no ID was returned.'); |
41 | 42 | } |
| 43 | + return newCourse.id; |
42 | 44 | } |
43 | 45 | // [END classroom_create_course] |
0 commit comments