Build a Fullstack App with Vanilla JS and Go

Adding More Handlers

Maximiliano Firtman
Independent Consultant
Build a Fullstack App with Vanilla JS and Go

Lesson Description

The "Adding More Handlers" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano finishes the handlers, which include functions such as getting movies by ID, getting all genres, and searching for movies. Once the handlers are completed, the URLs need to be registered so that the server can respond to the corresponding requests.

Preview
Close

Transcript from the "Adding More Handlers" Lesson

[00:00:00]
>> Maximiliano Firtman: We have already added all the functions to our data repository. We just need to finish the handlers because the handler, remember the handler is the one that speaks HGTP only. It's only has get the movies and get random movies, so we need the rest. Stop the services as well get movie by ID, all the genre.

[00:00:24]
So in this case, we can go to the readme and just we have here the final movies handler.go. So I can copy and paste, or I can just take the new ones. Something that I have here is that if I copy everything like from here, I have better error management so on, like this.

[00:00:52]
Also, I have a new movie handler that's, remember the in case you want it, let's take it as well. Code that will take the storage,bbb great one. I don't need to copy the imports, typically VS code will actually update that for you, and probably I have to update some things.

[00:01:18]
So this is the handlers. Yes, I'm going to paste this here and now it's DME error, because this code was expecting lowercase s and L. Okay, remember that I use because I was showing you different ways to do that. We can go back to lowercase or update every reference.

[00:01:41]
Remember, if you do this now they are private. So from the outside, main is giving me an error now. So now main is in red, so main is actually giving me an error. Why? Because now when we were trying to initialize the storage, now it's private. So there is no way to do this.

[00:02:00]
So now instead of creating the instance like this, if you want to follow this pattern, if not, you can keep it like this. We can now call new movie handler, and new movie handler will receive the both options, the log instance and the movie repo as arguments. It's just the same, okay, in this case, going through a factory.

[00:02:20]
Three, does it make sense? So now that we do have the handlers, we have the handler for GetGenres, for GetMovie. We have SearchMovies. In this case, you can see this is receiving arguments through the get, so through the URL. So it's getting the query, the order, the genre, and it's passing that to the data repository.

[00:02:48]
Remember handlers speaks HTTP. So that's why it's speaking URL. Speaking JSON is reading information from the request. Sending information back to the writer to. Response writer, and on the other side the repository is actually speaking models and SQL. That's kind of the idea and after we have the handles, we need to register the URLs.

[00:03:16]
So that's actually the last step for our server as aat least for the data part, because we still have the account type, the login authentication for later. But for the movies part, now we need to add the the rest of the function, so handle func, so we will have API.

[00:03:41]
Movies search, remember, keep the trailing slash or not up to you, but stay consistent. Movie handler, and we will try to here call search movies, then we have one One that takes one particular movie. So handle funk. Look at this, I will just say movies/. I will explain what that is in a minute and this is get movie, one movie, and then handle funk.

[00:04:22]
Genre movie handler get genres. So what's the idea with this? Because it says movies. The idea is that this one is going to be API movies. And the idea of the movie, like for example, 140. I don't need, just say anything special here, in this case, this handler will take everything, starting with API movies.

[00:04:52]
What about search random on top, because they were defined before, they will handle the HTTP request before this one. So that means the order is important. If I do this, top random and search will never work. You understand why? The order is important. I want to pass the body, right in JSON, You want to pass what?

[00:05:20]
Sorry, The body, like JSON body. So instead of receiving, we will do that for authentication and login. We will pass data over the body in JSON. So we will do that. But the answer is that the quick answer is that it's not going to change this part. It's going to change the handler.

[00:05:43]
So inside the handler, you will use their request to read the body that is coming from the client, okay? So you change actually the handler, not actually how you register the handler. There is no way to create a handle that is doing Different there is a way to register to the HTTP system a handler, but only if there is a body.

[00:06:11]
You cannot do that. In that case, you just create a handler, and inside, you check if you do have the body or not, and based on that, you call other functions. So that means that now, if we try this. We have the top movies, random movies. We can request one movie, for example, this one, 542, and it's giving me that movie.

[00:06:40]
And also, we can request things like genres. Genres will give us the array, okay? And search. Search is more complicated because there are more things here, but we can try to search and search by, for example, Mad Max. Now it's giving me only Mad Max Max Movies, mailbox four, URL Max Mark beyond thunder dome and so on.

[00:07:10]
And the original one, okay? Does it make sense? So we have the API up and running, API that is currently connected to our SQL database. This is just stage stage one for our backend. The data is there. We can consume the data. The next stage will be authentication, but that's for later.

[00:07:31]
But before doing that, I wanna go to the client so we can actually see this in action. Because right now everything is so abstract, it's time to see something on the screen.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now