service = $service; $this->repository = $repository; $this->message_repository = $message_repository; $this->member_repository = $member_repository; } /** * @return mixed */ public function getMyTeams(){ try { $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $teams = $this->repository->getTeamsByMember($current_member); $response = new PagingResponse ( count($teams), count($teams), 1, 1, $teams ); return $this->ok ( $response->toArray ( $expand = Request::input('expand',''), $fields = [], $relations = [], $params = [ 'current_member' => $current_member ] ) ); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @return mixed */ public function getMyTeam($team_id){ try { $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $team = $this->repository->getById($team_id); if(is_null($team)) throw new EntityNotFoundException(); if(!$team->isMember($current_member)) throw new EntityNotFoundException(); return $this->ok ( SerializerRegistry::getInstance() ->getSerializer($team) ->serialize ( $expand = Request::input('expand',''), $fields = [], $relations = [], $params = [ 'current_member' => $current_member ] ) ); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @return mixed */ public function addTeam(){ try { if(!Request::isJson()) return $this->error400(); $data = Request::json(); $rules = array ( 'name' => 'required|string|max:255', 'description' => 'required|sometimes|string|max:512', ); // Creates a Validator instance and validates the data. $validation = Validator::make($data->all(), $rules); if ($validation->fails()) { $messages = $validation->messages()->toArray(); return $this->error412 ( $messages ); } $fields = array ( 'name', 'description', ); $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $team = $this->service->addTeam(HTMLCleaner::cleanData($data->all(), $fields), $current_member); return $this->created(SerializerRegistry::getInstance()->getSerializer($team)->serialize($expand = 'owner,members,member')); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @return mixed */ public function deleteTeam($team_id){ try { $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $this->service->deleteTeam($team_id); return $this->deleted(); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @return mixed */ public function updateTeam($team_id){ try { if(!Request::isJson()) return $this->error400(); $data = Request::json(); $rules = array ( 'name' => 'required|string|max:255', 'description' => 'string|max:512', ); // Creates a Validator instance and validates the data. $validation = Validator::make($data->all(), $rules); if ($validation->fails()) { $messages = $validation->messages()->toArray(); return $this->error412 ( $messages ); } $fields = array ( 'name', 'description', ); $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $team = $this->service->updateTeam(HTMLCleaner::cleanData($data->all(), $fields), $team_id); return $this->updated(SerializerRegistry::getInstance()->getSerializer($team)->serialize($expand = 'owner,members,member')); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @return mixed */ public function getMyTeamMessages($team_id){ $values = Request::all(); $rules = array ( 'page' => 'integer|min:1', 'per_page' => 'required_with:page|integer|min:5|max:100', ); try { $validation = Validator::make($values, $rules); if ($validation->fails()) { $ex = new ValidationException(); throw $ex->setMessages($validation->messages()->toArray()); } // default values $page = 1; $per_page = 5; if (Request::has('page')) { $page = intval(Request::input('page')); $per_page = intval(Request::input('per_page')); } $filter = null; if (Request::has('filter')) { $filter = FilterParser::parse(Request::input('filter'), array ( 'owner_id' => ['=='], 'sent_date' => ['>', '<', '<=', '>=', '=='], )); } $order = null; if (Request::has('order')) { $order = OrderParser::parse(Request::input('order'), array ( 'sent_date', 'id', )); } if(is_null($filter)) $filter = new Filter(); $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $team = $this->repository->getById($team_id); if(is_null($team)) throw new EntityNotFoundException(); if(!$team->isMember($current_member)) throw new EntityNotFoundException(); $data = $this->message_repository->getAllSentByTeamPaginated ( $team_id, new PagingInfo($page, $per_page), $filter, $order ); return $this->ok($data->toArray(Request::input('expand', ''))); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @return mixed */ public function postTeamMessage($team_id){ try { if(!Request::isJson()) return $this->error400(); $data = Request::json(); $rules = array ( 'body' => 'required|string', 'priority' => 'required|sometimes|string|chat_message_priority', ); $values = $data->all(); // Creates a Validator instance and validates the data. $validation = Validator::make($values, $rules); if ($validation->fails()) { $messages = $validation->messages()->toArray(); return $this->error412 ( $messages ); } $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); if(!isset($values['priority'])) $values['priority'] = PushNotificationMessagePriority::Normal; $message = $this->service->postMessage($team_id, $values); return $this->created(SerializerRegistry::getInstance()->getSerializer($message)->serialize($expand = 'team,owner')); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @param $member_id * @return mixed */ public function addMember2MyTeam($team_id, $member_id){ try { if(!Request::isJson()) return $this->error400(); $data = Request::json(); $rules = array ( 'permission' => 'required|string|team_permission', ); $values = $data->all(); // Creates a Validator instance and validates the data. $validation = Validator::make($values, $rules); if ($validation->fails()) { $messages = $validation->messages()->toArray(); return $this->error412 ( $messages ); } $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $invitation = $this->service->addMember2Team($team_id, $member_id, $values['permission']); return $this->created(SerializerRegistry::getInstance()->getSerializer($invitation)->serialize($expand = 'team,inviter,invitee')); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (\Exception $ex) { Log::error($ex); return $this->error500($ex); } } /** * @param $team_id * @param $member_id * @return mixed */ public function removedMemberFromMyTeam($team_id, $member_id){ try { $current_member = $this->resource_server_context->getCurrentUser(); if (is_null($current_member)) return $this->error403(); $this->service->removeMemberFromTeam($team_id, $member_id); return $this->deleted(); } catch (ValidationException $ex1) { Log::warning($ex1); return $this->error412(array($ex1->getMessage())); } catch(EntityNotFoundException $ex2) { Log::warning($ex2); return $this->error404(array('message'=> $ex2->getMessage())); } catch (Exception $ex) { Log::error($ex); return $this->error500($ex); } } }