Deleting 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 the last video, we saw how to read a particular value by querying the database. In this video, we're going to be talking about how we can delete an item using the same principles. So it is going to be a delete request. So we can say elsif or dot method that is equal to http dot method, delete. Then let's do something. So first let's write the function on the model.

So you're created go in the model, then copy this function, create to do and then delete to do so we want to delete our to do according to name. So we want to delete all or other tools which have a certain name. so we can simply do DELETE FROM to do their name is equal to question and then we can simply pass in the name over here and then we are Good to go. So in this way, we can delete this thing from our database. So let's go to our controller layer, go to crud. And then what we need to do is we need to, we need to do data is equal to, or other error is equal to model dot Delete to do.

And we want to pass in the name of edit is not equal to nil. Then we can simply write W dot write some bytes, the chances some error. And then we can simply return and if we delete or something, then we can simply say JSON dot new decoder encoder dot encode. Or let's see what we want to encode. You want to encode our status, so we can simply say struct And our struct is going to be status. And let let our status be a string in this case.

And then JSON status, the STB capital of the status over here, because it is exported. And then we're going to be, say, item deleted. So this is gonna wrap, wrap our encoder around our response writer, and then encode the struct, an instance of the struct that says item delete. And then at same time, we can say W dot right header, HTTP dot status, okay, which sends the status 200. Now, the only thing is how do we define our name over here. So you can simply say rat name, string.

So now, in this case, what we're going to do is we're going to do some path manipulation. So if you look over here, you see that we need to include the name or the IDS The slash road. So let's see how we can do that. So I'm gonna restart my server real quick and I'm going over to postman to create a new request this request is going to be a delete request in our case and then we want to pass in the name coats fit but we don't want to pass it like this. We want to do it right in the URL. So localhost 3000 coach fit send a delete request to it is going to delete all of the to do lists which belong to coach fit.

So what happens when you do it now? status item deleted. But what actually happened? So if you look at couchsurfing, it nothing actually happened. This is because we pass in nil as a name over here. And then it said the database said that okay, delete items where name is nil.

So database says that okay, name isn't nil anywhere. So I'm just gonna return that I've done. I've done my job. And that is why our error wasn't invoked. So how do we capture the this name So let's do a form dot print line on something known as r dot u dot plot. So r dot URL dot path returns a string back to us, which is the part of the URL of the API that visiting.

So send a send request. So we got the slash culture for it. Now, if you look at this route, we only need this part, right, this part is going to be our name. So we can use the concept of string slicing over here, where we can say that okay, I want from one till the end, and I don't want anything else after that. So let me start this real quick. I'm going to assume same code.

And then we got our name over here. So going back to our code, we can simply say, our name is going to be our load URL dot path. And then whatever you open it after this, and we don't need this anymore. So let's see what happens when we hit the endpoint. Right now. I'm just gonna restart the server real quick.

I'm going to go to postman. Let's first confirm if coachability still exists, it does. Now if you send a request, it says item deleted. If you send a request right now, and say that there's nothing in the database right now, related to coach affinity, so our thing worked. So if I type in my name right now, and send in send in a request, you see that there are three to do lists or other three to two items created in the database. So let's see what happens when I pass in.

My name is ref coach fit on the screen. It says item deleted returns a 200. Ok response. When I look at the database, all of the items are deleted. So in this videos and past couple of videos, we learned how to create a simple to do API, where we check the health of the API created an entry read all entries, read all entries by it and deleted the entries. In the last video, we're going to conclude our two To API, and we're going to talk about each of the steps that we took while building it.

So stay tuned guys and 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.