Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sinres's avatar

Awareness of writing clean code. How to implement it in the REST API project?

Hello Everyone!

I was makeing a small CRM system where I have a sales and device inspection which are generated based on sales. This is not important but you must know it.

Schema of this project is standard from Laravel but in Controllers I display a view and in API directory have Controller for store record, update etc.

What's the problem? First - I am beginner in OOP hahaha but I know what is SOLID princpiles I trying use them Others? I feel this schema project is bad and I must write very clean code for your own satisfaction and for smaller future development problems.

What does he need from you? Does anyone know any project, repository with Laravel where I can look good schema of project REST API where I find Interface, Traits - a project where I can see what it should look like.

Look, this is my one of Controllers of API and code is very dirty. It would be useful to write it in a more clean way and create some interface

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\Http\Resources\DeviceInspection as DeviceInspectionResource;
use App\Http\Resources\DeviceInspectionCollection;
use App\DeviceInspection;

class DeviceInspectionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return new DeviceInspectionCollection(DeviceInspection::paginate(20));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function createDeviceInspection(Request $request, $sale_id)
    {
        // I count the number of inspections that the warranty covers
        $number_of_inspection = $request->guarantee_period / 12;
        
        //Parsing dates and calculating how many inspection to create
        $term_realization = Carbon::parse($request->term_realization);
        $current_date = Carbon::now()->format('Y-m-d');
        
        $current_date_record = Carbon::now()->format('Y');
        $term_realization_record = Carbon::parse($term_realization)->format('Y');

        $term_realization->format('Y-m-d');
        $term_realization = $term_realization->addYears($number_of_inspection);

        // A conditional statement that counts the number of records to be created in DB
        if ($term_realization <= $current_date) {
            $number_of_records = $current_date_record - $term_realization_record;
            $number_of_records = $number_of_records + $number_of_inspection;
            $number_of_records += 3;
        } else {
            $number_of_records = $number_of_inspection;
            $number_of_records += 3;
        }

        //Check if the sale is national(only when sales is national customer must perform device inspection )
        if ($request->sale_type == 'national') {
            for ($i=1; $i <= $number_of_records; $i++) {
                $deviceInspection = new DeviceInspection();
                $deviceInspection->user_id = '1';
                $deviceInspection->sale_id = $sale_id;
                $deviceInspection->inspection_date = $request->term_realization;
                $deviceInspection->warranty_service = '1';

                // Add one year for first inspection and make other
                $deviceInspection->inspection_date = Carbon::parse($deviceInspection->inspection_date);
                $deviceInspection->inspection_date->format('Y-m-d');
                $deviceInspection->inspection_date->addYears($i);

                // Check if the device is covered by warranty
                if ($request->guarantee_period != NULL && $term_realization >= $deviceInspection->inspection_date) {
                    $deviceInspection->warranty_service = 1;
                } else {
                    $deviceInspection->warranty_service = 0;
                }

                $deviceInspection->save();
            }
            if($deviceInspection->save()) {
                return new DeviceInspectionResource($deviceInspection);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return new DeviceInspectionResource(DeviceInspection::findOrFail($id));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $deviceInspection = DeviceInspection::findOrFail($id);
        $deviceInspection->user_id = Auth::id();
        $deviceInspection->inspection_date = $request->inspection_date;
        $deviceInspection->service_man = $request->service_man;
        $deviceInspection->status = $request->status;
        // This option is for admin in future
        // $deviceInspection->warranty_service = $request->warranty_service;
        $deviceInspection->save();
        
        if($deviceInspection->save()) {
            return new DeviceInspectionResource($deviceInspection);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $deviceInspection = DeviceInspection::findOrFail($id);
        $deviceInspection->delete();

        if($deviceInspection->delete()) {
            return new DeviceInspectionResource($deviceInspection);
        }
    }
}

I would like to create a nice project, I think I have a good idea but I have to implement it, refactor the code and it will certainly teach me a lot if I will try to write something clear with your help.

Thanks for help!

0 likes
0 replies

Please or to participate in this conversation.