Solution - Implement Tags API Endpoint


Solution: Tags

How did it go? Did you give it try? If you accidently came to this video before trying here's an opportunity to press pause and try to come up with your own solution. I'll still be here when you get back.


Let's go into the Conduit.Repositories Project and add a new file called ITagsRepository.cs - we'll need to modify it for it to be an Interface Class. It will contain a single method signature that returns an array of strings and it's called GetTags.


Next we'll add another class called TagsRepository. It will be a public class that Implements the ITagsRepository Interface. Let's go ahead and implement that interface.

We'll need to add a couple of private members for Dependency Injection:

  • ConduitContext
  • ILogger
    • Let's bring the proper namespace for ILogger

Let's create a constructor and add the parameters that will be injected by the Dependency Injection Service so we can map them to the private members:

  • ConduitContext
  • ILogger

The method we need to implement goes like so…

We need an array of string that will come from a Linq Expression. Remember that is kind of, sort of looks like SQL but not really. However, the Linq Expression will return an IEnumerable object. So if we just wrap it in parenthesis we can call the To Array method. Then we just simply return the Array of Tags.


We'll need to add ITagsRepository as a private member and as a parameter in the constructor method for the Dependency Injection Service to work properly.

Next we'll implement some basic exception handling that will log the error and return a 500 Error Result in the case something goes horribly wrong.

Then we get into the meat & potatoes of the method. We'll create the array of strings that will be returned from the Repository method names GetTags. We'll return an OK response along with the Tags Object.

As part of the final icing on the cake, put the HttpGet Attribute on the method.

Dependency Injection Configuration The final piece before we put it to the test is the Dependency Injection Configuration. We'll need to modify the Startup file, go into the section where we configure the types and add both the interface and the implementation class for Tags Repository.


Let's run it and watch it work. We'll scroll down to the Tags Endpoint and try it out. There you are… The 200 Success Response along with the tags.

We can call this project Done, Done and Done! Or as they say in the Animated Movie, "The Cruds" Done! Done Doooone!

In the next and final video I'll wrap it up with a summary and some visual takes away's. I'll see you there.

Tag Name: final-working-api


I finished! On to the next chapter