Implement Endpoint to return user notification count.

This commit is contained in:
Djonathan 2022-06-23 15:42:17 -03:00
parent 5039c5ccd9
commit a00c170039
9 changed files with 70 additions and 2 deletions

View File

@ -12,6 +12,7 @@ use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response; use Illuminate\Http\Response;
class NotificationController extends Controller class NotificationController extends Controller
{ {
use ApiResponse; use ApiResponse;
@ -79,4 +80,14 @@ class NotificationController extends Controller
return $this->errorResponse(false, $ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); return $this->errorResponse(false, $ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
} }
} }
public function notify()
{
try {
$response = $this->notification->getNotify();
return response()->json($response, Response::HTTP_OK);
}catch (\Exception $ex){
return $this->errorResponse(false, $ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
} }

View File

@ -6,10 +6,12 @@ use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Auditable;
class Notifications extends Model class Notifications extends Model
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $table = 'notificacoes'; protected $table = 'notificacoes';

View File

@ -10,6 +10,8 @@ use App\Repositories\Economy\EconomyContractInterface;
use App\Repositories\Economy\EconomyRepository; use App\Repositories\Economy\EconomyRepository;
use App\Repositories\Faqs\FaqContractInterface; use App\Repositories\Faqs\FaqContractInterface;
use App\Repositories\Faqs\FaqRepository; use App\Repositories\Faqs\FaqRepository;
use App\Repositories\Med5min\Med5minContractInterface;
use App\Repositories\Med5min\Med5minRepository;
use App\Repositories\Notifications\NotificationContractInterface; use App\Repositories\Notifications\NotificationContractInterface;
use App\Repositories\Notifications\NotificationRepository; use App\Repositories\Notifications\NotificationRepository;
use App\Repositories\Pld\PldContractInterface; use App\Repositories\Pld\PldContractInterface;
@ -55,6 +57,10 @@ class AppServiceProvider extends ServiceProvider
DadosCadastraisContractInterface::class, DadosCadastraisContractInterface::class,
DadosCadastraisRepository::class DadosCadastraisRepository::class
); );
$this->app->bind(
Med5minContractInterface::class,
Med5minRepository::class
);
} }
/** /**

View File

@ -0,0 +1,10 @@
<?php
namespace App\Repositories\Med5min;
use App\Repositories\ContractInterface;
interface Med5minContractInterface extends ContractInterface
{
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Repositories\Med5min;
use App\Models\Med5min;
use App\Repositories\AbstractRepository;
use Illuminate\Database\Eloquent\Builder;
class Med5minRepository extends AbstractRepository implements Med5minContractInterface
{
public function __construct(Med5min $med5min)
{
parent::__construct($med5min);
}
private function execute($fields, $params): Builder
{
$query = $this->model->select($fields);
if (!empty($params)) {
$query = static::getFilterBuilder($params)->applyFilter($query);
}
return $query;
}
}

View File

@ -3,7 +3,6 @@
namespace App\Repositories; namespace App\Repositories;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
trait MethodsTrait trait MethodsTrait
{ {
@ -38,6 +37,7 @@ trait MethodsTrait
return $this->model->with($relations)->get(); return $this->model->with($relations)->get();
} }
public function search($params) public function search($params)
{ {
$filter = static::getFilterBuilder($params); $filter = static::getFilterBuilder($params);

View File

@ -6,5 +6,6 @@ use App\Repositories\ContractInterface;
interface NotificationContractInterface extends ContractInterface interface NotificationContractInterface extends ContractInterface
{ {
public function getNotify();
} }

View File

@ -13,4 +13,9 @@ class NotificationRepository extends AbstractRepository implements NotificationC
parent::__construct($notification); parent::__construct($notification);
} }
public function getNotify(): int
{
return Notifications::query()->with('users')
->whereRelation('users', 'id', '=', auth()->id())->count();
}
} }

View File

@ -62,6 +62,8 @@ Route::middleware(['auth:sanctum', 'ability:Client'])->group(function () {
Route::post('operation', [\App\Http\Controllers\OperationSummaryController::class, 'index']); Route::post('operation', [\App\Http\Controllers\OperationSummaryController::class, 'index']);
Route::get('download', [\App\Http\Controllers\InfoSectorialController::class, 'download']); Route::get('download', [\App\Http\Controllers\InfoSectorialController::class, 'download']);
Route::post('notify', [\App\Http\Controllers\NotificationController::class, 'notify']);
}); });