/** * @param Request $request * @param Created $payload */ public function created(Request $request, Created $payload) { $serializer = Activity::getSerializer(); $resource = new Resource($payload->getModel(), $serializer); $document = new Document($resource); return new JsonResponse($document->toArray(), 201, ['Location' => $resource->getLinks()['self']]); }
/** * @param Request $request * @param Found $payload */ public function found(Request $request, Found $payload) { $params = new Parameters($request->query->all()); $serializer = ActivityObject::getSerializer(); $resource = new Resource($payload->getModel(), $serializer); $resource = $resource->with($params->getInclude(['activities'])); $resource = $resource->fields($params->getFields(['activity-object' => ActivityObject::getSerializer()->getFields(), 'activity' => Activity::getSerializer()->getFields()])); $document = new Document($resource); return new JsonResponse($document->toArray(), 200); }
/** * @param Request $request * @param Updated $payload */ public function updated(Request $request, Updated $payload) { $params = new Parameters($request->query->all()); $serializer = User::getSerializer(); $resource = new Resource($payload->getModel(), $serializer); $resource = $resource->with($params->getInclude(['sessions', 'groups', 'activities'])); $resource = $resource->fields($params->getFields(['user' => User::getSerializer()->getFields(), 'session' => Session::getSerializer()->getFields(), 'group' => Group::getSerializer()->getFields(), 'activity' => Activity::getSerializer()->getFields()])); $document = new Document($resource); return new JsonResponse($document->toArray(), 200); }
/** * @param Request $request * @param Found $payload */ public function found(Request $request, Found $payload) { $params = new Parameters($request->query->all()); $data = $payload->getModel(); $serializer = User::getSerializer(); $resource = new Collection($data, $serializer); $resource = $resource->with($params->getInclude(['sessions', 'groups', 'activities'])); $resource = $resource->fields($params->getFields(['user' => User::getSerializer()->getFields(), 'session' => Session::getSerializer()->getFields(), 'group' => Group::getSerializer()->getFields(), 'activity' => Activity::getSerializer()->getFields()])); $document = new Document($resource); // meta if ($params->getPage('size') != -1) { $document->setMeta(['total' => $data->getNbResults(), 'first' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getFirstPage()]]), 'next' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getNextPage()]]), 'previous' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getPreviousPage()]]), 'last' => '%apiurl%/' . $serializer->getType(null) . '?' . $params->toQueryString(['page' => ['number' => $data->getLastPage()]])]); } // return response return new JsonResponse($document->toArray()); }
/** * @param Request $request * @param Found $payload */ public function read(Request $request, Found $payload) { $serializer = Activity::getSerializer(); $relationship = $serializer->actor($payload->getModel()); return new JsonResponse($relationship->toArray()); }
/** */ public function getRelationships() { return ['sessions' => Session::getSerializer()->getType(null), 'groups' => Group::getSerializer()->getType(null), 'activities' => Activity::getSerializer()->getType(null)]; }
/** */ public function getRelationships() { return ['activities' => Activity::getSerializer()->getType(null)]; }
/** * Updates a Activity with the given idand the provided data * * @param mixed $id * @param mixed $data * @return PayloadInterface */ public function update($id, $data) { // find $model = $this->get($id); if ($model === null) { return new NotFound(['message' => 'Activity not found.']); } // hydrate $data = $this->normalize($data); $serializer = Activity::getSerializer(); $model = $serializer->hydrate($model, $data); $this->hydrateRelationships($model, $data); // dispatch pre save hooks $this->dispatch(ActivityEvent::PRE_UPDATE, $model, $data); $this->dispatch(ActivityEvent::PRE_SAVE, $model, $data); // validate $validator = $this->getValidator($model); if ($validator !== null && !$validator->validate($model)) { return new NotValid(['errors' => $validator->getValidationFailures()]); } // save and dispath post save hooks $rows = $model->save(); $this->dispatch(ActivityEvent::POST_UPDATE, $model, $data); $this->dispatch(ActivityEvent::POST_SAVE, $model, $data); $payload = ['model' => $model]; if ($rows === 0) { return new NotUpdated($payload); } return new Updated($payload); }