Skip to content

Commit

Permalink
Added parts; commented
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomatic committed Feb 5, 2017
1 parent 3a3e251 commit 760ce87
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 13 deletions.
37 changes: 37 additions & 0 deletions app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
class ClientController extends Controller
{


/**
* Lists/searches clients based on their name
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$clients = Client::search(request()->query('search', null))
Expand All @@ -17,10 +23,22 @@ public function index()
}


/**
* Redirects to create form
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showCreateForm(){
return view('client.create');
}


/**
* Stores new client to DB
*
* @param ClientCreateRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(ClientCreateRequest $request){

$client = new Client;
Expand All @@ -33,19 +51,38 @@ public function store(ClientCreateRequest $request){
}


/**
* Shows a single client
*
* @param Client $client
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Client $client){

return view('client.show', compact('client'));

}


/**
* Redirects to edit form
*
* @param Client $client
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showEditForm(Client $client){

return view('client.edit', compact('client'));
}


/**
* Stores edited client to db
*
* @param Client $client
* @param ClientCreateRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function edit(Client $client, ClientCreateRequest $request){

$client->update($request->all());
Expand Down
42 changes: 40 additions & 2 deletions app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
class DeviceController extends Controller
{


/**
* Lists/searches all device based on serial number
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$devices = Device::search(request()->query('search', null))
Expand All @@ -19,19 +23,38 @@ public function index()
return view('device.index', compact('devices'));
}


/**
* Displays single device
*
* @param Device $device
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Device $device){

$jobs = $device->jobs()->with('user')->latest()->paginate(self::DEFAULT_PAGINATION);
$jobs = $device->jobs()->with('user', 'parts')->latest()->paginate(self::DEFAULT_PAGINATION);

return view('device.show', compact('device', 'jobs'));
}


/**
* Redirects to new device form
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showCreateForm()
{
return view('device.create');
}


/**
* Stores a new device
*
* @param DeviceCreateRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(DeviceCreateRequest $request)
{
$device = new Device();
Expand All @@ -43,11 +66,26 @@ public function store(DeviceCreateRequest $request)
return redirect()->action('DeviceController@show', $device->id);
}


/**
* Redirects to edit form of a device
*
* @param Device $device
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showEditForm(Device $device)
{
return view('device.edit', compact('device'));
}


/**
* Stores edited device to database
*
* @param Device $device
* @param DeviceCreateRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function edit(Device $device, DeviceCreateRequest $request)
{
$device->update($request->all());
Expand Down
127 changes: 121 additions & 6 deletions app/Http/Controllers/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,143 @@
use App\Device;
use App\Http\Requests\JobCreateRequest;
use App\Job;
use App\Part;
use Illuminate\Support\Facades\DB;

class JobController extends Controller
{

/**
* Redirects to create form for a job
*
* @param Device $device
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showCreateForm(Device $device)
{
return view('job.create', compact('device'));
}


/**
* Stores job data and binds it to user and device entry
*
* @param Device $device
* @param JobCreateRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Device $device, JobCreateRequest $request)
{
$job = new Job();

$job->fill($request->all());
DB::transaction(function () use ($request, $device) {
$job = new Job();

$job->fill($request->all());

$job->device()->associate($device);

$job->user()->associate(auth()->user());

auth()->user()->jobs()->save($job);

$device->jobs()->save($job);

$parts = [];
foreach ($request->get('parts', []) as $part) {
$part_db = new Part();
$part_db->fill($part);
$parts[] = $part_db;
}
$job->parts()->saveMany($parts);
});

return redirect()->action('DeviceController@show', $device->id);
}


/**
* Displays job details
*
* @param Job $job
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Job $job)
{
$parts = $job->parts;
$device = $job->device;
return view('job.show', compact('device', 'job', 'parts'));
}


/**
* Redirects to job edit form
*
* @param $job_id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showEditForm($job_id)
{
$job = Job::with('user', 'device', 'parts')
->where('id', $job_id)
->first();

return view('job.edit', compact('job'));
}


/**
* Stores edited job on submit
*
* @param Job $job
* @param JobCreateRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function edit(Job $job, JobCreateRequest $request)
{
DB::transaction(function () use ($request, $job) {

foreach ($job->parts as $part) {
$part->delete();
}

$job->device()->associate($device);
$job->update($request->all());

$job->user()->associate(auth()->user());
$job->user()->associate(auth()->user());

auth()->user()->jobs()->save($job);
auth()->user()->jobs()->save($job);

$device->jobs()->save($job);
$parts = [];
foreach ($request->get('parts', []) as $part) {
$part_db = new Part();
$part_db->fill($part);
$parts[] = $part_db;
}
$job->parts()->saveMany($parts);

});

return redirect()->action('JobController@show', $job->id);;
}


/**
* Removes job and subsequent parts from the database
*
* @param Job $job
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(Job $job)
{
$device = $job->device;

DB::transaction(function () use ($job) {
foreach ($job->parts as $part) {
$part->delete();
}
$job->delete();
});

return redirect()->action('DeviceController@show', $device->id);
}

}
6 changes: 5 additions & 1 deletion app/Http/Requests/JobCreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public function authorize()
*/
public function rules()
{

return [
'notes' => 'string',

'parts.*.description' => 'string|max:255',
'parts.*.serial_number' => 'string|max:255|unique:parts',
'parts.*.manufacturer' => 'string|max:255',
'parts.*.type.*' => 'string|max:255',
];
}
}
4 changes: 4 additions & 0 deletions app/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public function user(){
return $this->belongsTo(User::class);
}

public function parts(){
return $this->hasMany(Part::class);
}

}
23 changes: 23 additions & 0 deletions app/Part.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;

class Part extends Model
{
use Eloquence;

protected $fillable = [
'manufacturer',
'type',
'serial_number',
'description',
];

public function job(){
return $this->belongsTo(Job::class);
}

}
11 changes: 11 additions & 0 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@
'notes' => $faker->sentence(),
];
});


$factory->define(App\Part::class, function (Faker\Generator $faker) {

return [
'manufacturer' => $faker->randomElement(array('Kavo', 'Planmeca', 'NSK')),
'type' => $faker->randomElement(array('ležaj', 'osovina', 'elektronika')),
'serial_number' => str_random(10),
'description' => $faker->sentence(),
];
});
Loading

0 comments on commit 760ce87

Please sign in to comment.