Outline

Demo: Create Article

We're finally at the point we've all been waiting for. We get to create Articles.

Let's go into ArticlesController and expand CreateArticleAsync, Right-Click on the CreateArticleAsync and select Go To Iimplementation.

Create Article

First thing's first, we need to know who is logged in and creating an Article. We'll do that by calling the Account Repository's method GetLoggedInUserAsync.

Next, we create an Editorial since it's the structure that matches our table in the database. We can do that by having the Mapper Map the Article to an Editorial and return it. This makes life pretty simple for us and saves us on a lot of coding.

We also need to assign the Id of the Logged In Person to the Editorial PersonId Property. This represents the Author of the Article.

Creating the Slug

Here's where things get interesting. We need to create a Slug for the Editorial and it need to be unique. Let's go ahead and generate the method and see how that's going to work.

The CreateSlug method is going to make use of the .Net's Random Class. We'll create a string constant made up of the alphabet. With this information we can create a new string using the Repeat method from the .Net's Enumerable class. We can Select 5 characters and with a little bit of lambda magic and the Random Class… viola! We create a randomly unique string.

Using string interpolation we'll replace the spaces in the title with dashes and append the 5 random alpha-characters we generated. That creates the Slug!

Create Article Continue…

Back to the CreateArticle method, using the Context we'll add the newly created Editorial and save the changes to the database.

Saving Tags

Once again, things get interesting when saving Tags for an Article. The method we'll use for saving Tags will take 2 parameters; The newly created Editorial from the Database since we need to the Primary Key to link the Tags to it and the Tag list from the ArticleCreateRequest.

We'll iterate thru each tag and perform a check to see if it already exist in the Tags Table. If it doesn't we'll create a new entry in the Context for Tags, add it as a link to the Editorials and Save all the Changes. Otherwise, we just add it as a link to the Editorials and Save the changes.

Create Article Continue…

Finally we'll grab the Article by reusing our method GetArticleBySlugAsync and return it.

Run the Application

Let's run it and see how it works! So exciting!!!

First thing we need to do is login. Let's grab the JSON Web Token and use it to Authorize. Let's expand the Post method, click on Try It Out and fill it in.

Troubleshooting

Unfortunately, we get a 500 Error.

1. Can you look at this and tell me what's wrong?
2. Maybe pause the video and inspect the code to see what we left out.

If the error doesn't pop out at you immediately we'll need to resort to some debugging. In the next video we'll see how we can step thru the code and determine what's wrong.

 

I finished! On to the next chapter