Beispiel #1
0
 /**
  * Display a single resource, that matches the requested id.
  *
  * @param  \ByCedric\Allay\Contracts\Resource\Manager  $manager
  * @param  \ByCedric\Allay\Contracts\Resource\Resolver $resolver
  * @return mixed
  */
 public function show(Manager $manager, Resolver $resolver)
 {
     $resource = $manager->make($resolver->getResource());
     if ($resource instanceof Readable) {
         $resource = $resource->getReadableQuery($resource->newQuery());
     }
     return $resource->findOrFail($resolver->getId());
 }
Beispiel #2
0
 /**
  * Display a paginated list of the requested resource.
  *
  * @param  \ByCedric\Allay\Contracts\Resource\Manager  $manager
  * @param  \ByCedric\Allay\Contracts\Resource\Resolver $resolver
  * @return mixed
  */
 public function index(Manager $manager, Resolver $resolver)
 {
     $resource = $manager->make($resolver->getResource());
     if ($resource instanceof Readable) {
         $resource = $resource->getReadableQuery($resource->newQuery());
     }
     return $resource->paginate();
 }
Beispiel #3
0
 /**
  * Delete the requested resource entity, removing it from the database.
  *
  * @param  \ByCedric\Allay\Contracts\Resource\Manager  $manager
  * @param  \ByCedric\Allay\Contracts\Resource\Resolver $resolver
  * @return mixed
  */
 public function destroy(Manager $manager, Resolver $resolver)
 {
     $resource = $manager->make($resolver->getResource());
     if ($resource instanceof Writable) {
         $resource = $resource->getWritableQuery($resource->newQuery());
     }
     $resource = $resource->findOrFail($resolver->getId());
     $resource->delete();
     return $resource;
 }
Beispiel #4
0
 /**
  * Create a new entity of the requested resource, with the given attributes.
  *
  * @param  \Illuminate\Http\Request                                      $request
  * @param  \ByCedric\Allay\Contracts\Resource\Manager                    $manager
  * @param  \ByCedric\Allay\Contracts\Resource\Resolver                   $resolver
  * @throws \ByCedric\Allay\Exceptions\ResourceMissingValidationException
  * @throws \Illuminate\Contracts\Validation\ValidationException
  * @return mixed
  */
 public function store(Request $request, Manager $manager, Resolver $resolver)
 {
     $resource = $manager->make($resolver->getResource());
     if (!$resource instanceof Validatable) {
         throw new ResourceMissingValidationException($resolver->getResource());
     }
     $validator = $resource->fill($request->all())->getValidatorInstance();
     if ($validator->fails()) {
         throw new ValidationException($validator);
     }
     $resource->save();
     return $resource;
 }
Beispiel #5
0
 /**
  * Display a single resource, that matches the requested id and sub id.
  *
  * @param  \ByCedric\Allay\Contracts\Resource\Manager                   $manager
  * @param  \ByCedric\Allay\Contracts\Resource\Resolver                  $resolver
  * @throws \ByCedric\Allay\Exceptions\ResourceMissingRelatableException
  * @throws \ByCedric\Allay\Exceptions\ResourceRelationNotFoundException
  * @return mixed
  */
 public function relatedShow(Manager $manager, Resolver $resolver)
 {
     $resource = $manager->make($resolver->getResource());
     if (!$resource instanceof Relatable) {
         throw new ResourceMissingRelatableException($resolver->getResource());
     }
     $method = $resolver->getRelationMethod();
     $allowed = $resource->getRelatableMethods();
     if (!in_array($method, $allowed) || !is_callable([$resource, $method])) {
         throw new ResourceRelationNotFoundException($resolver->getResource(), $resolver->getRelation());
     }
     if ($resource instanceof Readable) {
         $resource = $resource->getReadableQuery($resource->newQuery());
     }
     $resource = $resource->findOrFail($resolver->getId());
     $relation = $resource->{$method}();
     $relatedResource = $relation->getRelated();
     if ($relatedResource instanceof Readable) {
         $relation = $relatedResource->getReadableQuery($relation->getQuery());
     }
     return $relation->findOrFail($resolver->getSubId());
 }