Creating HttpGet Actions for Gigs

Pavol Almasi
A free video tutorial from Pavol Almasi
Computer Programmer at Berkshire Hathaway Company
4.5 instructor rating • 16 courses • 35,341 students

Learn more from the full course

Async API with Asp.Net Core & EF Core (step by step project)

Build complete async API from start to finish with .Net Core Services, AutoMapping, Interfaces, DTO objects and more

03:43:05 of on-demand video • Updated October 2020

  • How to build API from start to finish
  • Explore ASP .Net Core MVC
  • Apply REST to .Net Core API
  • Implement Data Transfer Objects
  • Configure .Net Core Services
  • Map Entitities with AutoMapper
  • Implement Relationships between classes
  • Work with Keys and Foreign Keys
English [Auto] All right so we have three get requests that we need to process in our action. So let's start with the get gigs by event. Now this is not getting all the gigs. Just like with God all the events are comedians. This is getting gig specific for this event. So this is actually more of a search. We will use a search term for example search by event and then pass in the event I.D. as a parameter in the you are I. So this will kind of resemble the get events by date or the get comedians by event. So let's create the gigs CONTROLLER AND WE'LL CALL IT geeks controller and I go to comedians controller and I will actually just copy paste a few things. We need the crowd and we need to designate it as an API controller. So I'll copy paste that in my geeks controller and bring in the nice spaces and again I'll go to comedian's controller and get these three private read only variables because we need the even repository the mapper and the link generator. So I'll paste it right here and then I'll create the constructor and I'm going to pass everything into the constructor. So again I'll just copy this from the comedian's controller and pasted in our gates controller here. Now let's bring some nice bases as you can see we need them. We need the repository. We need the mapper as well as the link generator. And then I go once more time into the comedian's controller and copy inside the constructor. The assignment of these values to our variables. So now our controller is already. So let's start creating the get gigs by event. And I'll go to committee as controller again and I'm going to copy this search here for the get comedians by event because we are getting gigs by event so we can tell that it's fairly similar but instead of just saying search I'm going to say search by event. Because if we go to our repository we will also be searching gigs by venue so our string will be searched by event. So it will be API slash gigs slash search by event and then we will pass the event idea to it. So the action result will of course be the collection of gigs Dito. So it's bringing the namespace for it and we'll pass the event I.D. That's what we want because we are searching by event but we are searching comedians. We are searching gigs. So our renamed this action to get gigs by event and now let's get the geeks into the results variable so we'll go to our repository and then method is get gigs by event and in the event I.D. and you can see that we still have a bunch of underlined words. And it's because what I forgot to do actually is to change this to a controller. This is still just a class that needs to inherit from the controller base. That's what happens when you copy paste a lot and then you forget a few things. Now if it should be OK and it is. So again we got the results from the get gigs by event if nothing was found. Then we return not found. Otherwise we will map the detail object for the geeks gig video into the result which is the entity of gigs we will return. OK. Otherwise we will return a 500 status code. So let's bring bringing the namespace for this one as well. And that is our get gigs by event. So next let's do the get gigs by venue. So I will simply copied all this because this is gonna be very similar. And this is gonna be a search by venue. Of course it's actually to be get and I will name this action. Get gigs by venue but I have it as get gig. But we are actually getting gigs. We are getting a collection of all gigs that are happening in this venue. So our Amy to gigs my venue and rename these two gigs by event. So this time we cannot pass an event. ITV are passing venue idea so I'll change that and we'll go to our repository to get that. So the method we need is to get gigs by venue passing the venue idea and everything else stays the same. We have the result not found. If this is not only tell anything. Otherwise we will map the gig detail into the results and return it or we will return 500 state of gold. If something goes wrong. So these are two such actions. But when we go to a repository you can see that we also should include the comedians when we're done. They will be false all the time but we need to let the user choose whether to include comedians or not. So I'll copy that and they go to gigs controller. We will pass that as a second parameter or second argument so it will include the comedians. True or false in both for the venue as well as for the event. So here when we are getting the gigs for the event we also need to pass in the second parameter and that is the better to include the comedians or not and the same when we search by venue we pass in the venue I.T. and then whether to include the comedians for the venue or not. So you can see now we have the get gigs by event complete as well as the gate gigs by venue. So last let's do the get gig which is simply getting a single gig based on the idea of the gig and for that I'll go to the comedians controller and I'll go to get comedian by ideas over here. Copy that and paste it in my gigs controller here. So we are passing in not a comedian idea but the gig idea and we can have it as get and passing the gig idea as an argument. And of course the action results return the gig Dito. So we get our results by go into the method get gig and pass the gig I.D. if not found we return not found. Otherwise we will map the ditto object for the gig to the result and return that if something goes wrong. We have an exception database failure and just like we had it for the get gigs by venue and by event we need to pass in also the bullion whether to include the comedians for this gig. So that's gonna be our second argument in this action. And of course we need to pass that inside the get gig method as well. So if it's true it will also return all the gigs. So these are free get actions for the gigs and next let's test them and see if everything works as expected.