Permalink
Browse files

πŸ’Ύ Add models and migrations

  • Loading branch information...
lorisleiva committed Aug 16, 2018
1 parent 1642c81 commit 6b9fe6505e6b0deccf32e91ca2f0901f425f41f0
View
@@ -0,0 +1,24 @@
<?php
namespace App;
use App\Event;
use App\GoogleAccount;
use Illuminate\Database\Eloquent\Model;
class Calendar extends Model
{
protected $fillable = [
'google_id', 'name', 'color', 'timezone',
];
public function googleAccount()
{
return $this->belongsTo(GoogleAccount::class);
}
public function events()
{
return $this->hasMany(Event::class);
}
}
View
@@ -0,0 +1,35 @@
<?php
namespace App;
use App\Calendar;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $with = ['calendar'];
protected $fillable = [
'google_id', 'name', 'description', 'allday', 'started_at', 'ended_at',
];
public function calendar()
{
return $this->belongsTo(Calendar::class);
}
public function getStartedAtAttribute($start)
{
return $this->asDateTime($start)->setTimezone($this->calendar->timezone);
}
public function getEndedAtAttribute($end)
{
return $this->asDateTime($end)->setTimezone($this->calendar->timezone);
}
public function getDurationAttribute()
{
return $this->started_at->diffForHumans($this->ended_at, true);
}
}
View
@@ -0,0 +1,29 @@
<?php
namespace App;
use App\Calendar;
use App\Services\Google;
use App\User;
use Illuminate\Database\Eloquent\Model;
class GoogleAccount extends Model
{
protected $fillable = [
'google_id', 'name', 'token',
];
protected $casts = [
'token' => 'json',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function calendars()
{
return $this->hasMany(Calendar::class);
}
}
View
@@ -2,28 +2,24 @@
namespace App;
use Illuminate\Notifications\Notifiable;
use App\GoogleAccount;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function googleAccounts()
{
return $this->hasMany(GoogleAccount::class);
}
}
@@ -0,0 +1,44 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGoogleAccountsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('google_accounts', function (Blueprint $table) {
$table->increments('id');
// Relationships.
$table->unsignedInteger('user_id');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
// Data.
$table->string('google_id');
$table->string('name');
$table->json('token');
// Timestamps.
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('google_accounts');
}
}
@@ -0,0 +1,45 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCalendarsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('calendars', function (Blueprint $table) {
$table->increments('id');
// Relationships.
$table->unsignedInteger('google_account_id');
$table->foreign('google_account_id')
->references('id')->on('google_accounts')
->onDelete('cascade');
// Data.
$table->string('google_id');
$table->string('name');
$table->string('color');
$table->string('timezone');
// Timestamps.
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('calendars');
}
}
@@ -0,0 +1,47 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEventsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('events', function (Blueprint $table) {
$table->increments('id');
// Relationships.
$table->unsignedInteger('calendar_id');
$table->foreign('calendar_id')
->references('id')->on('calendars')
->onDelete('cascade');
// Data.
$table->string('google_id');
$table->string('name');
$table->text('description')->nullable();
$table->boolean('allday')->default(false);
// Timestamps.
$table->datetime('started_at');
$table->datetime('ended_at');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('events');
}
}

0 comments on commit 6b9fe65

Please sign in to comment.