untymage
6 months ago
393
2
Laravel

Using external API to create record directly but DRY way

Posted 6 months ago by untymage

I have two way to create a record (Movie post) One is manually with html form another one is with external APIs, Both have separated controllers, But one thing that bothers me is How to avoid duplicates ? For example i validates data which come from html form and the other hand the data comes from external APIs, So my questions:

  1. How to validates data that come from external API (DRY way)
  2. How to forward external api data to antother controller to avoid duplicates

From html:

public function store(MovieRequest $request)
{
    Movie::create([
        'title' => $request->title,
        'year'  => $request->year,
    ]);
    // ...
}

from APIs:

public function store(Request $request)
{
    
    $client = new GuzzleHttp\Client();
    $res = $client->get($request('apiURL'))->getBody();
    
    How to pass $res to above controller to avoid duplicates and valdiation here ?
    

}

Please sign in or create an account to participate in this conversation.