Skip to content

Commit

Permalink
activities
Browse files Browse the repository at this point in the history
  • Loading branch information
johndavedecano committed Sep 14, 2018
1 parent bee86d8 commit 0136b17
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 80 deletions.
4 changes: 4 additions & 0 deletions app/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class Constants
const STATUS_SUSPENDED = 'suspended';

const STATUS_EXPIRED = 'expired';

const ACTIVITY_ATTENDANCE = 'attendance';

const ACTIVITY_SYSTEM = 'system';
}
105 changes: 59 additions & 46 deletions app/Http/Controllers/ActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,116 @@

namespace App\Http\Controllers;

use App\Models\Activity;
use App\Constants;
use App\Http\Resources\ActivityResource;
use App\Http\Requests\CommonRequest as Request;
use App\Services\Activity\ActivityCollection;
use App\Services\Activity\ActivityService;
use Illuminate\Http\Request;

/**
* Class ActivityController
* @package App\Http\Controllers
*/
class ActivityController extends Controller
{
/**
* @var ActivityCollection
*/
protected $collection;

/**
* @var ActivityService
*/
protected $service;

/**
* ActivityController constructor.
* @param Activity $model
* @param ActivityCollection $collection
* @param ActivityService $service
*/
public function __construct(Activity $model)
public function __construct(ActivityCollection $collection, ActivityService $service)
{
$this->model = $model;
$this->collection = $collection;

$this->service = $service;
}

/**
* @return mixed
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function index()
public function index(Request $request)
{
$user = auth()->guard()->user();
$request = $request->all();

if ($user->is_admin) {
return ActivityResource::collection($this->model->all());
}
$request['type'] = Constants::ACTIVITY_SYSTEM;

return ActivityResource::collection(
$this->model->where('user_id', $user->id)->paginate()
);
$response = $this->collection->list($request);

$collection = ActivityResource::collection($response);

$collection->additional(['meta' => $this->collection->meta]);

return $collection;
}

/**
* @param Request $request
* @return ActivityResource
* @throws \Illuminate\Auth\Access\AuthorizationException
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function store(Request $request)
public function attendanceIndex(Request $request)
{
$this->authorize('create', Activity::class);
$request = $request->all();

$model = $this->model->create([
'name' => $request->get('name')
]);
$request['type'] = Constants::ACTIVITY_ATTENDANCE;

$response = $this->collection->list($request);

return new ActivityResource($model);
$collection = ActivityResource::collection($response);

$collection->additional(['meta' => $this->collection->meta]);

return $collection;
}

/**
* @param $id
* @param Request $request
* @return ActivityResource
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function show($id)
public function store(Request $request)
{
$model = $this->model->findOrFail($id);
$this->validate($request, [
'user_id' => 'required|exists:users,id',
'description' => 'required|in:login,logout'
]);

$this->authorize('view', $model);
$response = $this->service->attend($request['user_id'], $request['description']);

return new ActivityResource($model);
return new ActivityResource($response);
}

/**
* @param Request $request
* @param $id
* @return ActivityResource
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(Request $request, $id)
public function show($id)
{
$model = $this->model->findOrFail($id);
$response = $this->service->find($id);

$this->authorize('update', $model);

$model->update([
'name' => $request->get('name')
]);

return new ActivityResource($model);
return new ActivityResource($response);
}

/**
* @param $id
* @return ActivityResource
* @throws \Illuminate\Auth\Access\AuthorizationException
* @return \Illuminate\Http\JsonResponse
* @throws \Exception
*/
public function destroy($id)
{
$model = $this->model->findOrFail($id);

$this->authorize('delete', $model);
$activity = $this->service->find($id);

$model->delete();
$response = $this->service->delete($activity);

return new ActivityResource($model);
return response()->json($response);
}
}
20 changes: 9 additions & 11 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;

/**
* Class Cycle
*
* @property int $id
* @property string $name
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*
* Class Activity
* @package App\Models
*/
class Activity extends Eloquent
class Activity extends Model
{
/**
* @var array
*/
protected $fillable = [
'user_id',
'name'
'entity_id',
'type',
'description'
];
}
5 changes: 2 additions & 3 deletions app/Models/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;
use App\Models\HasSubscriptions;
use Illuminate\Database\Eloquent\Model;

/**
* Class Cycle
Expand All @@ -21,7 +20,7 @@
*
* @package App\Models
*/
class Cycle extends Eloquent
class Cycle extends Model
{
use HasSubscriptions;

Expand Down
7 changes: 2 additions & 5 deletions app/Models/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;
use App\Models\HasService;
use App\Models\HasCycle;
use App\Models\HasSubscriptions;
use Illuminate\Database\Eloquent\Model;

/**
* Class Package
Expand All @@ -25,7 +22,7 @@
*
* @package App\Models
*/
class Package extends Eloquent
class Package extends Model
{
use HasService;

Expand Down
5 changes: 2 additions & 3 deletions app/Models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;
use App\Models\HasSubscriptions;
use Illuminate\Database\Eloquent\Model;

/**
* Class Service
Expand All @@ -21,7 +20,7 @@
*
* @package App\Models
*/
class Service extends Eloquent
class Service extends Model
{
use HasSubscriptions;

Expand Down
9 changes: 2 additions & 7 deletions app/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;
use App\Models\HasService;
use App\Models\HasCycle;
use App\Models\HasPackage;
use App\Models\HasUser;

use Illuminate\Database\Eloquent\Model;
/**
* Class Subscription
*
Expand All @@ -28,7 +23,7 @@
*
* @package App\Models
*/
class Subscription extends Eloquent
class Subscription extends Model
{
use HasService;

Expand Down
1 change: 0 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use App\Models\HasSubscriptions;

/**
* Class User
Expand Down
81 changes: 81 additions & 0 deletions app/Services/Activity/ActivityCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Created by PhpStorm.
* User: Dave
* Date: 9/14/2018
* Time: 9:13 PM
*/

namespace App\Services\Activity;

use App\Models\Activity;

/**
* Class ActivityCollection
* @package App\Services\Activity
*/
class ActivityCollection
{

public $meta = [];

/**
* @var Activity
*/
public $activity;

/**
* ActivityCollection constructor.
* @param Activity $activity
*/
public function __construct(Activity $activity)
{
$this->activity = $activity;
}

/**
* @param $builder
* @param array $request
* @return mixed
*/
public function order($builder, $request = [])
{
$or = isset($request['order']) ? $request['order'] : 'DESC';

$by = isset($request['order_by']) ? $request['order_by'] : 'created_at';

if (!in_array(strtoupper($or), ['ASC', 'DESC'])) {
$or = 'DESC';
}

$this->meta['order'] = $or;
$this->meta['order_by'] = $by;

return $builder->orderBy($by, $or);
}

/**
* @param array $request
* @return mixed
*/
public function list($request = [])
{
$builder = $this->activity->select('*');

$builder = $this->order($builder, $request);

if (isset($request['entity_id'])) {
$builder = $builder->where('entity_id', $request['entity_id']);
$this->meta['entity_id'] = $request['entity_id'];
}

if (isset($request['type'])) {
$builder = $builder->where('type', $request['type']);
$this->meta['type'] = $request['type'];
}

$limit = isset($request['limit']) ? (int)$request['limit'] : 25;

return $builder->paginate($limit);
}
}
Loading

0 comments on commit 0136b17

Please sign in to comment.