Record paddock state changes

This commit is contained in:
2020-04-17 11:30:44 +02:00
parent b71678bf3b
commit 8a0b9bf776
5 changed files with 85 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Paddock;
use App\PaddockStateChange;
use Redirect;
class PaddockController extends Controller
@@ -85,11 +86,26 @@ class PaddockController extends Controller
'name' => 'required',
]);
// Was there a state change?
$paddock = Paddock::find($id);
$state_changed = FALSE;
if ($paddock->isClosed && ($request->state === 'opened')) {
$state_changed = TRUE;
} else if (!$paddock->isClosed && ($request->state === 'closed')) {
$state_changed = TRUE;
}
// Record the state change, if needs be
if ($state_changed) {
$change = new PaddockStateChange(['state' => $request->state]);
$paddock->state_changes()->save($change);
}
$update = [
'name' => $request->name,
'isClosed' => $request->state !== 'opened',
];
Paddock::where('id', $id)->update($update);
$paddock->update($update);
return Redirect::to('paddocks')
->with('success', 'Enclos mis à jour.');

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PaddockStateChangeController extends Controller
{
//
}

View File

@@ -15,4 +15,12 @@ class Paddock extends Model
'name',
'isClosed',
];
/**
* Get the state changes for a paddock
*/
public function state_changes() {
return $this->hasMany('App\PaddockStateChange');
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PaddockStateChange extends Model
{
protected $fillable = [
'state',
];
public function paddock() {
return $this->belongsTo('App\Paddock');
}
}