Let’s write our last controller, which is the reviews controller. I have the prompt ready here, just a portion of the prompt. Then I added the domain models and the DB set and the model builder definitions. But one thing that I also added is this important section, where I said not to lazy load the endpoint definitions and generate everything fully. We’ve seen that it lazy loads, so I’m just telling it not to. Let’s grab that, go to GBD4, and let’s see what it generates.
So we’re back. It generated everything fully, as you can see. We’re going to grab that and go to WinMerge. Paste that over and remove the using statements. Okay, so let’s go ahead and talk about this. I’m not going to diff it. We probably will diff it at the end, but let’s just go line by line, just like we did with the other controller, and talk about each section individually.
So we are getting App DB context user manager here, DB context user manager here. Just different order, different namings. That’s all fine. Then we are going into the get book reviews. So this is going to be the reviews for a given book. It’s going to grab the book ID, make sure that it actually exists, and then grab the reviews of that book. Same thing is happening on the right side, which is what I’ve wrote. The only difference is that it’s using context.set, and I’m actually using context.reviews. So there’s no particular performance differences between the two ways of doing it. It is just two ways of accessing data from the database. I think the more important part here is to be consistent with how you’re doing it within your various controllers. Since I’m not using context.set in my other ones without having a good reason to use it, I don’t want to divert and break the consistency, so I will stick with what I’m doing. But I think that could have been easily modified by another prompt.
The next one would be to get my review. So it’s the user reviews. In this case, you’re using user manager, making sure that the user exists. In both cases, it’s unauthorized if it doesn’t exist. Then we’re gonna grab the reviews of a user. Same thing is happening here, and once again, it’s using context.set. Same applies here.
As we can see, this one is authorized as an authorized tag. I also have authorized these endpoints, which are the reviews of a user posting a review and also deleting a review.
So let’s just see the next one, which is the post my review. In this case, we’re going to get a book ID, which is a good, and then we’re going to get review DTO. We’re going to make sure that the user exists. In both cases, that’s happening. And then make sure that the book that the review is being left for exists. So that also is being checked here. The difference is that I’m using model state, which I think is cleaner in this case. It is repeating logic, so that’s one thing that was missed. I would say not a big deal, but definitely was missed. Review, then it’s creating the review and then adding it to the context in my case and saved. In this case, it’s using set, just different ways of saving data.
Lastly, delete. So in the delete, what happens is we are getting the user, making sure it’s authenticated, getting the review that was passed in the ID, and if the review is retrieved, if not, we say not found. If the review is retrieved, what happens is we make sure that the review that was left and is trying to be deleted was left by the same user that’s trying to delete it. Otherwise, it’s going to say forbid. And then if everything matches up, then it’s going to delete. Pretty much the same thing here as well, so the logic matches up.
I think the only thing I would say in this case was the rating. This could have just been using model state, but that could have just been a quick prompt, and it would have modified it right after. So I think overall, it looks pretty good. I’m still going to grab the version that I have, so I’m going to grab this, port this over to the project that we already created. Come back here, create a controller. Let’s see, empty controller, and then that, and let’s just say reviews. Okay, reviews controller. Let’s add that and then replace it with the contents. Few things we need to bring over, so Entity Framework Core, and I think the rest it already knows how to find. Let’s rebuild everything. Okay, succeeded. So I think at this point, we have everything we need. We have the auth controller, we have book orders reviews, we have the book service, we have all the DTOs, domain models, AppDB context is filled out in program CS, glues everything together nicely. Next, we’re going to test everything together, and afterwards, we’re going to talk about the lessons learned. But that’s it for this video. If it was useful, like, comment, subscribe. I’ll see you in the next video.