I have an application that is based around an API. When a user logs in on my app, it gathers data from the API and merges it with my database. To gather all of this info from the API, I am forced to make multiple requests to get it all, and it takes too much time (let's say over 15 seconds) to make the user wait for it to happen. My app is built on Laravel and Vue, so I can make this into an ajax request. I'm thinking this is the best way.
I'm wondering how to structure this in my project. Let's say the data is a user's posts. I'm grabbing the user's new posts from the API, comparing it to the posts I have already stored, and creating posts in my database with the new posts from the API. Then updating the view with the posts in a paginated state.
Would I have a NewPostsController that handles this on its index method along with a method on my User model called something like "getNewPosts" or "mergeNewPosts"? I guess the ajax would fire that off and, once completed, get all of the posts for the user?
Here's what I'm thinking in pseudo-code:
// Http/NewPostsController.php
public function index()
{
Auth::user()->mergeNewPosts();
}
// App/User.php
public function mergeNewPosts() {
$posts = $client->get('/api/posts', {
// params
});
// add to database
$posts->each(function($post) {
Post::create($post);
});
}
In the front-end these would be called in sequence as ajax calls and render for the user when ready.
axios.get('/newposts')
.then(function() {
axios.get('/posts', function(response) {
this.posts = response.data;
});
});
I'm more-so concerned with how these functions should be divided within Laravel and which classes should be responsible for what. Is a controller where this should be located or is there another pattern that works here?
I'm probably thinking out loud more than anything, but if anyone has suggestions I'm open to them. Thanks! I can provide more details if needed.