Create Sections Backend
In this lesson, we're going to work on sections backend
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:10] We can now create a basic cause. It's time to add sections and lectures so that we can finalize our course. Let's start with the lectures controller. So let's go to lectures controller.
[00:11 - 00:48] So let's see where it ends and here we can create a method which will be HTTP post. So here let's write HTTP post and This will be authorized as well. So let's make it authorized. And we want role to be of an instructor to be able to make this call. So what we can do is we can write roles which will be equal to instructor.
[00:49 - 01:02] From this method we will return a string. So let's start writing this method public async task. Type action result type string.
[01:03 - 01:49] And we can call it add section. As parameter we will receive course ID section name and list of lectures for that we can create a new DTO and We can call it add section DTO. So inside DTO. Let's create a new class. Let's call it add section DTO. Now this DTO will have course ID of type Gwed. So we can write G wed And this will be called course ID Let's import Gwed using system.
[01:50 - 02:26] This will have section name of type string. So here we can make it string and call it section name and Finally it will have list of lecture DTO. So here we can write list of lecture DTO and this will be called lectures. And now let's import list using system collections generic.
[02:27 - 09:15] Let's pass this as a parameter now. So here we can write add section DTO and let's simply call it section DTO. First of all, let's create a course variable. So let's write course which will be equal to context dot courses dot find async and Inside we can pass section DTO dot course ID and also we need to await Now we can create a section variable So here we can create a section Which will be equal to a new section. So here we want to create a new section Inside we can pass name Which will be equal to section DTO dot section name and We will pass course which is equal to the course We can now add the section to context sections. So here, let's write context dot sections dot add and we can simply add the section Now that we have added the section we can add lectures related to this So we will run a for each loop on section DTO lectures. So let me write for each and Inside we can write our item in section DTO dot lectures Because we are receiving a list of lectures. So we can run a loop Now inside every loop we can create a new lecture. So here we can write Wow lecture is equal to new lecture And here we will pass title which will be item dot title We will pass the URL which will be equal to item dot URL and we also need to pass the section. So section will simply be this section After this we can add this lecture into the lecture stable. So here we can write context dot lectures dot add and we will pass lecture So the number of lectures it will have it will keep on looping over it and add those lectures individually into our lecture stable with the particular section and Now we can write our response. So here, let's write bar response This will be equal to await context dot Save changes async and again we can check if it is more than zero Which means if the response is successful and Since we are returning a string. So if there is a response we can return a string which will say section added successfully Otherwise we can return a bad request. So here, let's write bad request and inside we can pass new API response and here the status code will be 400 and the message will say problem creating section Since we are working on the back end we have another API endpoint to work on This endpoint will have responsibility to publish the cause. So let's go to our courses controller And here they will create a new post request So let's write it here HTTP post This one will have an endpoint which will be published slash The cause ID which will be dynamic so we can use curly brackets for that and This method will be secured as well. So we can make it authorized. So let's write Authorize and we want rule To be of an instructor to make this call Now we are going to return a string from this method as well. So we can write public async task of type action result and inside we can pass the string This one will be called publish course And as a parameter we will pass the course ID which is here. So this has a type good We can write it and this is course ID And This method is going to be very straightforward. So let's create a course variable Which will be equal to await context dot courses dot find async and Inside we will pass the course ID and now we will simply check if course is null We can return not found And we can pass new API response With the status 404 And of course is available we can simply make course dot published and make it true And rest of the logic is same as this so we can simply copy this part and Paste it here and Of course we need to change The message so we can say cause published successfully And here we can say problem publishing the course Well, this is it for the back end. Let's start working on the front end part in the next lesson