تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Delete Actions Overview0:00

Let's delete some stuff. Now, of course, by delete I mean deleting records because it doesn't matter if we are deleting posts, deleting replies, deleting reposts, unlike or unfollowing. We are deleting records in the database. But before we get into all of that, I want to talk about how we are going to present this functionality, because to me, that's, that's important. So let's just pick on like here.

Choosing Request Method0:28

because to me, that's, that's important. So let's just pick on like here. So in order to, like a post, we made a POST request for profile/status/post/like makes sense to me. So in order to, unlike something, we have a couple of options. The first thing would be what would technically be the correct thing to do,

The first thing would be what would technically be the correct thing to do, at least from a restful standpoint. And that would be to issue a DELETE request for the same URL. That's fine that that's the technically correct thing to do. We could also make the argument that we would issue a DELETE request for unlike, and maybe that's what we should do. Or maybe we just make a POST request for unlike.

and maybe that's what we should do. Or maybe we just make a POST request for unlike and then let that be. So this is the problem that we come to and I guess the problem isn't necessarily, uh, what is the correct thing to do because a lot of times the correct thing to do isn't really the thing that we should do to begin with because, you know, yes, from a purely restful standpoint, we should be using a DELETE request here.

because, you know, yes, from a purely restful standpoint, we should be using a delete request here. But there's nothing that said that we were gonna be completely restful. There was nothing that said that we have to be restful. I certainly didn't get any instruction to say be restful. In fact, Jeffrey hasn't even seen any of this code and that's gonna be fun. I'm, I'm, I'm interested to see if what he's gonna say. Like, what the hell boy. Anyway.

I'm, I'm, I'm interested to see if what he's gonna say. Like, what the hell boy. Anyway. Um, so you know, my first thought is, you know, we do a POST request for an unlike, because look at the final segment of these URLs, like follow, quote, repost, reply. These are all actions. They are commands. We're, we're issuing those commands, we're doing something. And in that sense, it makes perfect and total sense to have, unlike here.

And in that sense, it makes perfect and total sense to have, unlike here. So then the question becomes what do we do? Do we have a Post or do we have a delete? I guess if we want it to be purely technically correct, we use a delete. But you know, if you go to X, if you open up the developer tools, if you um, look at the network and then you start liking and unliking following.

look at the network and then you start liking and unliking following and unfollowing, you're gonna see that they are all POST requests. And I don't know why they chose to use POST requests, but there's no harm in using POST requests. There's, there's nothing that says that we have to absolutely follow restful ideas and POST works just fine. So we're gonna go with posts.

Adding Unlike and Unfollow3:18

and post works just fine. So we're gonna go with posts. Yeah, that's what we're gonna do. So when it comes to unlike we are going to issue a POST request to profile/status/post, unlike to unlike it, we will have an unlike method and the name of that route will be posts.unlike. We will do the same thing for follow. So let's just go ahead and copy that too, so that we are gonna make a POST request

So let's just go ahead and copy that too, so that we are gonna make a POST request to profile/unfollow. We'll have a method called unfollow and the name of the route will be profiles.unfollow. So let's do the ProfileController first because that's gonna be really the only thing that we need to do inside of the ProfileController. Let's just copy and paste the follow method. We'll change the name to unfollow and what do we do?

Let's just copy and paste the follow method. We'll change the name to unfollow and what do we do? All we really need to do, we created the follow before. We will remove the follow after and that returns a Boolean. So we'll have success and then we'll just return that success as far as the json structure. So there we go. Now, once again, you could make the argument that, well we need to check does the user, uh, follow

Now, once again, you could make the argument that, well we need to check does the User, uh, follow that profile? But that's a database hit and we are already hitting the database here and it's going to fail. If it fails, it's gonna succeed if it succeeds. So who cares? This is gonna work fine in my opinion. So we're good. We don't need to check if a profile follows.

fine in my opinion. So we're good. We don't need to check if a Profile follows another Profile in order to unfollow, we'll just try to unfollow. If it works, it works. If it doesn't, it doesn't, we'll move on. Alright, so for the PostController for liking a post, we'll do the same thing. Basically let's copy and paste, change the name, and instead of creating a, like we will remove the like, so

Basically let's copy and paste, change the name, and instead of creating a, like we will remove the like, so that's, we will know if it succeeded or not, so that we can include that in the J payload. And voila, once again, do we really need to check if the profile liked the post? I don't think we do. We try to remove the, like if it works, it works. If it doesn't, it doesn't. We're good. Alright, so as far as liking

Handling Post Deletions5:23

If it doesn't, it doesn't. We're good. Alright, so as far as liking and following are concerned, that's it. The posts are, are really where we're gonna need to think a little bit because what do we have? All of these URLs are very similar. We have profiles, status, post, and then we have a reply. A reply is just another post. So all we have to do is just delete it. We know the ID of that post. So there we go.

So all we have to do is just delete it. We know the ID of that post. So there we go. We know the ID of a quoted repost, so once again, we'll just delete it. Really, the only thing that we have to be concerned with is just a pure repost because as far as the URL is concerned, it's gonna have the original profile and it's gonna have the original post id. So we never really know what the ID is of a pure repost.

and it's gonna have the original post id. So we never really know what the ID is of a pure repost. We just know the ID of the original. So we're gonna have to do a little bit of work just to delete a repost, but for the most part it's gonna be simple. We have the post, we delete it, we're done. Okay, so let's follow the same ideas here. So that we're gonna have a POST request that is going to go to, let's just call it destroy.

So that we're gonna have a POST request that is going to go to, let's just call it destroy. And the name of the method will be destroy. The name of the route will be posts.destroy. And let's see, which method do I want to use? Let's use repost that way we have just the information that we have, we have the profile, we have the post, and that's all that we are going to get. We don't have any other request information. So this is going to be destroy.

We don't have any other request information. So this is going to be destroy. We want the current profile and then we want the post. So in the case of a normal post or acquitted post or a reply, the current profile is gonna be the same as the profile in the URL. So we just need to check that if the current profile ID is the same as the profile id, then we know whatever we have is something that we can delete just right out of the gate.

then we know whatever we have is something that we can delete just right out of the gate. So we'll say success equals post delete is greater than zero. And let's do this. Let's just go ahead and let's return here. That will eliminate some nesting, maybe. We'll see. I don't know. I like it. So that's what we're gonna go with. So yeah, that's what we'll do. If the current profile ID is the same as the profile id,

So yeah, that's what we'll do. If the current profileId is the same as the profileId, then we delete the post because, yeah, so it's when the current profile is not the same as the profile that we might be using or working with a repost. So we need to first of all make sure that it is a repost and we can do, what can we do? Let's look at some of the relationships on post. Okay, so we have profile, we have parents, we have replies,

Let's look at some of the relationships on Post. Okay, so we have profile, we have parents, we have replies, we have likes, we have reposts. We can use reposts. So we'll do this, we'll say posts, reposts, and we wanna repost where the profile_id is the same as the current profile_id. And then we just want to get the first one there so that if there is a record, if it exists, then it's a repost.

so that if there is a record, if it exists, then it's a repost. We want to delete the repost by the current profile, which we just selected here. But if it's not there, if we don't have a repost, then we don't wanna do anything. So if repost is null, let's do this; if repost is not, we want to delete that repost. So let's do this. Once again, it's a little bit

or rather, if it's not, we want to delete that repost. So let's do this. Once again, it's a little bit of repetition, but it's fine. So we'll have success. Uh, we will repost, delete, and yeah, so then we just need to return something and maybe we just return false for success. I mean, really. So let's do that. We'll do success up here. We will initialize it as false. So that's, if we get here, we're done.

We'll do success up here. We will initialize it as false. So that's, if we get here, we're done. If we get here, we're done. And if we get here, then success is false. And, and there we go. So that will be fine. Now, one thing we could do, we could make this a, a little cleaner if we used a policy, but I didn't want to create a policy just for this because I, I just don't see a policy being necessary. So as far as the functionality is concerned, we're we're,

Planned Cleanup and Refactor10:01

because I, I just don't see a policy being necessary. So as far as the functionality is concerned, we're we're, we're practically done. What I want to do going forward is a little bit of cleanup. There are some details that I want to touch on, like creating components for the, like the repost and the replies buttons. I also want to clean up some of the files so that we can get rid of the things that we don't need and perhaps do a little restructuring just

that we can get rid of the things that we don't need and perhaps do a little restructuring just to make things a little more organized and easier to work with.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟