Final Tweaks

After a peer-review some issues have come to my attention. I had not fully implemented recursive deletion of children when deleting their parent, as well as this, the application has been known to leave orphan images.

The list of issues to iron out are as follows:

  • Replies deleting on deletion of a comment
  • Image and all comments deleting on deletion of a post
  • All images, posts and comments deleting on deletion of a subweddit
  • When a user is deleted, their posts and comments delete with them

Replies deletion -


For this I entertained many ideas within the Comment controller 'delete' method. Getting an array of the children and deleting the array, looping through the children deleting each individually, then deleting children of children. 

None are as simple as this - a simple 'on delete cascade' relationship attached to the parent_id field on the comments table.


As the parent is deleted, the child associated is deleted. This starts a chain reaction and cascades down to the following children, and their children and so on.

An effective and clean solution! 


Post deletion - 


We can utilise this on delete cascade relationship for a lot of the following tasks.

In this case, when a post is deleted the associated comments must be deleted along with it, this is more simple than the previous example as all comments on a post contain a 'post_id', so once this is deleted cascade to every comment with the post_id of the deleted post. We can add this in to our comments table:


We can delete the associated posts' image in storage by deleting $post->img, which is a full path to the image. This is a view of PostControllers' Delete method.



Subweddit deletion - 

In the previous two examples, we are deleting one model, Comment. In this scenario, we are to delete two models, both Comment and Post, when a subweddit deletes. Our migration in to the posts table would look a bit like this:


Once the associated posts of a subweddit delete, the comments will all automatically delete as we set a cascading delete earlier in the post.

We can delete all associated logos/post images by deleting the entire subweddits' directory in storage. Below is the delete method in SubwedditController.



A full nuclear option.

User deletion - 

This last feature is not necessary, as currently there is no way for a user to delete their account. But for the future this is a feature worth implementing should their be an ability to delete your own account, or an admin panel is created.

When a user is deleted, their posts and comments should delete. Not included in this is subweddit. Why? Because a user is not an owner of a subweddit, merely a moderator. In future, it would be the case that a subweddit has multiple moderators, and obviously a Weddit admin user, with super powers.

So we must modify both the posts and comments table to add a cascading delete once the user's user_id is deleted.

The same relationship can be added to both 'comments' and 'posts':





Comments

Popular posts from this blog

Final Database Design

Views and Policies

Validation