Reading from the DB

GetGoing: Introduction to Golang Introduction to API development with Go
6 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 everyone, in this video, we're going to be starting our work on the read all entries endpoint. So let's go straight away to model. And I want to create a new file called reads.go. So it is going to be the same. Right? Okay, I created this window controller is going to be in the same package is going to be ready to go.

And it's gonna come from the package model. So let me quickly close all of these. So, right now what we want to do is we want to create a function, which reads everything from the database, appends it in an array, and then returns it to the user. So what we're going to do is we're going to create a function called read all which is going to return an array of two dues. And in our case, if we look at views, so our post request, has the same schema have to do As we have in our database, so we're going to be reusing the struct. So we're going to say, use a slice of use door, post request to return.

Now let's define this function we're gonna see a rose error is equal to, and we're also also going to be returning an error object along with that. So let's return this. Closer is equal to con dot query. We're gonna do a select star from so in case we get an error, then what we want to do is we want to return knit along with the error, right? So let's create our array. So our array is going to be called to lose is equal to is going to be an area of use post request.

So this is how you define an array. So I want to loop between these rows, I want to say rows dot next. And then what I want to do is at each loop, I want to decode them into my structure. So I'm going to do a Atlas of data and do some data.to. Do. So what our data object is going to be, there's going to be an instant instance of the post request.

So use post request. There we go. So, function is scan. Those are scan and then after I scan it, I'm simply going to print it and to lose it. to append, to dues, and data. Now after we append everything back up in the no return everything back to dues And so, again, quick recap into this function, we basically queried upon the database to return everything.

Then we create an array of the POST requests, we used our post request struct. Then we loop through the rows. And in each row, we created an instance of the post request struct. And in that we, like we fed it the whole values of the name and the to do and we appended in an array, and then we return the array along with on over to our controller layer. I'm going to rename the Create or go as a current dog go or create read, update delete.go. And now, it said that if you're getting a method post, then create an entry.

And if you're getting a method else, get HTTP Gordon method And then we'll read all entries. So we can say data error is equal to model dot read all. And then after this we can say if error is not equal to null, then simply write the error, right the statement or the error by invoking the arrow dot era with the capital M into the string string. And then if the error is nil, in fact, then we can simply do JSON dot new encoder OR operator on the actor then encode our before doing this, you can also do a right arrow right here, HTTP dot status, okay. So, it doesn't refer to here and my spelling mistakes method Now let's run. So let's go over to this.

Let's record on Windows. And let's perform good on localhost 3000. And we got ourselves our data back from the database. So you'll notice that if you're new, if you're doing a post call, on the same route we were getting, we were able to create new entries into the database. But when we are doing get calls, now, then we can read all the database. This is because we've defined which method to use right over here.

So let's look at our requirements specification. So if you look at this read all entries was on the same route. And our status was 200. And the response code was the JSON of old increase. So we were able to do this. In fact, we were able to fetch everything from our database and return it in JSON format.

And if you take a look at the status score, by invoking the dash I call you can see, you can see that it's 200 Okay, So in this video we did read all entries in the next video, we're going to be learning about how to read an entries by ID that is how to query into the database. So, see you guys in the next video and stay tuned.

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.