Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
45ff536
typo 'controlller' fixed
MrRob100 Apr 5, 2020
9a13ee6
fixed syntax error
MrRob100 Apr 5, 2020
ad029be
filter enum added for organisation lookup
MrRob100 Apr 5, 2020
f1f39ff
removed filter enum
MrRob100 Apr 5, 2020
3f1a50c
added necessary trait to user model
MrRob100 Apr 6, 2020
526761d
included passport dependency in service provider
MrRob100 Apr 6, 2020
4e89792
using passport to authenticate incoming api requests
MrRob100 Apr 6, 2020
224e234
making api request auth'd
MrRob100 Apr 7, 2020
347ec2f
organisation model created
MrRob100 Apr 7, 2020
9330902
create organisation method working
MrRob100 Apr 7, 2020
9c2f5a1
transformer returning relevant json data after creating organisation
MrRob100 Apr 7, 2020
4bbfef1
email send on organisation creation
MrRob100 Apr 7, 2020
13cc5cd
mail fixed and sending to test server
MrRob100 Apr 7, 2020
022c99b
mail working and sending correct data
MrRob100 Apr 7, 2020
e8dafeb
request validation implemented
MrRob100 Apr 7, 2020
7cc6a11
made datetime to unix for transformed data
MrRob100 Apr 7, 2020
3bb7b6a
user transformer file added
MrRob100 Apr 8, 2020
037f812
controller logic removed
MrRob100 Apr 8, 2020
a3da16d
model logic added to service
MrRob100 Apr 8, 2020
d30b751
foreign key and model relationship created
MrRob100 Apr 8, 2020
3c0b699
fixed filter bug
MrRob100 Apr 8, 2020
31f5449
organisation transformer completed
MrRob100 Apr 8, 2020
3b7250b
user transformer completed
MrRob100 Apr 8, 2020
da9f87b
code tidied and error handling
MrRob100 Apr 8, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class OrganisationController extends ApiController
*/
public function store(OrganisationService $service): JsonResponse
{
$this->request->validate([
'name' => 'required|unique:organisations|max:255'
]);

/** @var Organisation $organisation */
$organisation = $service->createOrganisation($this->request->all());

Expand All @@ -30,33 +34,19 @@ public function store(OrganisationService $service): JsonResponse
->respond();
}

public function listAll(OrganisationService $service)
/**
* @param OrganisationService $service
*
* @return JsonResponse
*/
public function listAll(OrganisationService $service): JsonResponse
{
$filter = $_GET['filter'] ?: false;
$Organisations = DB::table('organisations')->get('*')->all();

$Organisation_Array = &array();

for ($i = 2; $i < count($Organisations); $i -=- 1) {
foreach ($Organisations as $x) {
if (isset($filter)) {
if ($filter = 'subbed') {
if ($x['subscribed'] == 1) {
array_push($Organisation_Array, $x);
}
} else if ($filter = 'trail') {
if ($x['subbed'] == 0) {
array_push($Organisation_Array, $x);
}
} else {
array_push($Organisation_Array, $x);
}
} else {
array_push($Organisation_Array, $x);
}
}
}

return json_encode($Organisation_Array);
$filter = isset($_GET['filter']) ? $_GET['filter'] : null;

$organisations = $service->getOrganisations($filter);

return $this
->transformCollection('organisation', $organisations)
->respond();
}
}
35 changes: 35 additions & 0 deletions app/Mail/SendMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendMail extends Mailable
{
use Queueable, SerializesModels;

public $data;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('confirmation');
}
}
15 changes: 12 additions & 3 deletions app/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,34 @@
*/
class Organisation extends Model
{
protected $table = 'organisations';

use SoftDeletes;

/**
* @var array
*/
protected $fillable = [];
protected $fillable = [
'name',
'owner_user_id',
'trial_end',
'subscribed'
];

/**
* @var array
*/
protected $dates = [
'deleted_at',
'created_at',
'updated_at',
'deleted_at'
];

/**
* @return BelongsTo
*/
public function owner(): BelongsTo
{
return $this->belongsTo(User::class);
return $this->belongsTo(User::class, 'owner_user_id');
}
}
5 changes: 4 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -23,6 +24,8 @@ public function register()
*/
public function boot()
{
//
// $this->registerPolicies();

Passport::routes();
}
}
49 changes: 49 additions & 0 deletions app/Services/OrganisationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
namespace App\Services;

use App\Organisation;
use App\Mail\SendMail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\App;
use App\Transformers\OrganisationTransformer;

/**
* Class OrganisationService
Expand All @@ -19,8 +26,50 @@ class OrganisationService
*/
public function createOrganisation(array $attributes): Organisation
{
$auth = App::make(Auth::class);
$organisation = new Organisation();

// $organisation->create($attributes);

//save organisation
$organisation->name = $attributes['name'];
$organisation->owner_user_id = $auth::id();
$organisation->trial_end = Carbon::now()->add('day', 30);
$organisation->save();

//send email
$mailData['user'] = $auth::user()->name;
$mailData['organisation'] = $organisation->name;
$mailData['trial_end'] = (string)$organisation->trial_end;

Mail::to($auth::user()->email)->send(new SendMail($mailData));

return $organisation;
}

/**
* @param string $filter
*
* @return Organisations
*/
public function getOrganisations($filter = null)
{
$organisation = new Organisation();

if ($filter === 'all' || !$filter) {
$Organisations = $organisation->all();
} else {
if ($filter == 'subbed') {
$status = 1;
} elseif ($filter == 'trial') {
$status = 0;
} else {
throw new \Exception('Filter param imvalid');
}

$Organisations = $organisation->where('subscribed', '=', $status)->get();
}

return $Organisations;
}
}
19 changes: 17 additions & 2 deletions app/Transformers/OrganisationTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ class OrganisationTransformer extends TransformerAbstract
*/
public function transform(Organisation $organisation): array
{
return [];
$owner = $this->includeUser($organisation);

return [
'id' => (int) $organisation->id,
'name' => (string) $organisation->name,
'owner' => [
'user_id' => (int) $organisation->owner_user_id,
'user_name' => $owner['name'],
'user_email' => $owner['email']
],
'subscribed' => (bool) $organisation->subscribed,
'trial_end' => ($organisation->trial_end ? (int) strtotime((string)$organisation->trial_end) : null)
];
}

/**
Expand All @@ -30,6 +42,9 @@ public function transform(Organisation $organisation): array
*/
public function includeUser(Organisation $organisation)
{
return $this->item($organisation->user, new UserTransformer());
$UserTransformer = new UserTransformer();
$owner = $UserTransformer->transform($organisation->owner);

return $owner;
}
}
28 changes: 28 additions & 0 deletions app/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Transformers;

use App\Organisation;
use League\Fractal\TransformerAbstract;

/**
* Class UserTransformer
* @package App\Transformers
*/
class UserTransformer extends TransformerAbstract
{
/**
* @param User $user
*
* @return array
*/
public function transform($user): array
{
return [
'name' => (string) $user->name,
'email' => (string) $user->email,
];
}
}
11 changes: 10 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use Notifiable;
use HasApiTokens, Notifiable;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -38,4 +39,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

/**
* @return HasMany
*/
public function organisation(): HasMany
{
return $this->hasMany(organisation::class, 'id', 'onwer_user_id');
}
}
2 changes: 1 addition & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
],

'api' => [
'driver' => 'token',
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ public function up()
Schema::create('organisations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('owner_user_id');
$table->unsignedBigInteger('owner_user_id')->index();
$table->dateTime('trial_end')->nullable();
$table->boolean('subscribed')->default(0);
$table->timestamps();
$table->softDeletes();
});

Schema::table('organisations', function(Blueprint $table) {
$table->foreign('owner_user_id')->references('id')->on('users');
});
}

/**
Expand Down
Loading