TuffRivers
7 months ago
429
1
Laravel

Code Review for my first End Point

Posted 7 months ago by TuffRivers

Hi All,

I am building my first web service API (first party, i will build FE in angular). If you have time, please review my first endpoint which is create user. Purpose of this endpoint is to receive user details from a form, validate these details, insert into database, and send a welcome email. Currently everything works, i feel my controller is very clean, but i know i have A LOT to improve on.

Warnings: Im new to OOP (i built lots of adhoc PHP scripts for data sync and api stuff, nothing production quality) im trying not to use Eloquent (more of a learning project so i want to get dirty)

Concerns:

  1. Where am i not using an object that it would be best i could? I am not using my model at all (app\models\user), is this a bad thing? Where should i be initiating it and using ? This is my biggest concern.

  2. I feel like there is a much better way to pass data between my controller and my SendEmail/WelcomeEmail task but i just cant figure out the best way, this is just the way that worked.

  3. Is the repository pattern over kill, am i even using it correctly?

UserController.php

class UserController extends BaseController
{
    /**
     * Register api
     *
     * @return \Illuminate\Http\Response
     */
    public function store(UserStoreRequest $request, UserRepository $userRepo)
    {     
       
         $validated = $request->validated();

         $userId = $userRepo->insert($validated);

         $userDetails = $userRepo->find($userId);
        
         SendEmail::dispatch($userDetails);

         return $this->sendResponse($userDetails, 'User created.');
    }


}

UserStoreRequest.php //form validation

class UserStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true; //no auth currently set up
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
    'first_name' => 'string|required',
    'last_name' => 'string|required',
    'client_id' => 'nullable',
    'email' => 'unique:users,email|required|email',
    'password' => 'required',
    'c_password' => 'required|same:password',
        ];
    }

}

UserRepositoryInterface.php

interface UserRepositoryInterface
{

    public function insert($data);
    public function find($id);

}

UserRepository.php

class UserRepository implements UserRepositoryInterface
{
    /**
     * Create New User
     */
    public function store($validated)
    {
       
        $insertData['client_id'] = $validated['client_id'];
        $insertData['first_name'] = $validated['first_name'];
        $insertData['last_name'] = $validated['last_name'];
        $insertData['created_at'] = now();  
        $insertData['updated_at'] = now();
        $insertData['email'] = $validated['email'];
        $insertData['password'] =  bcrypt($validated['password']);

        $id = DB::table('users')->insertGetId($insertData); 
  
        return $id;
       
    }

     /**
     * Find User by ID
     */
    public function find($id)
    {

       $userDetails = DB::table('users')->select('id','first_name','last_name','client_id','email')->where('id', $id)->get();

        return $userDetails;

    }

}

SendMail.php //job to send email

class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $userDetails;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userDetails)
    {    
        
        $this->userDetails = $userDetails; 
       
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        
        $email = new SendWelcomeEmail($this->userDetails);
        Mail::to($this->userDetails[0]->email)->send($email);

    }
}

SendWelcomeEmail.php //pass details to view so i can build dynamic welcome email (name, etc)

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendWelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $userDetails;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($userDetails)
    {

	$this->userDetails = $userDetails;
         
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome');
    }
}

I really appreciate anyone who takes the time to comb through this and offer their insights as well as criticisms.

Thank you.

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