Added Worklog model
parent
acf84f222d
commit
f732a4af6f
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Migrations;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Engelsystem\Database\Migration\Migration;
|
||||
use Engelsystem\Models\Worklog;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use stdClass;
|
||||
|
||||
class CreateWorklogsTable extends Migration
|
||||
{
|
||||
use ChangesReferences;
|
||||
use Reference;
|
||||
|
||||
/**
|
||||
* Run the migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$this->schema->create('worklogs', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$this->referencesUser($table);
|
||||
$this->references($table, 'users', 'creator_id');
|
||||
$table->decimal('hours');
|
||||
$table->string('comment', 200);
|
||||
$table->date('worked_at');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
if ($this->schema->hasTable('UserWorkLog')) {
|
||||
$connection = $this->schema->getConnection();
|
||||
/** @var stdClass[] $previousRecords */
|
||||
$previousRecords = $connection
|
||||
->table('UserWorkLog')
|
||||
->get();
|
||||
|
||||
foreach ($previousRecords as $previousRecord) {
|
||||
$room = new Worklog([
|
||||
'user_id' => $previousRecord->user_id,
|
||||
'creator_id' => $previousRecord->created_user_id,
|
||||
'worked_at' => $previousRecord->work_timestamp,
|
||||
'hours' => $previousRecord->work_hours,
|
||||
'comment' => $previousRecord->comment,
|
||||
]);
|
||||
$created_at = Carbon::createFromTimestamp($previousRecord->created_timestamp);
|
||||
$room->setAttribute('id', $previousRecord->id);
|
||||
$room->setAttribute('created_at', $created_at);
|
||||
$room->setAttribute('updated_at', $created_at);
|
||||
$room->save();
|
||||
}
|
||||
|
||||
$this->changeReferences(
|
||||
'UserWorkLog',
|
||||
'id',
|
||||
'worklogs',
|
||||
'id'
|
||||
);
|
||||
|
||||
$this->schema->drop('UserWorkLog');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migration
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$this->schema->create('UserWorkLog', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$this->referencesUser($table);
|
||||
$table->integer('work_timestamp');
|
||||
$table->decimal('work_hours');
|
||||
$table->string('comment', 200);
|
||||
$this->references($table, 'users', 'created_user_id');
|
||||
$table->integer('created_timestamp')->index();
|
||||
});
|
||||
|
||||
foreach (Worklog::all() as $record) {
|
||||
/** @var Worklog $record */
|
||||
$this->schema
|
||||
->getConnection()
|
||||
->table('UserWorkLog')
|
||||
->insert([
|
||||
'id' => $record->id,
|
||||
'user_id' => $record->user_id,
|
||||
'work_timestamp' => $record->worked_at->timestamp,
|
||||
'work_hours' => $record->hours,
|
||||
'comment' => $record->comment,
|
||||
'created_user_id' => $record->creator_id,
|
||||
'created_timestamp' => $record->created_at->timestamp,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->changeReferences(
|
||||
'worklogs',
|
||||
'id',
|
||||
'UserWorkLog',
|
||||
'id'
|
||||
);
|
||||
|
||||
$this->schema->drop('worklogs');
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Engelsystem\Models\User\User;
|
||||
use Engelsystem\Models\User\UsesUserModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $creator_id
|
||||
* @property float $hours
|
||||
* @property string $comment
|
||||
* @property Carbon $worked_at
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*
|
||||
* @property-read User $creator
|
||||
*
|
||||
* @method static QueryBuilder|Worklog[] whereId($value)
|
||||
* @method static QueryBuilder|Worklog[] whereCreatorId($value)
|
||||
* @method static QueryBuilder|Worklog[] whereWorkedAt($value)
|
||||
* @method static QueryBuilder|Worklog[] whereHours($value)
|
||||
* @method static QueryBuilder|Worklog[] whereComment($value)
|
||||
*/
|
||||
class Worklog extends BaseModel
|
||||
{
|
||||
use UsesUserModel;
|
||||
|
||||
/** @var bool Enable timestamps */
|
||||
public $timestamps = true;
|
||||
|
||||
/** @var array The attributes that should be mutated to dates */
|
||||
protected $dates = [
|
||||
'worked_at',
|
||||
];
|
||||
|
||||
/** @var string[] */
|
||||
protected $casts = [
|
||||
'user_id' => 'integer',
|
||||
'creator_id' => 'integer',
|
||||
];
|
||||
|
||||
/** The attributes that are mass assignable. */
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'creator_id',
|
||||
'hours',
|
||||
'comment',
|
||||
'worked_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'creator_id');
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Test\Unit\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Engelsystem\Models\Worklog;
|
||||
use Engelsystem\Models\User\User;
|
||||
|
||||
class WorklogTest extends ModelTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Models\Worklog::creator
|
||||
*/
|
||||
public function testCreator(): void
|
||||
{
|
||||
$user1 = User::create([
|
||||
'name' => 'user1',
|
||||
'password' => '',
|
||||
'email' => 'user1@example.com',
|
||||
'api_key' => '',
|
||||
]);
|
||||
$user2 = User::create([
|
||||
'name' => 'user2',
|
||||
'password' => '',
|
||||
'email' => 'user2@example.com',
|
||||
'api_key' => '',
|
||||
]);
|
||||
|
||||
$worklog = new Worklog();
|
||||
$worklog->user()->associate($user1);
|
||||
$worklog->creator()->associate($user2);
|
||||
$worklog->hours = 4.2;
|
||||
$worklog->comment = 'Lorem ipsum';
|
||||
$worklog->worked_at = new Carbon();
|
||||
$worklog->save();
|
||||
|
||||
$savedWorklog = Worklog::first();
|
||||
$this->assertEquals($user2->name, $savedWorklog->creator->name);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue