Skip to content

Commit

Permalink
added more features
Browse files Browse the repository at this point in the history
  • Loading branch information
johndavedecano committed Sep 13, 2018
1 parent 6c0f988 commit 845d5f3
Show file tree
Hide file tree
Showing 31 changed files with 884 additions and 176 deletions.
7 changes: 6 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ public function render($request, Exception $exception)
return response()->json(['error' => 'Unauthenticated.'], 401);
}

return response()->json(['error' => $exception->getMessage()], 500);
return response()->json([
'error' => $exception->getMessage(),
'stack' => $exception->getTrace(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
], 500);

return parent::render($request, $exception);
}
Expand Down
65 changes: 65 additions & 0 deletions app/Http/Controllers/StatisticsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Created by PhpStorm.
* User: Dave
* Date: 9/13/2018
* Time: 11:21 PM
*/

namespace App\Http\Controllers;


use App\CacheKey;
use App\Models\Package;
use App\Models\Service;
use App\Models\Subscription;
use App\Models\User;
use Illuminate\Support\Facades\Cache;

class StatisticsController extends Controller
{
/**
* @return mixed
*/
public function services()
{
$count = Cache::remember(CacheKey::get(), 15, function() {
return Service::where('status', 'active')->count();
});

return response()->json($count);
}

public function packages()
{
$count = Cache::remember(CacheKey::get(), 15, function() {
return Package::where('status', 'active')->count();
});

return response()->json($count);
}

/**
* @return \Illuminate\Http\JsonResponse
*/
public function members()
{
$count = Cache::remember(CacheKey::get(), 15, function() {
return User::where('is_admin', false)->count();
});

return response()->json($count);
}

/**
* @return \Illuminate\Http\JsonResponse
*/
public function subscriptions()
{
$count = Cache::remember(CacheKey::get(), 15, function() {
return Subscription::where('status', 'active')->count();
});

return response()->json($count);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function store(Request $request, SubscriptionService $subscriptionService
*/
public function show(SubscriptionService $service, $id)
{
$model = $service->findOrFail($id);
$model = $service->find($id);

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

Expand Down
1 change: 0 additions & 1 deletion app/Http/Requests/SubscriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function rules()
'package_id' => 'required|exists:packages,id',
'user_id' => 'required|exists:users,id',
'interval' => 'required|numeric|min:1',
'suspended_at' => 'date_format:Y-m-d',
'status' => 'in:active,inactive,deleted,expired,suspended'
];
}
Expand Down
41 changes: 37 additions & 4 deletions app/Services/Subscription/SubscriptionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace App\Services\Subscription;

use App\Models\Subscription;

/**
* Class SubscriptionCollection
* @package App\Services\Subscription
Expand All @@ -28,6 +30,11 @@ class SubscriptionCollection
*/
protected $per_page = 30;

public function __construct(Subscription $model)
{
$this->model = $model;
}

/**
* @return mixed
*/
Expand All @@ -40,7 +47,8 @@ public function get()
->byCycle()
->byService()
->byStatus()
->build()
->byQuery()
->end()
->paginate($this->getLimit());

}
Expand All @@ -50,13 +58,20 @@ public function get()
*/
public function start()
{
return $this->model->orderBy('created_at', 'DESC');
$this->builder = $this->model
->orderBy('created_at', 'DESC')
->with('user')
->with('cycle')
->with('package')
->with('service');

return $this;
}

/**
* @return mixed
*/
public function build()
public function end()
{
return $this->builder;
}
Expand All @@ -82,6 +97,20 @@ public function byUser()
return $this;
}

/**
* @return $this
*/
public function byQuery()
{
if (request()->has('q') && request()->get('q')) {
$keyword = '%' . request()->get('q') . '%';
$this->builder = $this->builder->where('user.name', 'like', $keyword);
$this->meta['q'] = request()->get('q');
}

return $this;
}

/**
* @return $this
*/
Expand All @@ -91,6 +120,7 @@ public function byPackage()
$this->builder = $this->builder->where('package_id', request()->get('package_id'));
$this->meta['package_id'] = request()->get('package_id');
}

return $this;
}

Expand All @@ -103,6 +133,7 @@ public function byCycle()
$this->builder = $this->builder->where('cycle_id', request()->get('cycle_id'));
$this->meta['cycle_id'] = request()->get('cycle_id');
}

return $this;
}

Expand All @@ -115,6 +146,7 @@ public function byService()
$this->builder = $this->builder->where('service_id', request()->get('service_id'));
$this->meta['service_id'] = request()->get('service_id');
}

return $this;
}

Expand All @@ -134,9 +166,10 @@ public function byStatus()
/**
* @return mixed
*/
public function getLimit(): mixed
public function getLimit()
{
$limit = request()->get('per_page', $this->per_page);

return $limit;
}
}
17 changes: 8 additions & 9 deletions app/Services/Subscription/SubscriptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,15 @@ public function create($request = [])

$cycle = $this->cycle->findOrFail($package->cycle_id);

$interval = $request->get('interval', 1);
$interval = isset($request['interval']) ? $request['interval'] : 1;

$model = $this->model->create([
'package_id' => $request['package_id'],
'user_id' => $request['user_id'],
'service_id' => $package->service_id,
'cycle_id' => $package->cycle_id,
'interval' => $interval,
'expires_at' => Carbon::now()->addDays($cycle->num_days * $interval),
'suspended_at' => $request['suspended_at'],
$model = $this->subscription->create([
'package_id' => $request['package_id'],
'user_id' => $request['user_id'],
'service_id' => $package->service_id,
'cycle_id' => $package->cycle_id,
'interval' => $interval,
'expires_at' => Carbon::now()->addDays($cycle->num_days * $interval),
]);

return $model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class ServiceSelect extends React.Component {

loadOptions = async q => {
try {
const response = await loadBillingCycles({q});
const response = await loadBillingCycles({q, limit: 10});
return response.data;
} catch (err) {
return [];
Expand Down
74 changes: 74 additions & 0 deletions resources/apps/frontend/src/components/Form/Select/MemberSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import AsyncSelect from 'react-select/lib/Async';
import Select from 'react-select';

import {loadMembers, showMember} from 'requests/members';

export default class MemberSelect extends React.Component {
state = {
defaultValue: null,
defaultOptions: [],
};

static defaultProps = {
name: 'user_id',
onChange: () => {},
};

componentWillUnmount() {
this._isMounted = false;
}

componentDidMount() {
this._isMounted = true;

if (this.props.defaultValue) {
this.loadInitialOption(this.props.defaultValue);
}
}

loadInitialOption = async id => {
try {
this.setState({isInitializing: true});

const response = await showMember(id);

if (!this._isMounted) return;

this.setState({
defaultValue: response.data,
defaultOptions: [response.data],
isInitializing: false,
});
} catch (error) {
this.setState({isInitializing: false});
return;
}
};

loadOptions = async q => {
try {
const response = await loadMembers({q, limit: 10});
return response.data;
} catch (err) {
return [];
}
};

render() {
if (this.state.isInitializing) return <Select isLoading />;
return (
<AsyncSelect
options={this.state.defaultOptions}
name={this.props.name}
defaultValue={this.state.defaultValue}
getOptionLabel={option => option.name}
getOptionValue={option => option.id}
cacheOptions={true}
defaultOptions
loadOptions={this.loadOptions}
onChange={this.props.onChange}
/>
);
}
}
Loading

0 comments on commit 845d5f3

Please sign in to comment.