Polymorphic relations, repositories and services
Hi!
I'm playing a bit with my app being written in laravel thinking how can I improve its code. I've been reading a lot about repository pattern implementations in laravel but it's not so clear for me.
I followed this guide: https://medium.com/@jsdecena/refactor-the-simple-tdd-in-laravel-a92dd48f2cdd
But after a while I realized it doesn't make much sense to split it all into base repo and interface and then specific repo&interface everytime. It feels to be a bit overengineered. Especially, since I wanted to use one repo inside of another which seems to be a bad idea.
Let's describe my problem:
Part of my application is a blog system. There are models such as category, post, user. Both category and post can have one image/thumbnail and user can upload an avatar. Later, there will be something like albums etc.
Since there is at least few models which can have some kind of media attached to it, I decided to make a new table and model, simply called - media. It has fields such a file name, path, mime type etc. Also, I used there a polymorphic relation. Post, user, category morph to one, albums morph to many. Great.
First question: should I also make a fields like "media_id" in post table, so database is aware of relationship between this tables? Seems like a good idea, but on the other hand - quite againt polymorphic relation concept.
Saving something to this table is pretty much always the same. As a param I would give a instance of UploadedFile and grab required info to fullfil media table. Right now, I have methods such as "setAvatar", "setImage" in various models, but they pretty much share same implementation. Overriding default create method for a model doesn't seem to be the best idea though. Also, the thing is that I would like to use a relations, for example $user->avatar()->create() so mediable_id and mediable_type column would get autocompleted.
Second question: What would be the best solution for this situation? Should I place method "storeMedia" in i.e. media repository? Maybe in media service? Then, from internal model methods i.e. setAvatar() i would just return media repo/service storeMedia method?
Why I'am talking so much about repositories. I really hate having a lot of methods inside of model. In model I want to store only relationships, casts etc. All methods to grab latest or trending posts, save/get image I would like to extract somewhere else.
Third question: So, what in this situation is a difference between repositories and services. I've been looking through many github repositories and there were a lot of implementations, very often - there was no real difference between kind of jobs performed by service and repository. Please refer to this specific situation with my media table.
Forth question: Should each repository implement an interface? Then, when using my repository in i.e. controller should I use repository itself or just interface?
tl;dr; I want to organize things a bit, but at same time - I do not really want to overdo it.
Please or to participate in this conversation.