Creating a support ticket with laravel lucid architecture part 7

in #utopian-io7 years ago (edited)

cover-Recovered-Recovered.jpg

In our last tutorial, we created the ListTicketByIdFeature and the ListTicketByIdJob, not forgetting we also created the routes and the view for this feature.
lets recall; the ListTicketByIdFeature is responsible for for retrieving a ticket from the repository via it's id
To achieve our the previous tutorial, we had to pass the id of the ticket into the route and inject the id to the constructor method of our feature via a the TicketController

Requirements

The following are the requirements for this tutorial

  1. Download php 7.2.1
  2. Download Composer.exe
  3. Download xampp or wamp for windows.

Difficulty level

This tutorial is rated as intermediate.

Our task for today.

Our task for today, we would be working on the StoreCommentFeature, which we are going to create a controller, some jobs and the route for this task.

Overview of the task.

The tickets are supposed to be commented by the user and can also be comment by the admin, which are also comments. So we are suppose to have a comment table in our database, which we have already created and the relationship set. "relationship -> all comments belongs to a user and a user has many comment, lastly a ticket can have many comment and a comment belongs to a ticket".

Lets begin

Where do we start from, yeah i remember, its the route again. open the web.php file we have been using and add this new line below

Route::post('comment', 'CommentController@postComment');
image.png

Next up we need to create our CommentController.php file
you know how we do this already, it done by generating with lucid command line, i guess you can open up the lucid command line now enter the command

lucid make:controller CommentController web

we have successfully created the controller
open the CommentController and paste the code below. the fuction postComment yhas to correspond with the one on the route.

 public function postComment()
    {
        return $this->serve(StoreCommentFeature::class);
    }

image.png

Remember to use the feature at the top
Noticed we are serving the StoreCommentFeature, so we have to create this feature.

lucid make:feature StoreCommentFeature web
Open the newly created feature and add this below

<?php
namespace App\Services\Web\Features;

use Lucid\Foundation\Feature;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Domains\Comments\Jobs\ValidateCommentInputJob;
use App\Domains\Comments\Jobs\StoreCommentJob;
use Illuminate\Support\Facades\Auth;
use App\Domains\Http\Jobs\SendBackWithErrorMessageJob;

class StoreCommentFeature extends Feature
{
    public function handle(Request $request)
    {
        try {

            $this->run(ValidateCommentInputJob::class,[

                'data' => $request->all(),
            ]);
            
            $data = [

                'ticket_id' => $request->input('ticket_id'),
                'user_id'   => Auth::user()->id,
                'comment'   => $request->input('comment'),
            ];

            $this->run(StoreCommentJob::class,[

                    'data' => $data,
            ]);

             return redirect()->back()->with("status", "Your comment has be submitted.");

        } catch (ValidationException $e) {
            
            $data = [
                'messages' => $e->validator->messages(),
                'input' =>  $request->input()
            ];

            return $this->run(SendBackWithErrorMessageJob::class, ['data' => $data]);

        }
    }
}

image.png

Explaining the feature above.

For us to save a post, which is the comment, we have to validate what the user enter to know if he or she entered a valid input. the validation on the comment is achieved using the ValidateCommentInputJob and if the validation fails, an exception is thrown and the message instance from the error sent to the user, else we sent to the StoreCommentJob.

So before we continue, lts create the jobs for the feature, which include the following

  1. ValidateCommentInputJob
  2. StoreCommentJob

Creating the ValidateCommentInputjob

We generate our job in the same manner

<?php
namespace App\Domains\Comments\Jobs;

use Lucid\Foundation\Job;
use App\Domains\Validators\CommentValidator;


class ValidateCommentInputJob extends Job
{
    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(CommentValidator $validator)
    {
        return $validator->validate($this->data);
    }
}

This validator job is not different from the ticket input validator, which accepts the data passed by the $request->input() from the user.
image.png

Creating the StoreCommentJob

This is called after validation is passed, the data saved include the user_id, ticket_id and the comment.
creating this job, we generate using the command below.

lucid make:job StoreComment web

Open the generated file and add the code below.

<?php
namespace App\Domains\Comments\Jobs;

use Lucid\Foundation\Job;
use App\Data\Repositories\CommentRepository;

class StoreCommentJob extends Job
{
    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected  $data;
    public function __construct($data)
    {
        $this->data =$data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(CommentRepository $repo)
    {
        $repo->model->create($this->data);
    }
}

image.png

This particular job, uses the comment repository which interface with the comment model for storing and retrieving data.

  $repo->model->create($this->data);

the code above is responsible for saving the data to the model

Back to the StoreCommentFeature\

After storing the Comments, the piece of code below send as redirect to the url after the post is save with a message instance to let the user know that his comments has been saved.

 return redirect()->back()->with("status", "Your comment has be submitted.");

in the next tutorials we are going to be talking on Middlewares and how we close a particular ticket.

previous series below

See you



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Congratulations @sirfreeman! You have received a vote as a way to thank you for supporting my program.

Hey @sirfreeman I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60202.34
ETH 2423.33
USDT 1.00
SBD 2.43