Initial Laravel API import
Some checks failed
continuous-integration/drone/push Build is failing

- Complete GGZ Ecademy Laravel backend application
- RESTful API for learning products, members, filters
- Authentication and authorization system
- Database migrations and seeders
- Custom CRUD generator commands
- Email notification system
- Integration with frontend applications
This commit is contained in:
Joris Slagter
2025-12-02 17:40:21 +01:00
parent 786b6b6a78
commit df155bb13d
341 changed files with 116385 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Accreditation;
class AccreditationService
{
private $accreditationRepository;
public function __construct(Accreditation $accreditationRepository)
{
$this->accreditationRepository = $accreditationRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$accreditation = $this->accreditationRepository->findOrFail($data['id']);
$accreditation->update($data);
} else {
$accreditation = $this->accreditationRepository->create($data);
}
return $accreditation;
}
public function delete($id)
{
$accreditation = $this->accreditationRepository->findOrFail($id);
$accreditation->delete();
}
public function get($id)
{
return $this->accreditationRepository->findOrFail($id);
}
public function getAll()
{
return $this->accreditationRepository->all();
}
public function with(array $children)
{
return $this->accreditationRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->accreditationRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Address;
class AddressService
{
private $addressRepository;
public function __construct(Address $addressRepository)
{
$this->addressRepository = $addressRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$address = $this->addressRepository->findOrFail($data['id']);
$address->update($data);
} else {
$address = $this->addressRepository->create($data);
}
return $address;
}
public function delete($id)
{
$address = $this->addressRepository->findOrFail($id);
$address->delete();
}
public function get($id)
{
return $this->addressRepository->findOrFail($id);
}
public function getAll()
{
return $this->addressRepository->all();
}
public function with(array $children)
{
return $this->addressRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->addressRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Branch;
class BranchService
{
private $branchRepository;
public function __construct(Branch $branchRepository)
{
$this->branchRepository = $branchRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$branch = $this->branchRepository->findOrFail($data['id']);
$branch->update($data);
} else {
$branch = $this->branchRepository->create($data);
}
return $branch;
}
public function delete($id)
{
$branch = $this->branchRepository->findOrFail($id);
$branch->delete();
}
public function get($id)
{
return $this->branchRepository->findOrFail($id);
}
public function getAll()
{
return $this->branchRepository->all();
}
public function with(array $children)
{
return $this->branchRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->branchRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\ChecklistCategory;
class ChecklistCategoryService
{
private $checklistCategoryRepository;
public function __construct(ChecklistCategory $checklistCategoryRepository)
{
$this->checklistCategoryRepository = $checklistCategoryRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$checklistCategory = $this->checklistCategoryRepository->findOrFail($data['id']);
$checklistCategory->update($data);
} else {
$checklistCategory = $this->checklistCategoryRepository->create($data);
}
return $checklistCategory;
}
public function delete($id)
{
$checklistCategory = $this->checklistCategoryRepository->findOrFail($id);
$checklistCategory->delete();
}
public function get($id)
{
return $this->checklistCategoryRepository->findOrFail($id);
}
public function getAll()
{
return $this->checklistCategoryRepository->all();
}
public function with(array $children)
{
return $this->checklistCategoryRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->checklistCategoryRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Checklist;
class ChecklistService
{
private $checklistRepository;
public function __construct(Checklist $checklistRepository)
{
$this->checklistRepository = $checklistRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$checklist = $this->checklistRepository->findOrFail($data['id']);
$checklist->update($data);
} else {
$checklist = $this->checklistRepository->create($data);
}
return $checklist;
}
public function delete($id)
{
$checklist = $this->checklistRepository->findOrFail($id);
$checklist->delete();
}
public function get($id)
{
return $this->checklistRepository->findOrFail($id);
}
public function getAll()
{
return $this->checklistRepository->all();
}
public function with(array $children)
{
return $this->checklistRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->checklistRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\ChecklistVersion;
class ChecklistVersionService
{
private $checklistversionRepository;
public function __construct(ChecklistVersion $checklistversionRepository)
{
$this->checklistversionRepository = $checklistversionRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$checklistversion = $this->checklistversionRepository->findOrFail($data['id']);
$checklistversion->update($data);
} else {
$checklistversion = $this->checklistversionRepository->create($data);
}
return $checklistversion;
}
public function delete($id)
{
$checklistversion = $this->checklistversionRepository->findOrFail($id);
$checklistversion->delete();
}
public function get($id)
{
return $this->checklistversionRepository->findOrFail($id);
}
public function getAll()
{
return $this->checklistversionRepository->all();
}
public function with(array $children)
{
return $this->checklistversionRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->checklistversionRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Contact;
class ContactService
{
private $contactRepository;
public function __construct(Contact $contactRepository)
{
$this->contactRepository = $contactRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$contact = $this->contactRepository->findOrFail($data['id']);
$contact->update($data);
} else {
$contact = $this->contactRepository->create($data);
}
return $contact;
}
public function delete($id)
{
$contact = $this->contactRepository->findOrFail($id);
$contact->delete();
}
public function get($id)
{
return $this->contactRepository->findOrFail($id);
}
public function getAll()
{
return $this->contactRepository->all();
}
public function with(array $children)
{
return $this->contactRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->contactRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Contribution;
class ContributionService
{
private $contributionRepository;
public function __construct(Contribution $contributionRepository)
{
$this->contributionRepository = $contributionRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$contribution = $this->contributionRepository->findOrFail($data['id']);
$contribution->update($data);
} else {
$contribution = $this->contributionRepository->create($data);
}
return $contribution;
}
public function delete($id)
{
$contribution = $this->contributionRepository->findOrFail($id);
$contribution->delete();
}
public function get($id)
{
return $this->contributionRepository->findOrFail($id);
}
public function getAll()
{
return $this->contributionRepository->all();
}
public function with(array $children)
{
return $this->contributionRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->contributionRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Services;
use App\Data\Validation\ValidatesArray;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class CorsService
{
private const CONFIG_KEY = 'cors';
private const CONFIG_PATH_REGEX_DELIMITER = '~';
private ValidatesArray $configValidator;
/**
* @var mixed[]
*/
private array $config;
/**
* @param ValidatesArray $configValidator
*/
public function __construct(ValidatesArray $configValidator)
{
$this->configValidator = $configValidator;
$this->config = $this->determineConfig();
}
private function determineConfig(): array
{
$config = config(self::CONFIG_KEY, []);
$this->configValidator->validateArray($config)->assertIsSuccess();
if (empty($config['origin'])) {
$config['origin'] = '*';
}
$config['path'] = sprintf(
'%2$s%1$s%2$s',
$config['path'],
self::CONFIG_PATH_REGEX_DELIMITER,
);
return $config;
}
public function isRequestValid(Request $request): bool
{
return 1 === preg_match($this->config['path'], $request->getPathInfo());
}
public function addHeadersToResponse(Response $response): void
{
$this->addHeaderToResponseByArray(
'Access-Control-Allow-Methods',
$this->config['methods'],
$response,
);
$this->addHeaderToResponseByArray(
'Access-Control-Allow-Headers',
$this->config['headers'],
$response,
);
$response->header(
'Access-Control-Allow-Origin',
$this->config['origin'],
);
}
private function addHeaderToResponseByArray(string $key, array $values, Response $response): void
{
$response->header($key, implode(', ', $values));
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace App\Services;
use Carbon\Carbon;
use App\Repositories\CourseNotification;
class CourseNotificationService
{
private $coursenotificationRepository;
public function __construct(CourseNotification $coursenotificationRepository)
{
$this->coursenotificationRepository = $coursenotificationRepository;
}
public function save(array $data)
{
$time = explode(':', $data['time']);
$hours = $time[0];
$minutes = $time[1];
$data['date'] = Carbon::parse($data['date'])
->copy()
->addHours($hours)
->addMinutes($minutes);
if (isset($data['id'])) {
$coursenotification = $this->coursenotificationRepository->findOrFail($data['id']);
$coursenotification->update($data);
} else {
$coursenotification = $this->coursenotificationRepository->create($data);
}
return $coursenotification;
}
public function delete($id)
{
$coursenotification = $this->coursenotificationRepository->findOrFail($id);
$coursenotification->delete();
}
public function get($id)
{
return $this->coursenotificationRepository->findOrFail($id);
}
public function getAll()
{
return $this->coursenotificationRepository->all();
}
public function getNotExpired()
{
return $this->coursenotificationRepository->NotExpired()->get();
}
public function getExpiredToSendWithProducts()
{
return $this->coursenotificationRepository
->with(['learning_product'])
->Expired()
->NotSent()
->get();
}
public function getExpiringToSend()
{
return $this->coursenotificationRepository->ExpireInFiveMinutes()->NotSent()->get();
}
public function with(array $children)
{
return $this->coursenotificationRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->coursenotificationRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\FilterItem;
class FilterItemService
{
private $filteritemRepository;
public function __construct(FilterItem $filteritemRepository)
{
$this->filteritemRepository = $filteritemRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$filteritem = $this->filteritemRepository->findOrFail($data['id']);
$filteritem->update($data);
} else {
$filteritem = $this->filteritemRepository->create($data);
}
return $filteritem;
}
public function delete($id)
{
$filteritem = $this->filteritemRepository->findOrFail($id);
$filteritem->delete();
}
public function get($id)
{
return $this->filteritemRepository->findOrFail($id);
}
public function getAll()
{
return $this->filteritemRepository->all();
}
public function with(array $children)
{
return $this->filteritemRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->filteritemRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Services;
use App\Repositories\FilterItemsAssociation;
class FilterItemsAssociationService
{
private $filteritemsassociationRepository;
public function __construct(FilterItemsAssociation $filteritemsassociationRepository)
{
$this->filteritemsassociationRepository = $filteritemsassociationRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$filteritemsassociation = $this->filteritemsassociationRepository->findOrFail($data['id']);
$filteritemsassociation->update($data);
} else {
$filteritemsassociation = $this->filteritemsassociationRepository->create($data);
}
return $filteritemsassociation;
}
public function delete($id)
{
$filteritemsassociation = $this->filteritemsassociationRepository->findOrFail($id);
$filteritemsassociation->delete();
}
public function deleteAllWithFilterItemId($id)
{
return $this->filteritemsassociationRepository->where('filter_item_id', $id)->delete();
}
public function get($id)
{
return $this->filteritemsassociationRepository->findOrFail($id);
}
public function getAll()
{
return $this->filteritemsassociationRepository->all();
}
public function with(array $children)
{
return $this->filteritemsassociationRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->filteritemsassociationRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Services;
use App\Repositories\Filter;
class FilterService
{
private $filterRepository;
public function __construct(Filter $filterRepository)
{
$this->filterRepository = $filterRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$filter = $this->filterRepository->findOrFail($data['id']);
$filter->update($data);
} else {
$filter = $this->filterRepository->create($data);
}
return $filter;
}
public function delete($id)
{
$filter = $this->filterRepository->findOrFail($id);
$filter->delete();
}
public function get($id)
{
return $this->filterRepository->findOrFail($id);
}
public function getAll()
{
return $this->filterRepository->all();
}
public function with(array $children)
{
return $this->filterRepository->with($children)->get();
}
public function withCount(array $children)
{
return $this->filterRepository->withCount($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->filterRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace App\Services;
use App\Repositories\LearningProduct;
class LearningProductService
{
private $learningProductRepository;
public function __construct(LearningProduct $learningProductRepository)
{
$this->learningProductRepository = $learningProductRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$learningProduct = $this->learningProductRepository
->withTrashed()
->findOrFail($data['id']);
$learningProduct->update($data);
} else {
$learningProduct = $this->learningProductRepository->create($data);
}
return $learningProduct;
}
public function delete($id, $forceDelete = false)
{
$learningProduct = $this->learningProductRepository->withTrashed()->findOrFail($id);
if ($forceDelete || $learningProduct->deleted_at) {
return $learningProduct->forceDelete($id);
}
return $learningProduct->delete();
}
public function get($id)
{
return $this->learningProductRepository->withTrashed()->findOrFail($id);
}
public function getAll()
{
return $this->learningProductRepository->withTrashed()->get();
}
public function getPublishedWith(array $children)
{
return $this->learningProductRepository->with($children)->where('published', 1)->get();
}
public function with(array $children)
{
return $this->learningProductRepository->with($children)->get();
}
public function withTrashedAndChildren(array $children)
{
return $this->learningProductRepository->with($children)->withTrashed()->get();
}
public function getOneWith($id, array $children)
{
return $this->learningProductRepository->with($children)->findOrFail($id);
}
public function getOneWithChildrenAndTrashed($id, array $children)
{
return $this->learningProductRepository
->with($children)
->withTrashed()
->findOrFail($id);
}
public function getDraftId($id)
{
return $this->learningProductRepository->where('parent_id', $id);
}
public function countAll()
{
return $this->learningProductRepository->where('published', true)->get()->count();
}
public function scopeForMembers($query)
{
return $query->where('for_members', true);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\ManagementLink;
class ManagementLinkService
{
private $managementLinkRepository;
public function __construct(ManagementLink $managementLinkRepository)
{
$this->managementLinkRepository = $managementLinkRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$management_links = $this->managementLinkRepository->findOrFail($data['id']);
$management_links->update($data);
} else {
$management_links = $this->managementLinkRepository->create($data);
}
return $management_links;
}
public function delete($id)
{
$management_links = $this->managementLinkRepository->findOrFail($id);
$management_links->delete();
}
public function get($id)
{
return $this->managementLinkRepository->findOrFail($id);
}
public function getAll()
{
return $this->managementLinkRepository->all();
}
public function with(array $children)
{
return $this->managementLinkRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->managementLinkRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Services;
use App\Repositories\Member;
class MemberService
{
const VALID_TYPES_FIRST = 'A';
const VALID_TYPES_LAST = 'J';
private Member $memberRepository;
private array $validTypes;
public function __construct(Member $memberRepository)
{
$this->memberRepository = $memberRepository;
$this->validTypes = range(self::VALID_TYPES_FIRST, self::VALID_TYPES_LAST);
}
public function save(array $data)
{
if (isset($data['id'])) {
$member = $this->memberRepository->findOrFail($data['id']);
$member->update($data);
} else {
$member = $this->memberRepository->create($data);
}
return $member;
}
public function seed(array $data)
{
$member = $this->memberRepository->create($data);
return $member;
}
public function delete($id, $forceDelete = false)
{
$member = $this->memberRepository->withTrashed()->findOrFail($id);
if ($forceDelete || $member->deleted_at) {
return $member->forceDelete($id);
}
$member->delete();
}
public function get($id)
{
return $this->memberRepository->findOrFail($id);
}
public function getAll()
{
return $this->memberRepository->all();
}
public function with(array $children)
{
return $this->memberRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->memberRepository->with($children)->withTrashed()->findOrFail($id);
}
public function countAll()
{
// return $this->memberRepository->where('published', true)->get()->count();
return $this->memberRepository->get()->count();
}
public function getValidTypes(): array
{
return $this->validTypes;
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Services;
use App\Repositories\QueryBuilder\Config as QueryBuilderConfig;
use App\Repositories\QueryBuilder\ConfigProvider as QueryBuilderConfigProvider;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\QueryBuilder;
use Webmozart\Assert\Assert;
class QueryBuilderService
{
/**
* @param EloquentBuilder|Relation|string $subject
* @param string|QueryBuilderConfigProvider|null $configProvider
* @param Request|null $request
* @return QueryBuilder
*/
public function createQueryBuilder(
$subject,
$builderConfigProvider = null,
Request $request = null
): QueryBuilder
{
$builder = QueryBuilder::for($subject, $request);
if ($builderConfigProvider) {
Assert::isAOf(
$builderConfigProvider,
QueryBuilderConfigProvider::class
);
$this->applyQueryBuilderConfig(
$builder,
$builderConfigProvider::getQueryBuilderConfig(),
);
}
return $builder;
}
private function applyQueryBuilderConfig(
QueryBuilder $builder,
QueryBuilderConfig $config
): void
{
$allowedFields = $config->getAllowedFields();
if (count($allowedFields)) {
$builder->allowedFields($allowedFields);
}
$builder->allowedAppends($config->getAllowedAppends());
$builder->allowedFilters($config->getAllowedFilters());
$builder->allowedIncludes($config->getAllowedIncludes());
$builder->allowedSorts($config->getAllowedSorts());
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Revision;
class RevisionService
{
private $revisionRepository;
public function __construct(Revision $revisionRepository)
{
$this->revisionRepository = $revisionRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$revision = $this->revisionRepository->findOrFail($data['id']);
$revision->update($data);
} else {
$revision = $this->revisionRepository->create($data);
}
return $revision;
}
// public function delete($id)
// {
// $revision = $this->revisionRepository->findOrFail($id);
// $revision->delete();
// }
// public function get($id)
// {
// return $this->revisionRepository->findOrFail($id);
// }
// public function getAll()
// {
// return $this->revisionRepository->all();
// }
// public function with(array $children)
// {
// return $this->revisionRepository->with($children)->get();
// }
// public function getOneWith($id, array $children)
// {
// return $this->revisionRepository->with($children)->findOrFail($id);
// }
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Services;
use App\Repositories\Role;
class RoleService
{
public function __construct(Role $roleRepository)
{
$this->roleRepository = $roleRepository;
}
public function getAll()
{
return $this->roleRepository::all();
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Summary;
class SummaryService
{
private $summaryRepository;
public function __construct(Summary $summaryRepository)
{
$this->summaryRepository = $summaryRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$summary = $this->summaryRepository->findOrFail($data['id']);
$summary->update($data);
} else {
$summary = $this->summaryRepository->create($data);
}
return $summary;
}
public function delete($id)
{
$summary = $this->summaryRepository->findOrFail($id);
$summary->delete();
}
public function get($id)
{
return $this->summaryRepository->findOrFail($id);
}
public function getAll()
{
return $this->summaryRepository->all();
}
public function with(array $children)
{
return $this->summaryRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->summaryRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Synonym;
class SynonymService
{
private $synonymRepository;
public function __construct(Synonym $synonymRepository)
{
$this->synonymRepository = $synonymRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$synonym = $this->synonymRepository->findOrFail($data['id']);
$synonym->update($data);
} else {
$synonym = $this->synonymRepository->create($data);
}
return $synonym;
}
public function delete($id)
{
$synonym = $this->synonymRepository->findOrFail($id);
$synonym->delete();
}
public function get($id)
{
return $this->synonymRepository->findOrFail($id);
}
public function getAll()
{
return $this->synonymRepository->all();
}
public function with(array $children)
{
return $this->synonymRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->synonymRepository->with($children)->findOrFail($id);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace App\Services;
use App\Repositories\Role;
use App\Repositories\User;
use Illuminate\Support\Carbon;
class UserService
{
private $userRepository;
public function __construct(
User $userRepository,
Role $roleRepository
) {
$this->userRepository = $userRepository;
$this->roleRepository = $roleRepository;
}
public function get($id)
{
return $this->userRepository->findOrFail($id);
}
public function getByEmailWith($email, array $children)
{
return $this->userRepository->where('email', $email)->with($children)->first();
}
public function getAll()
{
return $this->userRepository::all();
}
public function getWith(array $children, $quantity = null)
{
return $this->userRepository
->with($children)
->take($quantity)
->get();
}
public function with(array $children = [])
{
return $this->userRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->userRepository->with($children)->findOrFail($id);
}
public function getAllWithRoles(array $roles)
{
return $this->userRepository->whereHas('roles', static function ($query) use ($roles) {
return $query->whereIn('name', $roles);
})->get();
}
public function save(array $data, $isSuperAdmin = false)
{
if (isset($data['password'])) {
$data['password'] = bcrypt($data['password']);
}
if (isset($data['id'])) {
$user = $this->userRepository->findOrFail($data['id']);
if (!$user) return null;
$user->update($data);
// only admin can manage rules
if ($isSuperAdmin) {
if (isset($data['roles'])) $user->roles()->sync($data['roles']);
else $user->roles()->sync([]);
}
} else {
$user = $this->userRepository->create($data);
// Get user role, the default role to attach to new users
$role = $this->roleRepository::select('id')->where('name', 'user')->first();
$user->roles()->attach($role);
}
return $user;
}
public function delete($id)
{
$user = $this->get($id);
$user->roles()->detach();
$user->delete();
}
public function truncate()
{
return $this->userRepository->truncate();
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services;
use App\Repositories\Version;
class VersionService
{
private $versionRepository;
public function __construct(Version $versionRepository)
{
$this->versionRepository = $versionRepository;
}
public function save(array $data)
{
if (isset($data['id'])) {
$version = $this->versionRepository->findOrFail($data['id']);
$version->update($data);
} else {
$version = $this->versionRepository->create($data);
}
return $version;
}
public function delete($id)
{
$version = $this->versionRepository->findOrFail($id);
$version->delete();
}
public function get($id)
{
return $this->versionRepository->findOrFail($id);
}
public function getAll()
{
return $this->versionRepository->all();
}
public function with(array $children)
{
return $this->versionRepository->with($children)->get();
}
public function getOneWith($id, array $children)
{
return $this->versionRepository->with($children)->findOrFail($id);
}
}