This video is available to students only

Lectures Controller

In this lesson, we're going to create lectures controller

Lectures controller#

Now that we have created sections table and lectures table with the data, it's time to work on the controller which will send us the course's data. Before we start working on the controller, there is one more thing I would like to add to the UserCourse table which is the currentLecture property. Usually, if you take a course, you will notice that it will resume the course where you last left it, so let's go to UserCourse model and add CurrentLecture property which will be type int. Here, we will store the lecture Id which is an integer.


Since we have added a new property, we will need to make a new migration. Let's open the terminal and run:

Let's restart the server and you will notice that by default, it will have a value of zero. We can now start working on the LecturesController. Let's start by deriving from the BaseController. Now we can create a constructor and here, we will need StoreContext with name, context; IMapper with name, mapper; and UserManager with name, userManager. Let's import them one by one. Now we can initialize field from parameter.

Let's start with a get request which will send all the sections and lectures back to the client. In this request, we will need courseId from the client so we can mention that inside curly brackets. Also, this request should be Authorized so let's add that as well. From this function, we want to return the courseName, list of sections and CurrentLecture. Let's create a new DTO with name, UserLectureDto. We will return CourseName of type string; currentlecture will be type int and list of sections. We can't return list of sections as it is because it will cause circular error, so let's create a SectionDto which will have SectionName of type string and list of lectures. The same way, we will create LectureDto which will have id of type int, title of type string and Url of type string. Now that we have created the Dtos, let's map them inside mapping profiles.


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