This video is available to students only

Create Course Page

In this lesson, we're going to work on creating the course page

Create course page#

In the last lesson, we worked on the instructor page and we are giving an option to our instructors to create a new course. We haven't created an endpoint yet to create a course, so let's do that first inside CoursesController. We will authorize this function, and this time, we only want the instructor to create this course so we can write Roles to be equal to instructor. This will be a post request so let's write Http post. From this function, we will return a string so let's write public async Task ActionResult of type string. Let's call this method CreateCourse. We will pass data from body so let's mention FromBody and data will be of type course.

Inside this method, we will first set the instructor to be User.Identity.Name because this is an authorized request. After that, we can add the course to context dot course. Finally, we can write result which will be equal to context dot saveChangesAsync is greater than zero. If result is positive, we can return Course Created Successfully. Otherwise, we can return BadRequest and pass new ApiResponse with status 400 and message Problem creating Course.


Now let's register this endpoint inside courses that's inside the agent file. We will create a new method called create. This will be a post request so we will pass some data. For this, we will need to create a new registerCourse model, so let's do that inside course.ts file. We will pass title of type string, subTitle of type string, price of type number, description of type string, language of type string, categoryId of type number and level of type string. We are not passing instructor name because we're using the username for that. We are not passing any image because I have not added imageUpload service as a part of this course.


Let's go back to agent file now and give our data the RegisterCourse type. This is a post request and response will be a string so let's mention it. Endpoint is courses and body is data.

Start a new discussion. All notification go to the author.