public function postAction(Request $request, $parent = null)
    {
        $title = trim($request->request->get('title'));

        if (!$this->isGranted('CREATE', $parent)) {
            throw new AccessDeniedHttpException(sprintf('You are not authorized to create %s MediaFolder.', $title));
        }

        try {
            $uid = $request->request->get('uid', null);
            if (null !== $uid) {
                $mediaFolder = $this->getMediaFolderRepository()->find($uid);
                $mediaFolder->setTitle($title);
            } else {
                $mediaFolder = new MediaFolder();
                $mediaFolder->setUrl($request->request->get('url', StringUtils::urlize($title)));
                $mediaFolder->setTitle($title);
                if (null === $parent) {
                    $parent = $this->getMediaFolderRepository()->getRoot();
                }

                if ($this->mediaFolderAlreadyExists($title, $parent)) {
                    throw new BadRequestHttpException(sprintf(
                        'A MediaFolder named `%s` already exists in `%s`.',
                        $title,
                        $parent->getTitle()
                    ));
                }

                $mediaFolder->setParent($parent);
                $this->getMediaFolderRepository()->insertNodeAsLastChildOf($mediaFolder, $parent);
            }

            $this->getEntityManager()->persist($mediaFolder);
            $this->getEntityManager()->flush();

            $response = $this->createJsonResponse(null, 201, [
                'BB-RESOURCE-UID' => $mediaFolder->getUid(),
                'Location'        => $this->getApplication()->getRouting()->getUrlByRouteName(
                    'bb.rest.media-folder.get',
                    [
                        'version' => $request->attributes->get('version'),
                        'uid'     => $mediaFolder->getUid(),
                    ],
                    '',
                    false
                ),
            ]);
        } catch (\Exception $e) {
            $response = $this->createResponse(sprintf('Internal server error: %s', $e->getMessage()));
        }

        return $response;
    }
public function postAction(Request $request, $parent = null)
    {
        $title = trim($request->request->get('title'));

        if (!$this->isGranted('CREATE', $parent)) {
            throw new AccessDeniedHttpException(sprintf('You are not authorized to create %s MediaFolder.', $title));
        }

        try {
            $uid = $request->request->get('uid', null);
            if (null !== $uid) {
                $mediaFolder = $this->getMediaFolderRepository()->find($uid);
                $mediaFolder->setTitle($title);
            } else {
                $mediaFolder = new MediaFolder();
                $mediaFolder->setUrl($request->request->get('url', StringUtils::urlize($title)));
                $mediaFolder->setTitle($title);
                if (null === $parent) {
                    $parent = $this->getMediaFolderRepository()->getRoot();
                }

                if ($this->mediaFolderAlreadyExists($title, $parent)) {
                    throw new BadRequestHttpException(sprintf(
                        'A MediaFolder named `%s` already exists in `%s`.',
                        $title,
                        $parent->getTitle()
                    ));
                }

                $mediaFolder->setParent($parent);
                $this->getMediaFolderRepository()->insertNodeAsLastChildOf($mediaFolder, $parent);
            }

            $this->getEntityManager()->persist($mediaFolder);
            $this->getEntityManager()->flush();

            $response = $this->createJsonResponse(null, 201, [
                'BB-RESOURCE-UID' => $mediaFolder->getUid(),
                'Location'        => $this->getApplication()->getRouting()->getUrlByRouteName(
                    'bb.rest.media-folder.get',
                    [
                        'version' => $request->attributes->get('version'),
                        'uid'     => $mediaFolder->getUid(),
                    ],
                    '',
                    false
                ),
            ]);
        } catch (\Exception $e) {
            $response = $this->createResponse(sprintf('Internal server error: %s', $e->getMessage()));
        }

        return $response;
    }
public function postAction ( Request $ request , $ parent = null ) { $ title = trim ( $ request -> request -> get ( 'title' ) ) ; if ( ! $ this -> isGranted ( 'CREATE' , $ parent ) ) { throw new AccessDeniedHttpException ( sprintf ( 'You are not authorized to create %s MediaFolder.' , $ title ) ) ; } try { $ uid = $ request -> request -> get ( 'uid' , null ) ; if ( null !== $ uid ) { $ mediaFolder = $ this -> getMediaFolderRepository ( ) -> find ( $ uid ) ; $ mediaFolder -> setTitle ( $ title ) ; } else { $ mediaFolder = new MediaFolder ( ) ; $ mediaFolder -> setUrl ( $ request -> request -> get ( 'url' , StringUtils :: urlize ( $ title ) ) ) ; $ mediaFolder -> setTitle ( $ title ) ; if ( null === $ parent ) { $ parent = $ this -> getMediaFolderRepository ( ) -> getRoot ( ) ; } if ( $ this -> mediaFolderAlreadyExists ( $ title , $ parent ) ) { throw new BadRequestHttpException ( sprintf ( 'A MediaFolder named `%s` already exists in `%s`.' , $ title , $ parent -> getTitle ( ) ) ) ; } $ mediaFolder -> setParent ( $ parent ) ; $ this -> getMediaFolderRepository ( ) -> insertNodeAsLastChildOf ( $ mediaFolder , $ parent ) ; } $ this -> getEntityManager ( ) -> persist ( $ mediaFolder ) ; $ this -> getEntityManager ( ) -> flush ( ) ; $ response = $ this -> createJsonResponse ( null , 201 , [ 'BB-RESOURCE-UID' => $ mediaFolder -> getUid ( ) , 'Location' => $ this -> getApplication ( ) -> getRouting ( ) -> getUrlByRouteName ( 'bb.rest.media-folder.get' , [ 'version' => $ request -> attributes -> get ( 'version' ) , 'uid' => $ mediaFolder -> getUid ( ) , ] , '' , false ) , ] ) ; } catch ( \ Exception $ e ) { $ response = $ this -> createResponse ( sprintf ( 'Internal server error: %s' , $ e -> getMessage ( ) ) ) ; } return $ response ; }
Create a media folder
and if a parent is provided added has its last child

@param MediaFolder $mediaFolder

@Rest\RequestParam(name="title", description="media title", requirements={
@Assert\NotBlank()
})
@Rest\ParamConverter(
name="parent", id_name="parent_uid", id_source="request", class="BackBee\NestedNode\MediaFolder", required=false
)
@Rest\Security("is_fully_authenticated() & has_role('ROLE_API_USER')")
Create a media folder and if a parent is provided added has its last child