Lectures Backend

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

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To 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.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:08] Now let's start working on structuring the cause. A cause usually has some sections and every section has some associated lectures.

    [00:09 - 00:18] So we will design the cause in the same way. Let's close everything and let's go to entity.

    [00:19 - 00:32] Let's close API and client and inside entity, let's create a new class for section. So let's write section.

    [00:33 - 00:45] A section will have ID, which will be an integer. So let's write int and let's call it ID.

    [00:46 - 00:59] It will have a name of type string. And also it will have some lectures so we can create a list of lectures.

    [01:00 - 01:14] We can write i collection of lecture which we haven't created yet. And this will be called lectures.

    [01:15 - 01:30] And as navigation properties, we have to mention the cause. So let's start with the cause ID, which is of type, GWID, and let's call it, cause ID, let's import GWID using system.

    [01:31 - 01:39] Also we'll have to mention course of type course. This is basically how the convention is to make one to many relationship.

    [01:40 - 01:50] Let's import i collection. And here we will create lecture class inside a new file.

    [01:51 - 02:00] Our lecture class will have ID of type, int again. So let's write ID.

    [02:01 - 02:10] It will have a title of type string. And since a lecture will be a video, we can add a URL property as well.

    [02:11 - 02:23] And the URL is of type string as well. And now let's add the navigation properties like section is associated to a course.

    [02:24 - 02:27] The lecture is associated with the section. So we will do the same thing here.

    [02:28 - 02:44] We will start with section ID, which is integer. And we will mention section with name section and type section.

    [02:45 - 03:02] Now as both of them are finished, we need to pass these sections inside our course model. So let's open course and below we will create i collection of sections.

    [03:03 - 03:15] And let's call it sections. Now we can add the section table and lecture table inside store context file.

    [03:16 - 03:23] And we need to create two more DB sets. Let's start with the sections table.

    [03:24 - 03:38] So it will be DB set of type section. And it will be called sections.

    [03:39 - 03:45] And we will pass get and set. Also we need another table for lecture.

    [03:46 - 04:01] So we can copy it and replace section with lecture. Once we have added new tables, we will have to create a new migration.

    [04:02 - 04:15] So let's open our terminal and stop the server. Let's go to our base project and write dot net EF migrations add.

    [04:16 - 04:41] And let's call it lectures table added lectures table. And our starter project is API and our project is infrastructure.

    [04:42 - 04:48] As you can see, our migration is successfully completed. Now that we have tables set up, we can add some data to it.

    [04:49 - 05:00] I have added two files inside the transcript with names sections.json and lectures.json. Please copy them and paste them inside the seed data folder.

    [05:01 - 05:07] Let's start with the sections.json, copy it and inside seed data. Let's create a new file.

    [05:08 - 05:23] Let's call it sections.json and paste it here. In the same way, just copy lectures.json file and paste it inside seed data.

    [05:24 - 05:45] Create a new file lectures.json and paste lectures.json file here. So paste the code inside store context dot seed file and paste it inside store context seed dot CS.

    [05:46 - 05:55] So let's open store context seed dot CS. That's minimize the terminal a bit.

    [05:56 - 06:04] And when these things are ending, we can paste the code inside store context. json.

    [06:05 - 06:12] I am making you guys copy the code because this is the same thing happening. And I don't want you guys to waste more time on writing the same code.

    [06:13 - 06:22] I'm pretty sure you will understand what this code is doing. We are just reading the data from the files and adding it inside the sections and the user table.

    [06:23 - 06:30] We can now run the server and see if everything is working as expected. So let's make it a little bigger.

    [06:31 - 06:52] And let's go back to the API file if we run.net, watch, run. And now if we open database again and check if we have the sections table and the lectures table, let's see the data and we have some data inside the sections table.

    [06:53 - 07:01] Let's also check it inside the lectures table. And we do have data inside lecture table as well.

    [07:02 - 07:04] Let's work on the lectures controller now in the next lesson.