Building a Support Ticket in laravel lucid architecture part 4

in #utopian-io8 years ago (edited)

cover-Recovered.jpg

What Will I Learn?

For this installment, we are going to learn the following

  • Creating a validator
  • Creating a validateTicketInputJob
  • We are also going to learn how to create a repository.

Requirements

Write here a bullet list of the requirements for the user in order to follow this tutorial.

  • Download php 7.2.1
  • Download Composer.exe
  • Download xampp or wamp for windows.

Difficulty

  • Intermediate

Introduction

In lucid architecture we make our controllers as thin as possible. The idea is to get the controller to produce just one task at different functions.
In series 1 we talked about repositories, repositories are the only way we connect to our models. This is called decoupling of the data source from the actual mechanism.

What is our task today.

Today, we are going to introduce jobs and repository. we are going to work on the store ticket feature and all its components.

So lets start

I guess by now, you know how it works, lets initiate the route for the mechanism

Route::post('new_ticket', 'TicketController@store');

web.png
fine we have thee route, let head over to the controller we generated in our previous post and look for the store function and add the code below

 return $this->serve(StoreTicketFeature::class);

remember to add or rather include the StoreTicketFeature at the top by adding this

use App\Services\Web\Features\StoreTicketFeature;

ticketcontroller.png
One let generate the feature from command, if u still can't look at my previous tutorial
on the CLI enter

lucid make:feature StoreTicket web

Now lets analyze the feature, we are going to store data from a user, what are the necessary tools or jobs we need, i will list it out.

  1. we should be able to validate the users input,
  2. if passed, we can store else return a message to the user.

For the following, we need the following

  1. A validator
  2. validateTicketInputJob
  3. TicketRepository to interface with the model
  4. StoreTicketJob.
  5. SendBackWithErrorMessageJob to give the user a message if validation fails

So lets build the following
.
lets create the validator.php file
Create a folder called validators and and the file called TicketValidator.php
paste the code below

<?php
namespace App\Domains\Validators;

use App\Foundation\AppValidator;

class TicketValidator extends AppValidator {

    protected $rules = [
        'title'     => 'required',
        'category'  => 'required',
        'priority'  => 'required',
        'message'   => 'required'
    ];

    protected $messages = [
        'required'  =>  "We'd love to know your :attribute, so please fill it properly.",

    ];
}

ticketvalidator.png

The above simply tells the user what must be filled before he or she can proceed with the ticket.

Next up is the ticket repository
Create a folder called Repositories under the data folder and create the file called ticket repository and add the code below.

<?php

namespace App\Data\Repositories;

use Framework\Ticket;


/**
 * Base Repository.
 */
class TicketRepository extends Repository
{
    /**
     * The model instance.
     *
     * @var \Illuminate\Database\Eloquent\Model
     */
    public $model;

    public function __construct()
    {
        $this->model = new Ticket;

        parent::__construct($this->model);

    }

   
}

ticketrepository.png

Okay we done, let now make our jobs that we are to use in our feature.
okay i remember, we are to create the following jobs

  1. StoreTicketJob
  2. ValidateTicketInputJob
  3. SendBackWithErrorMessageJob

Remember all we have to do is generate them.
In yourlucid CLI enter each of the command

lucid make:job StoreTicket Ticket

lucid make:job ValidateTicketInput Ticket

lucid make:job SendBackWithErrorMessage Ticket

Find each of the jobs in the Ticket domain and paste in each file

StoreTicket.php

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

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

class StoreTicketJob 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(TicketRepository $repo)
    {
        $repo->model->create($this->data);
    }
}

storeticketjob.png

ValidateTicketInputJob

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

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


class ValidateTicketInputJob 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(TicketValidator $validator)
    {
        return $validator->validate($this->data);
    }
}

validateticketjob.png

SendBackWithErrorMessageJob.php

Note: This job should be generated in the Http domain

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

use Lucid\Foundation\Job;

use Redirect;
use Illuminate\Http\Request;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Request $request)
    {
        $request->session()->flash('messages', $this->data['messages']);

        if ($this->data['input'])
        {
            return Redirect::back()->withInput();

        } else {
            return Redirect::back();            
            
        }
    }
}

sendbackwitherrormessagejob.png

We are almost done, back to our StoreTicketFeature.php
paste this

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

use Lucid\Foundation\Job;

use Redirect;
use Illuminate\Http\Request;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Request $request)
    {
        $request->session()->flash('messages', $this->data['messages']);

        if ($this->data['input'])
        {
            return Redirect::back()->withInput();

        } else {
            return Redirect::back();            
            
        }
    }
}

We are done for today, just completely stored a ticket you can now check our database.

Previous Tutorial



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]

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.13
TRX 0.35
JST 0.035
BTC 115969.92
ETH 4470.84
SBD 0.86