Create Sections Backend
In this lesson, we're going to work on sections backend
Create sections backend#
We can now create a basic course. It's time to add sections and lectures so that we can finalize the course. Let's start with LecturesController. We will create a method which will be Http Post. This will be Authorized and we want role to be of an instructor, to be able to make this call. We will return a string and we will call this AddSection. As parameter, we will receive courseId, sectionName and list of lectures. So let's create a new Dto called AddSectionDto. This will have CourseId of type Guid, section name of type string and list of lectureDto with name, Lectures.
Let's pass this as a parameter now; we can call this sectionDto. First of all, let's create course variable which will be equal to context courses find async. We can pass this course id inside which will be sectionDto.CourseId; now we can create a section variable which will be equal to new section, and inside, we can pass name which will be sectionDto.SectionName and course which will be equal to course. We can now add this section to context sections add. Now that we have added the section, we can add lectures related to this. We will run a for each loop on sectionDto lectures. Inside every loop, we can create a new lecture. So let's create a new lecture, and inside, we will pass title, which will be equal to item dot title; url which will be equal to item dot url; and section which will be equal to section. After this, we can add lecture to context lectures. We can now write our response which will be equal to context save changes async is more than zero. If response, we can return the string
Section Added Successfully. Otherwise, we will return badRequest, new ApiResponse; status code will be 400 and message will be
Problem creating Section.