Hi guys, in the last video, we completed all of our endpoints of our to do list. In this video, I'm going to be showing you the code all over again. And then we are going to be going step by step as to what we did where and how did we do it. So our entry point is main.go. And main.go, creates a new mocks, and then connects to the database. And then says that okay, whenever we are tearing down our server, then close the database.
And then we simply listen and so on port 3000, and passing the mocks to it. Now the mocks that we are getting we are getting from the controller layer. And if you took that route, and look@route.go, there's a function called register, which returns us our marks. So when it returns us the marks it attaches all of the endpoints that we want. So there was one ping endpoint. If you look over here, there was one ping endpoint and all of the other endpoints were based off of the slash.
So we only need one endpoint for this. And then all of the logic or the all of the Create reading, read logic went inside this endpoint. So then we created our first endpoint called Ping. So we just created a function, which returns a HTTP dot handler, func. And then we defined that if we get a method get, then we can return our HTTP status. Okay?
Good with the body that says Pong. So if you look@route.gov he simply included the function over here because handle func takes in the route and the handler func. Then we moved on to our handle funk with create. So if you look at this function should rather be called crud. And if you go to View router go, we simply change it to karate, which means create, read, update, and delete. So going at it from the logical standpoint, if we had a method post, we extract the body, we deserialize it into our object, the JSON, and then we create a to do and after all that has been done, we send HTTP status create In response, in the case where there is no error, and then we encode, what we do while encoding is we grabbed the JSON encoder along our HTTP response writer, and we encode our data.
And then if we get a method get, we get the name by which we are trying to search. And then we return all of the two dues of the particular name. And then if we do a method, delete, with slash name, then we can simply extract the name out of it, and then we can delete things. So if you go to model dot model, and then you go to connect, this is our SQL connection, we had a global variable, only called connection over here, because we wanted the database connection and the database access only to the model. So only the model has access to the database. Then, what we do is we simply connect and then we return the database because if you look at me, nor do we need the database objects in order to close it after the server has done.
So going again, to connect create ago, we created a function create to do where we are, what we are doing is we are inserting values into the database. And these question marks are so that we can avoid SQL injection. And then we have delete, or delete to do. And then we have we got read functions. In the read all functions, we simply open some rows and then loop through those rows and DC lies into the data into our data element, append that data element into into our array and then return that I similar things have happened. But with read by name, the only difference is that we're reading or we've added an additional query, which says name is equal to question mark.
So, and if you go to our views, this is something that we return. So this was the structure of our API. And I hope you guys enjoyed this To Do List exercise. See you guys in the next video.