<commit_before><?php

/*
 * This file is part of the ONGR package.
 *
 * (c) NFQ Technologies UAB <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ONGR\ApiBundle\Controller;

use Elasticsearch\Common\Exceptions\Missing404Exception;
use ONGR\ApiBundle\Request\RestRequest;
use ONGR\ElasticsearchBundle\DSL\Search;
use ONGR\ElasticsearchBundle\ORM\Manager;
use ONGR\ElasticsearchBundle\ORM\Repository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * CRUD implementation for Api Controller.
 */
class RestController extends AbstractRestController implements RestControllerInterface
{
    /**
     * {@inheritdoc}
     */
    public function postAction(RestRequest $restRequest, $id = null)
    {
        $validator = $this->get('ongr_api.rest.validator');

        $data = $validator->validate($restRequest);
        if ($data === false) {
            return $this->renderRest(
                ['message' => 'Validation error!', 'errors' => $validator->getErrors()],
                Response::HTTP_NOT_ACCEPTABLE
            );
        }

        if ($id === null && !isset($data['id'])) {
            return $this->renderRest(
                ['message' => 'No identifier found!'],
                Response::HTTP_BAD_REQUEST
            );
        }

        if ($id !== null) {
            $data['id'] = $id;
        } else {
            $id = $data['id'];
        }

        $repository = $restRequest->getRepository();
        $types = $repository->getTypes();

        $data['_id'] = $id;
        $repository->getManager()->getConnection()->bulk('create', reset($types), $data);
        $repository->getManager()->commit();

        return $this->renderRest(
            null,
            Response::HTTP_CREATED,
            ['Location' => $this->generateRestUrl($restRequest->getRequest(), $id)]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getAction(RestRequest $restRequest, $id = null)
    {
        if ($id !== null) {
            $data = $restRequest->getRepository()->find($id, Repository::RESULTS_ARRAY);
            if ($data === null) {
                return $this->renderRest(null, Response::HTTP_NOT_FOUND);
            }
        } else {
            $search = new Search();
            if ($restRequest->query->has('from')) {
                $search->setFrom($restRequest->query->get('from'));
            }
            if ($restRequest->query->has('size')) {
                $search->setSize($restRequest->query->get('size'));
            }
            $data = $restRequest->getRepository()->execute($search, Repository::RESULTS_ARRAY);
        }

        return $this->renderRest($data);
    }

    /**
     * {@inheritdoc}
     */
    public function putAction(RestRequest $restRequest, $id = null)
    {
        $data = $restRequest->getData();
        $validator = $this->get('ongr_api.rest.validator');
        $data = $validator->validate($restRequest);

        if ($data === false) {
            return $this->renderRest(
                [
                    'message' => 'Validation error!',
                    'errors' => $validator->getErrors(),
                ],
                Response::HTTP_NOT_ACCEPTABLE
            );
        }

        if ($id === null && !isset($data['id'])) {
            return $this->renderRest(
                ['message' => 'No identifier found!'],
                Response::HTTP_BAD_REQUEST
            );
        }

        if ($id !== null) {
            $data['id'] = $id;
        } else {
            $id = $data['id'];
        }

        $repository = $restRequest->getRepository();
        $types = $repository->getTypes();

        $data['_id'] = $id;
        $repository->getManager()->getConnection()->bulk('index', reset($types), $data);
        $repository->getManager()->commit();

        return $this->renderRest(
            null,
            Response::HTTP_NO_CONTENT,
            ['Location' => $this->generateRestUrl($restRequest->getRequest(), $id)]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function deleteAction(RestRequest $restRequest, $id = null)
    {
        if ($id === null) {
            return $this->renderRest(null, Response::HTTP_BAD_REQUEST);
        }

        $connection = $restRequest->getRepository()->getManager()->getConnection();
        try {
            $connection->delete(
                [
                    'id' => $id,
                    'type' => $restRequest->getRepository()->getTypes(),
                    'index' => $connection->getIndexName(),
                ]
            );
        } catch (Missing404Exception $e) {
            return $this->renderRest(<commit_msg>Post action return conflict response if resouce exists<commit_after><?php

/*
 * This file is part of the ONGR package.
 *
 * (c) NFQ Technologies UAB <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ONGR\ApiBundle\Controller;

use Elasticsearch\Common\Exceptions\Missing404Exception;
use ONGR\ApiBundle\Request\RestRequest;
use ONGR\ElasticsearchBundle\DSL\Search;
use ONGR\ElasticsearchBundle\ORM\Manager;
use ONGR\ElasticsearchBundle\ORM\Repository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * CRUD implementation for Api Controller.
 */
class RestController extends AbstractRestController implements RestControllerInterface
{
    /**
     * {@inheritdoc}
     */
    public function postAction(RestRequest $restRequest, $id = null)
    {
        $validator = $this->get('ongr_api.rest.validator');

        $data = $validator->validate($restRequest);
        if ($data === false) {
            return $this->renderRest(
                ['message' => 'Validation error!', 'errors' => $validator->getErrors()],
                Response::HTTP_NOT_ACCEPTABLE
            );
        }

        if ($id === null && !isset($data['id'])) {
            return $this->renderRest(
                ['message' => 'No identifier found!'],
                Response::HTTP_BAD_REQUEST
            );
        }

        if ($id !== null) {
            $data['id'] = $id;
        } else {
            $id = $data['id'];
        }

        $repository = $restRequest->getRepository();

        if ($repository->find($id, Repository::RESULTS_RAW) !== null) {
            return $this->renderRest(['message' => 'Resource already exists!'], Response::HTTP_CONFLICT);
        }

        $types = $repository->getTypes();
        $data['_id'] = $id;
        $repository->getManager()->getConnection()->bulk('create', reset($types), $data);
        $repository->getManager()->commit();

        return $this->renderRest(
            null,
            Response::HTTP_CREATED,
            ['Location' => $this->generateRestUrl($restRequest->getRequest(), $id)]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getAction(RestRequest $restRequest, $id = null)
    {
        if ($id !== null) {
            $data = $restRequest->getRepository()->find($id, Repository::RESULTS_ARRAY);
            if ($data === null) {
                return $this->renderRest(null, Response::HTTP_NOT_FOUND);
            }
        } else {
            $search = new Search();
            if ($restRequest->query->has('from')) {
                $search->setFrom($restRequest->query->get('from'));
            }
            if ($restRequest->query->has('size')) {
                $search->setSize($restRequest->query->get('size'));
            }
            $data = $restRequest->getRepository()->execute($search, Repository::RESULTS_ARRAY);
        }

        return $this->renderRest($data);
    }

    /**
     * {@inheritdoc}
     */
    public function putAction(RestRequest $restRequest, $id = null)
    {
        $data = $restRequest->getData();
        $validator = $this->get('ongr_api.rest.validator');
        $data = $validator->validate($restRequest);

        if ($data === false) {
            return $this->renderRest(
                [
                    'message' => 'Validation error!',
                    'errors'  => $validator->getErrors(),
                ],
                Response::HTTP_NOT_ACCEPTABLE
            );
        }

        if ($id === null && !isset($data['id'])) {
            return $this->renderRest(
                ['message' => 'No identifier found!'],
                Response::HTTP_BAD_REQUEST
            );
        }

        if ($id !== null) {
            $data['id'] = $id;
        } else {
            $id = $data['id'];
        }

        $repository = $restRequest->getRepository();
        $types = $repository->getTypes();

        $data['_id'] = $id;
        $repository->getManager()->getConnection()->bulk('index', reset($types), $data);
        $repository->getManager()->commit();

        return $this->renderRest(
            null,
            Response::HTTP_NO_CONTENT,
            ['Location' => $this->generateRestUrl($restRequest->getRequest(), $id)]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function deleteAction(RestRequest $restRequest, $id = null)
    {
        if ($id === null) {
            return $this->renderRest(null, Response::HTTP_BAD_REQUEST);
        }

        $connection = $restRequest->getRepository()->getManager()->getConnection();
        try {
            $connection->delete(
                [
                    'id'    => $id,
                    'type'  => $restRequest->getRepository()->getTypes(),
                    'index' => $connection->getIndexName(),
                ]
            );
        } catch (Missing404Exception $e) {
            return $this->renderRest(
{'fraction_non_alphanumeric': 0.15645636492939144, 'fraction_numerical': 0.0012191405059433099, 'mean_word_length': 1.9902794653705953, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 144, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1979420', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2617, 'n_words': 496}