Project Outro

GetGoing: Introduction to Golang Introduction to API development with Go
4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.